bsxfun(func, x, y) arrayfun(func, ...)
x, y
.bsxfun
applies element-by-element a binary function to two vectors
or matrices of the same size. arrayfun
applies func
to each element of the arrays and
returns an array of the same size.
Vectorize
X <- matrix(rep(1:10, each = 10), 10, 10)
Y <- t(X)
bsxfun("*", X, Y) # multiplication table
f <- function(x, y) x[1] * y[1] # function not vectorized
A <- matrix(c(2, 3, 5, 7), 2, 2)
B <- matrix(c(11, 13, 17, 19), 2, 2)
arrayfun(f, A, B)
Run the code above in your browser using DataLab