2014-10-20 71 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