Turning a matrix in to a multi color graph

10 次查看(过去 30 天)
I am tring to turn this matrix into a plot that has multiple colors. The color code is this
%whtie = 1;
%orange = 2;
%yellow = 3;
%red = 4;
%blue = 5;
%green = 6;
%error (black) = 9999;
%nothing (no color) = 0
This is the matrix I have, it is a 9x12
UnsolvedLayout =
Columns 1 through 32
0 0 0 1 3 4 0 0 0 0 0 0 6 4 6 5 1 5 2 1 3 1 2 3 0 0 0 3 4 5 0 0
0 0 0 6 2 5 0 0 0 0 0 0 4 1 3 5 1 6 2 5 5 4 2 4 0 0 0 2 6 6 0 0
0 0 0 5 1 4 0 0 0 0 0 0 6 6 2 4 4 3 3 2 2 1 3 5 0 0 0 1 3 6 0 0
Columns 33 through 36
0 0 0 0
0 0 0 0
0 0 0 0
Please help me figure out how to do this, my initial thought was using meshgrid

回答(1 个)

Arjun
Arjun 2025-2-5,7:36
编辑:Arjun 2025-2-5,7:46
I see that you want to convert your matrix into a multiple colored plot with customized colors assigned to each value in the matrix.
This can be done by using a customized 'colormap' along with 'imagesc' function of MATLAB.
'colormap' helps you create a custom colormap by defining a three-column matrix of values between 0.0 and 1.0. Each row defines a three-element RGB triplet. The first column specifies the red intensities. The second column specifies the green intensities. The third column specifies the blue intensities.
'imagesc' helps you display image with scaled colors. By default, 'imagesc' scales the color limits so that image uses the full range of the colormap, where the smallest value in the matrix maps to the first color in the colormap and the largest value maps to the last color.
You can take care of the 9999 value additionally so that it does not impact the scaling of colors. Kindly refer to the suggestion below.
cmap = [
1, 1, 1; % white
1, 0.65, 0; % orange
1, 1, 0; % yellow
1, 0, 0; % red
0, 0, 1; % blue
0, 1, 0; % green
0, 0, 0; % black
1, 1, 1 % none (white)
];
% If this is your colormap then map all the 9999 values to value 7 they
% will have black color and map 0 values to either 1 or 8 since both are
% white other colors will be matched.
% e.g
data(data==9999) = 7;
data(data==0) = 1;
I hope this helps!

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by