フィルターのクリア

set colormap values to an array

30 ビュー (過去 30 日間)
bsriv
bsriv 2022 年 11 月 15 日
コメント済み: bsriv 2022 年 11 月 16 日
Hello,
I have a 22x1 array that I'd like to assign to a colorscale (jet) so that each value has a corresponding corresponding color
>> colorpost
colorpost =
-1.4721
-11.3430
23.4227
7.0674
-2.7839
42.3308
-2.6353
0.1617
6.3158
-20.6096
2.6023
3.1703
5.8185
-4.1358
6.9481
-5.1080
24.6877
-5.1542
-17.9669
1.8017
-23.3427
-15.8499
But when I do the following it doesn't work
>> c=jet(colorpost)
Error using /
Matrix dimensions must agree.
Error in jet (line 23)
u = [(1:1:n)/n ones(1,n-1) (n:-1:1)/n]';
I've seen a similar problem here but it doesn't seem to help me since that array is all integers https://www.mathworks.com/matlabcentral/answers/897552-get-colormap-values-corresponding-to-array-values

採用された回答

Walter Roberson
Walter Roberson 2022 年 11 月 15 日
colorpost = [
-1.4721
-11.3430
23.4227
7.0674
-2.7839
42.3308
-2.6353
0.1617
6.3158
-20.6096
2.6023
3.1703
5.8185
-4.1358
6.9481
-5.1080
24.6877
-5.1542
-17.9669
1.8017
-23.3427
-15.8499
];
cmap = jet();
numcol = size(cmap,1);
cps = floor(rescale(colorpost, 1, numcol));
colorized_cp = permute(cmap(cps,:), [1 3 2]);
image(colorized_cp)
  1 件のコメント
bsriv
bsriv 2022 年 11 月 16 日
Thank you!

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 11 月 15 日
Try this:
colorpost =[...
-1.4721
-11.3430
23.4227
7.0674
-2.7839
42.3308
-2.6353
0.1617
6.3158
-20.6096
2.6023
3.1703
5.8185
-4.1358
6.9481
-5.1080
24.6877
-5.1542
-17.9669
1.8017
-23.3427
-15.8499];
% Specify the number of colors to be the same as the number of elements in colorpost.
numColors = numel(colorpost)
% Create a colormap.
cmap = jet(numColors);
% Assume min value is cmap(1, :) and max value is cmap(end, )
% Get color sorted in the same way as colorpost (or you could make a dictionary)
[sortedColorpost, sortOrder] = sort(colorpost, 'ascend');
assignedColors = cmap(sortOrder, :);
Is it what you want?
  2 件のコメント
bsriv
bsriv 2022 年 11 月 15 日
perfect thank you!
Walter Roberson
Walter Roberson 2022 年 11 月 15 日
This assigns one color per element, even if two elements are close or even identical. The color is not proportional to the value.
The solution I gave scales entries according to relative value through the span of values, and so is suitable for color representing relative value.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeColormaps についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by