MATLAB: Color matrix elements by a user-set color map

colormapmatrix manipulation

I have a 965*965 matrix with values ranging from 0 to 14. I would like to color each pixel according to a color map I will define.
I would like all elements that equal 1 to be red, all elements equal 2 to be green, all elements equal 3 to be blue and so on ..
I have tried to use imagesc but :
  • I want the difference between the colors to be more distinct and in imagesc the hues are pretty similar when values are relativly close
  • I have other matrices where some values are missing and I still want the other values to have the sam colors. If I use imagesc then the color scheme changes according to the values in the matrix.

Best Answer

imagesc will always scale the values passed to it (the sc part of the function name) to use the full range of the colormap.
If that is not what you want, why not just use image?
load matrix.mat
matrix(matrix == 14) = 0;
image(matrix)
c = hot(14);
colormap(c)
colorbar
Here I've removed all the values of 14, but notice the colorbar still goes to 14.
Compare that to when the unmodified matrix is shown:
If you're not familiar with the colormap syntax, I'm requesting just 14 distinct colors from the intermal colormap 'hot', and then setting that as the colormap. That's just easier than creating a custom colormap for the example. However, this approach with the right colormap may help you solve the first issue - the hues of the colormap being too similar.
You can also use the Colormap Editor to modify the selected colors in your color map to help make it easier to distinguish different values.