Storing matrices from a loop in an array
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposta accettata
Guillaume
il 11 Giu 2015
Modificato: Guillaume
il 11 Giu 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 Commenti
Guillaume
il 11 Giu 2015
Modificato: Guillaume
il 11 Giu 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);
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!