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

2 visualizzazioni (ultimi 30 giorni)
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)

Risposta accettata

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

Più risposte (2)

Aquatris
Aquatris il 20 Lug 2018
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 Commenti
Aquatris
Aquatris il 20 Lug 2018
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 il 21 Lug 2018
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.

Accedi per commentare.


Image Analyst
Image Analyst il 21 Lug 2018
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.

Categorie

Scopri di più su Color and Styling in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by