I often find that I want to collapse an n-dimensional matrix in one dimension using a user-defined function and cannot figure out if there is a compressed spell I can use to do this.
For example, when analyzing an image, I often want to do something like this. (Note: for illustrative purposes only. I know about rgb2gray for this particular case.)
img = imread('whatever.jpg'); s = size(img); for i=1:s(1) for j=1:s(2) bw_img(i,j) = mean(img(i,j,:)); end end
I would like to express it somehow like:
bw = on(color, 3, @mean);
or
bw(:,:,1) = mean(color);
Is there a short way to do this?
EDIT: Apparently
mean already doing this; I want to be able to do this for any function that I wrote. For example.
... filtered_img(i,j) = reddish_tint(img(i,j,:)); ...
Where
function out = reddish_tint(in) out = in(1) * 0.5 + in(2) * 0.25 + in(3) * 0.25; end
function arrays matrix multidimensional-array matlab
Alex feinman
source share