Distributing a function over a single dimension of an array in MATLAB?
- by Alex Feinman
I often find myself wanting to collapse an n-dimensional matrix across one dimension, and can't figure out if there is a concise incantation I can use to do this.
For example, when parsing an image, I often want to do something like this. (Note! Illustrative example only. I know about rgb2gray for this specific case.)
img = imread('whatever.jpg');
s = size(img);
for i=1:s(1)
for j=1:s(2)
bw_img = mean(img(i,j,:));
end
end
I would love to express this as something like:
bw = on(color, 3, @mean);
or
bw(:,:,1) = mean(color);
Is there a short way to do this?