2014-10-20 118 views
1

我得到了2个包含数字的文件。一个数字用于计算骨骼的最大压缩量,而另一个数字则是骨骼上的压缩量。如果第二个数字高于最大压缩率,我想用红色绘制点,如果它小于那么它应该是绿色。
我的问题是,即使大部分的应该是绿色的,我所有的点都会变成红色。我试图通过打印出H和C矢量来调试它,并且有100个数字应该是红色的,其余的是绿色的。任何帮助或暗示赞赏。比较Matlab中的元素和绘图

这是我的代码

p=VarName5; 
c=VarName7*2.5; %%The compression that is on the bone 
if p<0.317; 
    H=10500*p.^1.88; %%Calculate max compression the bone handles 
else 
    H=114*p.^1.72; %%Calculate max compression the bone handles 
end 
if(c < H) %% if the compression on the bone is smaller then max compression 
    plot(p,c,'+G') %% plot using green+ 
    hold on 
else 
    plot(p,c,'+R') %if the compression is higher than max compression use red+ 
end 
hold off 

回答

2

您可以创建一个逻辑向量,其中所有元素大于最高值是1,而其他都是0:

ind = c > H; 
plot(p(ind),c(ind),'+R') 
hold on 
plot(p(~ind),c(~ind),'+G') 

然后您就可以绘制出来分别。

要与一些随机数据说明:

c = repmat([1:6 7:-1:1],1,2); %// The compression 
H = rand(1,numel(c))*8; %// The compression the bone handles (in this case: random) 
p = 1:numel(c); 

ind = c > H; %// Index of elements where the bone is compressed more than it handles. 
plot(p(ind),c(ind),'+R') 
hold on 
plot(p(~ind),c(~ind),'+G') 

enter image description here

我把它留给你找出如何落实到你的代码。

+0

完美的,正是我所期待的。不知道我可以使用这样的情节。 – 2014-10-20 15:36:52