How to display only negative values of a matrix in red?

Viewed 170

I work on matlab and I have a question:

I have a matrix:

M1 = [5000  5000  3000  4000
      11000 15000 13000 10000
      6000  8000  14000 9000]

And I subtract to this matrix 12000:

M2 = [7000  7000  9000  8000
      1000 -3000  -1000 2000
      6000  4000  -2000 3000]

I want to change the negative values to red while keeping the other values.

So the result would be: enter image description here

This is what i tried:

M1 = [5000 5000 3000 4000;
          11000 15000 13000 10000;
          6000 8000 14000 9000];


LmB = 12000;

M2 = LmB - M1;
M2(M2 < 0) = -1;
M2(M2 > 0) = 0;


figure;
imshow(M1, [], 'InitialMagnification','fit');

figure;
imshow(M2, [], 'InitialMagnification', 'fit');

cmap = [1 0 0; 0 0 0; 0 0 0];

% Apply colormap
colormap(cmap);

I have the negative values in red, but i don't know how to keep the other values and not change them in black.

Thanks

1 Answers

A solution: distort the colormap to fit your data.

If you define M2 as:

M2 = LmB - M1;
M2(M2 < 0) = -max(M2(:)); 

Now the range of negative values and positive values is the same (they go from 0 to the same abs() value).

Using this, we can distort our colormap. All positives will be gray, all negatives, simply red.

red=[1 0 0];
cmap= gray;
cmap=[repmat(red,size(cmap,1),1); gray]; %half red, half gray

And plot:

imshow(M2, [], 'InitialMagnification', 'fit');
% Apply colormap
colormap(cmap); 

enter image description here

Related