# MATLAB: How to associate specific colors with specific numbers of a matrix

colormapmatrix

Title basically says it all; I want to be able to control which color get associated with which number. I have tried the code stated below to give a color to every number from 0 to 5. The way I thought this code would work was that the first row of cmap would be the color for 0, the second for 1, the third for 2, etc. But it still seems to associate colors from cmap randomly to the numbers in the matrix A.
A=[1:3;2:4;3:5;0 0 0];cmap = [0 0.5 0.5;0.75 0.75 0;1 0.75 0.25;        0.25 0 1;1 1 0.75;0.75 0 0.75;         0.75 0.75 0;0.25 0.25 0.25;1 0.5 0];colormap(cmap);caxis([0 5]); imagesc(A); 

cb = colorbar(); set(cb,'YTick', 1:9)
unique(A(:))ans =     0     1     2     3     4     5
 A=[1:3;2:4;3:5;0 0 0];cmap = [0 0.5 0.5;    0.75 0.75 0;    1 0.75 0.25;    0.25 0 1;    1 1 0.75;    0.75 0 0.75];colormap(cmap);imagesc(A);cb = colorbar(); set(cb,'YTick', 0:5)
A=[1:3;2:4;3:5;0 0 0];cmap = [0 0.5 0.5;0.75 0.75 0;1 0.75 0.25;    0.25 0 1;1 1 0.75;0.75 0 0.75;    0.75 0.75 0;0.25 0.25 0.25;1 0.5 0];imagesc(A);colorbar()colormap(cmap);caxis([0 9]);  % <----