Achim
Achim

Reputation: 292

Colormatrix from colorvector

I have a scatter3 plot in which I use a vector C to define the colors of the different points. Here the color of the colormap is linearly mapped onto the numbers which are in C. I want to somehow find the RGB-values of each value inside my C vector. So I want a x by 3 matrix out of my C vector. Anybody knows if this is possible?

Best wishes, Achim

Edit: Thanks to @Aabaz I was able to solve the problem. Here is my solution:

colors_current = colormap;
color = [color zeros(length(color),2)];
stepw = floor(length(color)/length(colors_current));
colorsort = sortrows(color);
color_old = 0;
counter = 1;

for i = stepw:stepw:length(JAbs)
    color_indices = find(color_old < color(:,1) & color(:,1) < color_sort(i));
    if counter >= length(colors_current) 
        break;
    end
    for j=1:length(color_indices)
        JAbs(color_indices(j),:) = colors_current(counter,:);
    end
    color_old = colorsort(i);
    counter = counter + 1;
end

Not the most elegant way but it seems to work.

Upvotes: 3

Views: 218

Answers (1)

Aabaz
Aabaz

Reputation: 3116

The function colormap used with no argument, returns the current axes colormap as an m by 3 matrix storing the RGB codes for each color. From there you can get to the RGB code for every element in your vector C.

UPDATE: I am sorry, I must have misread your question because I did not understand you were looking for an explicit way to get the rgb codes, just the connexion between the colormap and the rgb code. Anyway I see you found the solution yourself, well done. Did a quick try myself which I give you here :

n=10;
C=rand(n,1);
map=colormap(jet);
Cregspaced=(min(C):(max(C)-min(C))/(size(map,1)-1):max(C))';
Cmapindex=interp1(Cregspaced,(1:size(map,1))',C,'nearest');
Crgb=map(Cmapindex,:);

This should work, depending on how Matlab interpolates the index for the colormap. You can test it against your own solution to see if the results match.

Upvotes: 2

Related Questions