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

조회 수: 8 (최근 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