itaied
itaied

Reputation: 7107

MATLAB: how to set colors in matrix

I have a matrix N*N, with three different values, for example 0, 0.5, 1. How can i print to the screen an image, which each value represent a different color? Important: the matrix is a loop so the values may change (i want to print the matrix every iteration).

I tried to use colormap, it worked fine if all the three values were in the matrix, but when one or two values only remain, the colors were changed.

How I want it to work: matrix with values 0, 0.5, 1 prints to the screen a matrix which each cell contains 0 colored black, 0.5 colored green, 1 colored yellow.

Thanks a lot!

Upvotes: 4

Views: 3666

Answers (2)

Smash
Smash

Reputation: 3802

Just create your own colormap that has only three possible values:

a = [1 0.5 0;1 .5 0;0.5 0 1];
b = [1 0 1;1 1 0;0 0 1];
cmap = [0,0,0;0,1,0;1,1,0];
clims = [0 1];
imagesc(a,clims); colormap(cmap);
imagesc(b,clims); colormap(cmap);

a gives:

a

b gives:

enter image description here

Upvotes: 5

Gilad
Gilad

Reputation: 6595

I would try maybe imagsec. Or any other scaling for the colors. Start with gray scale. RGB will be alittle more complex

Upvotes: 0

Related Questions