Convert a 3D image array into a cell array

4 views (last 30 days)
Hi,
I know this will be basic for an experienced Matlab user but what I'm trying to do is convert an image stack, say a [mxnXp] matrix where p is the indexing dimension into a [px1] cell array.
My motivation for doing this originally was because I wanted to check to see which 'frames' of my image array were blank, i.e. equal to zeros(m,n).
I tried to use something like the following:
indices = find(arrayfun(@(IDX) ~isequal(IDX), isequal(imstack, zeros(size(imstack,1),size(imstack,2)))));
however this didn't work since imstack is a 3D matrix (at least this is why I suspect it didn't work)
To remedy I thought to convert imstack to a px1 cell array, with the view to using a 'cellfun' version of the above. And this is where I got stuck converting imstack to a cell array.
Any suggestions would be appreciated.
Thank you.
n

Accepted Answer

Razvan
Razvan on 29 Sep 2012
Edited: Razvan on 29 Sep 2012
Try this:
blank = true(1,p);
for k = 1:p
if any(M(:,:,k))
blank(k) = false;
end
end
where M is your 3d matrix and p is the number of frames. You'll get in the end a vector blank which is true for the blank frames. To get the indices do
find(blank)

More Answers (1)

Matt J
Matt J on 29 Sep 2012
Edited: Matt J on 29 Sep 2012
A more efficient approach than the for-loop would be
blank=~any(reshape(M,[],p),1);
Also, to put the slices of an array into cells, you can use this FEX file
In your case, this would look like
Mcell=mat2tiles(M,[inf,inf,1]);
Mcell=Mcell(:); %make px1

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!