【问题标题】:How do I define colors in imagesc based on the values of the data如何根据数据的值在 imagesc 中定义颜色
【发布时间】:2014-10-11 12:22:53
【问题描述】:

我正在尝试根据我的数据值更改 imagesc 图上的色标。

我的数据是一个向量,取值范围是 0-50。

我已经像这样设置了我的颜色图:

color = [0  0   0
         linspace(.7,1,6)' linspace(.5,1,6)' linspace(0,0,6)'];

我还设置了一个向量,为数据中的每个值分配不同的索引。

for i=1:length(mydata)

  if (mydata(i)==0)
      index(i)=1;
  elseif(mydata(i)==1)
      index(i)=2;
  elseif(mydata(i)==2)
      index(i)=2;    
  elseif(mydata(i)==3)
      index(i)=3;
  elseif(mydata(i)==4)
      index(i)=3;
  elseif(mydata(i)>5 & mydata(i)<10)
      index(i)=4;
  elseif(mydata(i)>10 & mydata(i)<15)
      index(i)=5;
  elseif(mydata(i)>15 & mydata(i)<20)
      index(i)=6;
  else
      index(i)=7;

  end
end

但是,我现在如何将它传递给 imagesc 函数?

关于如何解决这个问题的任何其他建议? 非常感谢!

【问题讨论】:

    标签: matlab colors matlab-figure


    【解决方案1】:

    首先,您需要在循环中将 length(mydata) 更改为 numel(mydata)。否则,您不会访问mydata 的所有元素。或者更好的是,将其矢量化(没有循环):

    index = NaN(size(mydata)); %// preallocate
    index(mydata==0) = 1;
    index(mydata==1) = 2;
    %// etc.
    

    那就用

    imagesc(index); %// display image
    colormap(color); %// set your matrix "color" as the colormap
    colorbar %// show color bar, if desired
    

    【讨论】:

    • imagesc(索引)!是的!我自己才意识到!非常感谢!!
    猜你喜欢
    • 2022-08-03
    • 2014-12-24
    • 1970-01-01
    • 2022-08-17
    • 2020-04-04
    • 2021-04-30
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多