Storing matrices from a loop in an array

1 view (last 30 days)
My program takes a 3D matrix and creates as many 2D matrices as the size of the third dimension. This matrices are later represented as images. The problem is my code isn't really storing this matrices but overwriting them everytime. I need to store them in another array. I also need to store only those matrices that are not made up of only zero values. I have successfully obtained the number of only-zero matrices but I haven't succeeded in storing them. What is the best way to do this?
n=1;
for s=1:d3
thisimage=MA(:,:,s);
%counts how many matrices have only zero values
if sum(thisimage)==0
n=n+1;
end
% create a new matrix, size, minus the zero only matrices
%store non zero matrices in this vector
t=cell(d3-n);
if sum(thisimage)~=0
t(s)=thisimage;
end
subplot(12,17,s),subimage(thisimage);figure(gcf)
axis off
colormap(gray);
%%Draw only every five images
s=s+5;
end
Thank you for your help

Accepted Answer

Guillaume
Guillaume on 11 Jun 2015
Edited: Guillaume on 11 Jun 2015
There are two major errors with your code:
t(s) = thisimage;
is going to throw an error: conversion to cell from double is not possible.
The second problem is that your s = s+5 has no effect. You can't modify the loop index once you've started looping. You would have to use:
for s = 1:5:d3
to draw every 5 five images.
I'm unclear on what you're trying to do. If all you're trying to do is convert the 3D matrix into a cell array of 2D matrices (but why?), then you don't need a loop:
t = squeeze(num2cell(thisimage, [1 2]))
and if you want to remove the all zeros matrices:
t = t(cellfun(@(m) any(m(:) ~= 0), t))
  2 Comments
Fer
Fer on 11 Jun 2015
Thank you. Maybe my approach isn't correct. I'm taking 2D matrices from my original 3D in order to represent the images, so the loop's main objective is the drawing of the image. I thought of taking advantage of that loop to also store the data of the new 2D matrices, although I know that is a bit redundant, I do not fully understand the difference between a 3D matrix and an array of 2D matrices. It may be better to build this array first and then represent all the images (2D matrices) contained in it, which is way I wanted to know how to store the matrices.
Guillaume
Guillaume on 11 Jun 2015
Edited: Guillaume on 11 Jun 2015
A 1D array of 2D matrices is a 3D matrix (if all the matrices are the same size). You can also store matrices of any size and dimension into a cell array (as in my answer), but if all your matrices are the same size, there's no benefit.
In your case, what I'd do is to remove the all zeros 2D matrices, then iterate over the third dimension for plotting. There would be no benefit in storing them in a cell array. You'd just use a different syntax ( thisimage(:, :, k) versus t{k}). So:
thisimage(:, :, all(all(thisimage == 0))) = []; %remove all zero 2D matrices
for imgidx = 1 : size(thisimage, 3)
subplot(12, 17, imgidx); %that's a lot of plots!
subimage(thisimage(:, :, imgidx));
%... other formatting
end
figure(gcf);

Sign in to comment.

More Answers (0)

Categories

Find more on Display Image 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!