set colormap values to an array

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

 Risposta accettata

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)

Più risposte (1)

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 Commenti

bsriv
bsriv il 15 Nov 2022
perfect thank you!
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.

Accedi per commentare.

Categorie

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by