set colormap values to an array

33 vues (au cours des 30 derniers jours)
bsriv
bsriv le 15 Nov 2022
Commenté : bsriv le 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

Réponse acceptée

Walter Roberson
Walter Roberson le 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)
  1 commentaire
bsriv
bsriv le 16 Nov 2022
Thank you!

Connectez-vous pour commenter.

Plus de réponses (1)

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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Colormaps dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by