illini_17
illini_17

Reputation: 11

Get matrix with colors of colomap figure

I have generated a heatmap from a matrix of values and applied a colormap on it. Is there a way for me to create an m-by-3 matrix of the colors of each square segment?

Upvotes: 1

Views: 75

Answers (2)

Dan
Dan

Reputation: 45752

My understanding is that you have a matrix of values (m-by-n) which you colour using a colour map and you want to get the 3D (m-by-n-by-3) matrix that is the RBG image of this coloured matrix. In that case try:

%//C is your data
C = randn(m,n);

%//d is the number of discrete colour in your colormap and map is your colormap itself.
d=4;
map = jet(d);

%//Normalize C to index map, i.e. make values of C range from 1 to d
C_norm = (C - min(C(:)))./(max(C(:)) - min(C(:)));  %//Normalize to btw [0,1]
C_discrete = ceil(C_norm*(d-1)+1);                  %//Normalize to btw [1,d]

%//Index in map using linearized (i.e. m*n-by-1 vector) version of C
C_mapped = map(C_discrete(:),:);

%//Reshape to m-by-n-by-3
C_RGBreshape(permute(C_mapped, [1, 3, 2]), m, n, 3);

Upvotes: 1

patrik
patrik

Reputation: 4558

You mean to create a matrix with the colormap? Normally you do it the other way around. Try

cmap = jet(64);
colormap(cmap);

This assigns the colormap to cmap as a matrix and then assigns it to th axes. I also think that the colormap is an axes property and can be accessed through the set and get functions. Also you may not want to use the jet colormap but any colormap can of course be used

Upvotes: 0

Related Questions