如何仅以红色显示矩阵的负值?

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

我在 matlab 上工作,我有一个问题:

我有一个矩阵:

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

然后我减去这个矩阵 12000:

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

我想将负值更改为红色,同时保留其他值。

所以结果是:

这是我试过的:

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);

我有红色的负值,但我不知道如何保留其他值而不更改它们为黑色。

谢谢

解决方案:扭曲颜色图以适合您的数据。

如果您将 M2 定义为:

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

现在负值和正值的范围相同(它们从 0 到相同的 abs() 值)。

使用这个,我们可以扭曲我们的颜色图。所有正数都将是 gray,所有负数都是红色。

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

和情节:

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