Filter löschen
Filter löschen

set colormap values to an array

30 Ansichten (letzte 30 Tage)
bsriv
bsriv am 15 Nov. 2022
Kommentiert: bsriv am 16 Nov. 2022
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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 15 Nov. 2022
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)

Weitere Antworten (1)

Image Analyst
Image Analyst am 15 Nov. 2022
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 Kommentare
bsriv
bsriv am 15 Nov. 2022
perfect thank you!
Walter Roberson
Walter Roberson am 15 Nov. 2022
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Colormaps finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by