Processing all subsets of a larger matrix

I have data in the form of MxNx3xK or MxNx3xKxJ types of matrices. If I wanted to use a function that takes an array of 3 values as an input (e.g. the entire third dimension of the matrix), does anyone know of a way to perform the function on the entire matrix?
The only way I know to do this is with nested for loops which is (or at least has been in earlier versions of matlab) very slow.
An simple example would be an image file that was 640x480x3 (where the third dimension contains RGB data). If I wanted to do a color transform on the whole image without going through pixel by pixel with nested loops, how could I do that?
Thanks in advance.

3 Comments

Your question is not clear
Sean
Sean on 14 Nov 2012
Edited: Sean on 14 Nov 2012
Can you give me an indication of what wasn't clear? The question seems pretty straight forward (to me).
I added an example to go with the question in case that helps clarify things.
It's not clear to me either. What is this? Is it a movie (or set of multiple movies) where k is the frame number, and j is the movie number? Or is it a volumetric color image where k is the slice number in the volume and J is a time point? Or is it something else? Why can't you just extract the color image and do the transform and then combine it into an output image?

Sign in to comment.

 Accepted Answer

out = cellfun(fun, num2cell(data,3), 'uni',0);

3 Comments

Hello Matt,
You've gotten me 90% of the way there. Thank you.
My only issue is that I may be performing a variety of functions, some have only 1 output, but some have more than one.
I understand that I can use "cell2mat(out)" to convert the matrix to an array if I have only 1 output, but if the function output is an array of values then cell2mat() appears to concatentate the data to keep the same number of dimensions as the cell array (instead of adding a new dimension or dimensions from/for the cell contents).
Do you know of a trivial way to keep the dimensions distinct?
Thanks again, Sean
Instead of using CELL2MAT, use CAT and RESHAPE. Example:
A=rand(5,4,3,5,6);
B=cellfun(@(x) x(:)*x(:).' , num2cell(A,3), 'uni',0);
C=reshape(cat(3,B{:}),5,4,[],5,6);
>> Whos C
Name Size Kilobytes Class Attributes
C 5x4x9x5x6 43 double
Thanks... You are awesome. :)

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 14 Nov 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!