MATLAB: how to set colors in a matrix

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

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

How I want it to work: a matrix with values ​​of 0, 0.5, 1 displays a matrix in which each cell contains 0 color black, 0.5 color green, 1 color yellow.

Thanks a lot!

+4
source share
2 answers

Just create your own color code 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

+5
source

I would try maybe imagsec. Or any other color scaling. Start with a gray scale. RGB will be more complex

0
source

All Articles