简体   繁体   English

MATLAB:如何将值缩放到颜色条并返回RGB 3列矩阵

[英]MATLAB: How to scale values to colorbar and return RGB 3 column matrix

I have an array (A) of length 500 containing values between 0 and 0.25 I want to scale them to one of the inbuilt maltab colorbars (so that the max value falls at one end and the min at the other). 我有一个长度为500的数组(A),其中包含0到0.25之间的值,我想将它们缩放为内置的maltab颜色条之一(以便最大值位于一端,最小值位于另一端)。

But then I want matlab to tell me what the rgb values it gives each value in A. ie a matrix of size 500x3 但是然后我想让matlab告诉我它给A中每个值的rgb值是什么,即大小为500x3的矩阵

How can I do this, seems like it should be easy. 我应该怎么做,似乎应该很容易。

You can get the colormap rgb values for the colormap jet by typing: cm = jet(number_of_colors) . 您可以通过键入以下命令获取颜色图喷射的颜色图rgb值: cm = jet(number_of_colors) Now you just need to find the correct indice in the colormap-matrix for every value... 现在,您只需要在colormap-matrix中为每个值找到正确的索引...

clear all
number_of_colors = 100;
cm = jet(number_of_colors);  % choose colorbar (jet)

values = rand(1,500)*50 + 20; % your data

values_min = min(values); % range of the colorbar
values_max = max(values);  

% Calculate the respective index in the colormap for every value
idx_in_colorbar = floor(1+ (values - values_min) / (values_max -values_min) * (number_of_colors-1));

matrix_with_rgb = cm(idx_in_colorbar,:)

Matlab actually scales the data a bit differently, so the right formula is: Matlab实际上对数据的缩放有些不同,因此正确的公式是:

idx_in_colorbar = floor(1+ (values - values_min) / (values_max -values_min) * (number_of_colors)); idx_in_colorbar = floor(1+(值-values_min)/(values_max -values_min)*(number_of_colors)); idx_in_colorbar(idx_in_colorbar > number_of_colors) = number_of_colors; idx_in_colorbar(idx_in_colorbar> number_of_colors)= number_of_colors;

see more here: https://edoras.sdsu.edu/doc/matlab/techdoc/ref/caxis.html 在此处查看更多信息: https : //edoras.sdsu.edu/doc/matlab/techdoc/ref/caxis.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM