Distributing a function over a single dimension of an array in MATLAB?

Posted by Alex Feinman on Stack Overflow See other posts from Stack Overflow or by Alex Feinman
Published on 2010-06-01T14:58:03Z Indexed on 2010/06/01 15:03 UTC
Read the original article Hit count: 242

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about matlab

Related posts about array-manipulation