Suppose we have an 3-D array (tensor) and we want to apply in each slice a function, e.g., myFun = @(x)mean(x)
. Is there any way to do this without for
loop using build-in functions (possibly withbsxfun
, or arrayfun
, or accumarray
)?
For loop example:
inputA = rand(10,5,20);
for sl = 1:size(A,3)
inputB = myFun(inputA(:,:,sl));
end
Thank you.
EDIT:
inputB = arrayfun(@(iterSlice) myFun(inputA(:,:,iterSlice), 1:size(inputA,3))
PS: I would like to mention, that the handler function applied is more complicated in each slice, mean
was an example included in the handler function.