The accepted answer seems to be to convert to cells first and then use cellfun
to operate over all of the cells. I do not know the specific application, but in general I would think using bsxfun
to operate over the matrix would be more efficient. Basically bsxfun
applies an operation element-by-element across two arrays. So if you wanted to multiply each item in an n x 1
vector by each item in an m x 1
vector to get an n x m
array, you could use:
vec1 = [ stuff ]; % n x 1 vector
vec2 = [ stuff ]; % m x 1 vector
result = bsxfun('times', vec1.', vec2);
This will give you matrix called result
wherein the (i, j) entry will be the ith element of vec1
multiplied by the jth element of vec2
.
You can use bsxfun
for all sorts of built-in functions, and you can declare your own. The documentation has a list of many built-in functions, but basically you can name any function that accepts two arrays (vector or matrix) as arguments and get it to work.