How can I display elements of a 2D matrix as blue, green and red squares?

7 次查看(过去 30 天)
Hi guys!
I have a problem when I try to use a colormap. I have a vector A and a vector B, for each combination of elements from A and B, my matrix C gets a number that can be 0, 5 or 10. So, for example, for A(2) and B(2), C(2,2) is 0. For A(3), B(5), C(3,5) is 10. I'm using mesh to plot my matrix C, then, I change to view(2) and use that new view. Also, I use colormap(jet) so the elements 0 are represented by a blue square. The elements 5 by green and the elements 10 by red. My problem is that when my matrix C has for example only 0 and 5, then I get only blue and red squares. I want to get blue and green in this case. The same happens when my C has only 5 and 10. How can I correct that?
mesh(A,B,C)
map = colormap(jet);
map(1,1) = 0;
map(1,3) = 1;
map(64,1) = 1;
map(64,3) = 0;
colormap(map);
view(2)

采纳的回答

Kelly Kearney
Kelly Kearney 2018-7-20
You just need to set the color limits:
set(gca, 'clim', [0 10]);

更多回答(2 个)

Aquatris
Aquatris 2018-7-20
A simple fix would be to add 3 additional variables to your matrices that have X and Y values that are outside of your region of interest and Z values that are 0, 5, and 10. This might solve your issue by forcing the color assignment to be the same since Z will always have all three variables.
  2 个评论
Aquatris
Aquatris 2018-7-20
Here is an example to play with;
x = 1:4;
y = 1:4;
x = [x 10]; % x = 10 is the dummy
[A,B] = meshgrid(x,y)
C = zeros(4,4);
C(1:2,3:4) = 5;
C(:,5) = [0 5 10 0]'; % dummy C to let C have all 3 variables
mesh(A,B,C)
map = colormap(jet);
map(1,1) = 0;
map(1,3) = 1;
map(64,1) = 1;
map(64,3) = 0;
colormap(map);
view(2)
axis([1 4 1 4]) % region of interest
Felipe Higa
Felipe Higa 2018-7-21
I was using these additional variables before coming here to ask, but I thought there was another way to do that. Anyway, thanks for your answer.

请先登录,再进行评论。


Image Analyst
Image Analyst 2018-7-21
You might like to take a look at the heatmap() function or im2html. Not really a solution but kind of related and might be interesting.

类别

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