views:

346

answers:

4

Hi, I am trying to plot a series of 2D matrices containing ones and zeros (effectively black and white images) in matlab, which are ordered in 3D.

The code I have so far is:

function PlotthreeD()

 numrows = 100;
 numcols = 100;

 Plot1 = zeros(numcols);
 Plot1(20:50,20:50) = 1;

 Plot2 = zeros(numcols);
 Plot1(20:70,20:90) = 1;

 Plot3 = zeros(numcols);
 Plot3(20:50,20:50) = 1;

         B = cat(3, Plot1, Plot2, Plot3);

 figure; 
 offset = 100;
 hold on; 

 for i=1:3; 
     mesh(B(:,:,i)+offset*(i));
 end

end

Is there a drawing command (rather than mesh) that will allow me show the 2D arrays as solid shapes (where the matrix elements equal 1), rather than having a these regions shown as being raised (as they are with mesh)?

Thanks in advance,

James

A: 

Investigate the Matlab function imagesc()

High Performance Mark
A: 

The function PCOLOR is one option. The one thing you would have to be mindful of is this comment in the documentation:

The default shading is faceted, which colors each cell with a single color. The last row and column of C are not used in this case.

So you may want to add an extra row and column of zeroes to your matrix by adding 1 to numrows and numcols.

Here's some sample code from the documentation:

pcolor(hadamard(20))
colormap(gray(2))
axis ij
axis square

alt text

gnovice
A: 

This will do it:

numrows = 100;
numcols = 100;
close all;

Plot1 = zeros(numcols);
Plot1(20:50,20:50) = 1;

Plot2 = zeros(numcols);
Plot2(20:70,20:90) = 1;

Plot3 = zeros(numcols);
Plot3(20:50,20:50) = 1;

B = cat(3, Plot1, Plot2, Plot3);
B(B==0)=NaN;

figure;
offset = 100;
hold on;

for i=1:3;
    surf(B(:,:,i)+offset*(i)); 
end
AVB
Thanks a lot, thats a great help.
James
A: 

SPY is also a good way of viewing binary matrices.

MatlabDoug

related questions