How to set specific values in an array to a certain color when using imagesc?

19 views (last 30 days)
I have an array that is 100x100 filled with zeros. I want to set some cells to other values and have the cell change colour depending on the value.
i want cells that have a value of 1 to be green, a value 2 to be white, 3 to be brown and 0 to be blue.
how would i do this?

Answers (1)

Miriam
Miriam on 20 Nov 2018
You can define your own colormap as follows:
x = randi([0 3],10,10);
colors = [0 0 1;... % blue for 0
0 1 0;... % green for 1
1 1 1;... % white for 2
0.65 0.16 0.16]; % brown for 3, etc.
figure;
imagesc(x);
colormap(colors);

Categories

Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!