This function should help when it is difficult to generalize a function to operate on all dimensions.
Take the eig of each page in the 3rd dimension
b = apply(@eig,rand(4,4,3),3,'uni',0)
Normalize each column
b = apply(@norm, rand(5,3), 2)
Normalize each row
b = apply(@norm, rand(5,3), 1)
This looks like a useful helper-function to me. I have what I hope is a complementary helper-function
that can be used when you want to apply a function to vectors along a dimension, rather than (potentially higher dimensional) arrays perpendicular to that dimension.
To clarify the differences, with a 3d array:
r = randn(4,4,4);
the second of the four elements returned by:
n = apply(@norm, r, 3)
gives the matrix norm of the second matrix:
where matrices are stacked in the third dimension.
n = dimfunc(@norm, r, 3)
gives you a 4-by-4 matrix of the vector norms, for vectors running along the third dimension, e.g. element n(2,3) equals:
I hope that both functions will be useful for simplifying code to handle nD arrays.
Good for clear code but doesn't increase speed.
Examples worked fine. I'll refine rating after error checking and documentation completed.
Do not waste bandwidth with something unfinished ...
Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.