Illusionist
Illusionist

Reputation: 5489

plot a structure in matlab

I have a structure of known (but variable length) like this-

1 0 1
0 1 1

I want to plot this structure as colored squares- color each 1 as a green square, and 0s as a red square

Something like

[green][red][green]
[red][green][green]

It would be nice to add some optional text on each square.

Also, I have another data structure of same length, with numbers going from 0.0 to 1.0 .. something like

0.99 0.09 1.0
0.09 0.87 1.0

I want to possibly change the intensity of red and green in the above pic depending on how close to 0 or 1 is the corresponding number.

Any suggestions are helpful. Thanks a lot.

Upvotes: 0

Views: 1284

Answers (2)

gevang
gevang

Reputation: 5014

You can set the colormap after displaying the matrix as a scaled image:

Z = [1 0 1; 0 1 1];
figure; imagesc(Z); 
colormap([1 0 0; 0 1 0]);
axis off; axis image;

Upvotes: 2

PearsonArtPhoto
PearsonArtPhoto

Reputation: 39698

Essentially, you want to turn the 2-d structure into a 3-d one, the last dimension being x3, one for each of the RGB colors. Start with this code, and play with it until it does what you want.

map=zeros(2,2,3);
map(:,:,1)=[1 1; 0 0];
map(:,:,2)=[1 0; 1 0];
map(:,:,3)=[0 0; 0 0];

figure;image(map);

Alternatively, you could have a colormap, which would translate the pixel counts to intensity. It's been a while since I've done it, but I can at least point you in the right direction. Run the first command, and look at the colormap. You want to have a gradual changing from Green to Red. Format it how you want it, pass it back in with the last command, and see what you get out.

cmap = colormap;
%You'll want to change cmap to meet your needs
imagesc([.1 .2; .8 .9]);
colormap(cmap);

Upvotes: 1

Related Questions