I have the following computation I’d like to vectorize in matlab.
I have a N x 3 array, call it a.
I have a 4 x 1 cell array of function handles, call them b.
I would like to create an Nx4 matrix c, such that c(i,j) = b{j}(a(i,:).
b is actually an array, but I don’t know how to write down my representation for c in a format that matlab would understand that uses a matrix.
If your function handles work on arrays (i.e.
b{j}(a)returns a Nx1 array in your example), you can use CELLFUN and CELL2MAT to generate your output array:If your function handles only work on individual rows (i.e.
b{j}needs to be applied to every row ofaseparately, you can throw ARRAYFUN into the mix (readability suffers a bit though; basically, you’re applying each element ofbvia cellfun to each row ofavia arrayfun):