根据z值将颜色设置为z值

时间:2017-11-12 20:47:34

标签: matlab matlab-figure

我有一个我想要着色的表面。 z值取自100x100矩阵,范围从-10到2,我想要的东西

If z is between -10 and -9, color [1 0 0]
If z is between -9 and -8, color [.9 0 0]
....
If z is between -1 and 0, color [.1 0 0]
If z is between 0 and 1, color [0 0 .5]
If z is between 1 and 2, color [0 0 1]

现在,我想在不同的曲面图上使用相同的精确颜色贴图。同样,这些新的z值取自100x100矩阵,但其值仅在-5到1之间。所以,我想要

If z is between -5 and -4, color [.5 0 0]
If z is between -4 and -3, color [.4 0 0]
....
If z is between -1 and 0, color [.1 0 0]
If z is between 0 and 1, color [0 0 .5]

有没有办法实现这一点,而不是每当我有一个我想要着色的新表面时手动定义一个新的颜色图?

1 个答案:

答案 0 :(得分:1)

由于我无法理解您的色彩图的基础模式(并因此将其转换为根据当前处理的数据范围自动计算当前颜色的for循环),我建议:

z_1 = randi([-10 2],100);
cmap_1 = cell(size(z_1));
cmap_1(z_1 < -9) = {[1.0 0.0 0.0]};
cmap_1(z_1 >= -9 & z_1 < -8) = {[0.9 0.0 0.0]};
% ...
cmap_1(z_1 >= -1 & z_1 < 0) = {[0.1 0.0 0.0]};
cmap_1(z_1 >= 0 & z_1 < 1) = {[0.0 0.0 0.5]};
cmap_1(z_1 >= 2) = {[0.0 0.0 1.0]};

z_2 = randi([-5 1],100);
cmap_2 = cell(size(z_2));
cmap_2(z_2 < -4) = {[0.5 0.0 0.0]};
cmap_2(z_2 >= -4 & z_2 < -3) = {[0.4 0.0 0.0]};
% ...
cmap_2(z_2 >= -1 & z_2 < 0) = {[0.1 0.0 0.0]};
cmap_2(z_2 >= 0) = {[0.0 0.0 0.5]};

要检索值及其各自的颜色:

my_z = z_1(2,19);
my_col = cmap_1{2,10};