Plot matrix values as colors in a checkerboard pattern

6 views (last 30 days)
Hi
I have the following matrix
C = [-1 -1 0.155;
0.150 -1 0.152;
0.140 0.143 0.148];
I would like to plot each value as a colored cell in a checkerboard pattern. The "-1" values are throwaway data, and should be marked by a red cell. The rest of the cells should have some color scale, so to be distinguishable from each other. Is this possible?
I tried using the suggestion by Cam in this answer, and it almost does what I want. However the negative values become purple, and the rest yellow. I'm guessing because the colorscale is applied to the range [-1,0.155], and all the actual data values are very close.
Regards
Søren

Accepted Answer

Tommy
Tommy on 4 May 2020
Why red? What if some real data is mapped to red?
You are right about the -1 values messing things up. But the 0s on the border (from the answer in your link) also mess things up.
You could replace all -1s with NaN and pad with NaNs instead of 0s:
C0 = [-1 -1 0.155;
0.150 -1 0.152;
0.140 0.143 0.148];
C = C0;
C(C == -1) = NaN;
C = [[C nan(size(C,1),1)] ; nan(1,size(C,2)+1)];
pcolor(C)
This leaves the -1 squares completely blank. One way to set them to red would be to color the underlying axes to red:
C0 = [-1 -1 0.155;
0.150 -1 0.152;
0.140 0.143 0.148];
C = C0;
C(C == -1) = NaN;
C = [[C nan(size(C,1),1)] ; nan(1,size(C,2)+1)];
ax = axes;
pcolor(ax, C)
ax.Color = 'r';
  1 Comment
Søren Holm-Petersen
Søren Holm-Petersen on 5 May 2020
Hi Tommy
Thanks for the answer. It was wrong to call the -1 values "throwaway data". I have an algorithm which I have evaluated at some (x,y) values. Whenever the algorithm reports failure to complete the task, it outputs -1.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!