nsoures
nsoures

Reputation: 25

Set color of specific values with colormap

so a small version of what I am trying to accomplish is I have a matrix A;

A = [0 1 0; 2 0 0;1 3 6;9 0 1];
imagesc(A)

So when I use imagesc(A) I get a nice grid with each value represented by a different color. However I want to be able to set the value of 0 specifically to white and ideally be able to change the other colors as I see fit such as if I know two values represent the same thing like 3 and 6, then they could be set to the same or relatively similar colors. Is imagesc the wrong command to use because from what I can tell it uses a color gradient.

Thanks

Upvotes: 1

Views: 4394

Answers (2)

gregswiss
gregswiss

Reputation: 1456

Simple solution:

% get colormap and set first value to white
cmap = colormap;    
cmap(1,:) = [1 1 1];

% apply new colormap
colormap(cmap);

% display matrix 
imagesc(A);

Obviously you can change the colors for other values in the same way

Upvotes: 0

Dan
Dan

Reputation: 45752

2 options:

  1. you can create your own colormap as shown in How to create a custom colormap programmatically?
  2. or simply map your matrix A to a matrix that would be coloured as you want it. So if you know you want 3 and 6 to the same colour then create a mapping function that makes that so. You then use A to index the map so the 3rd and 6th element of map must be the same e.g.

    map = [1, 2, 3, 4, 5, 6, 4, 7, 8, 9, 10];
    imagesc(map(A+1))
    

    note that elements 4 and 7 in map are the same because your A values start from 0, this is also why there is the +1 in the second line.

    and then just choose a colormap that starts from white.

Personally I would go with method 1.

Upvotes: 1

Related Questions