If condition for each matrix elements
2 ビュー (過去 30 日間)
古いコメントを表示
Hi, I have a question about if function for matrix elements.
My code is here:
d1 = 1.5 + 0.3.*randn(100,1); %ball-bearing diameter
d2 = 1.2 + 0.5.*randn(100,1); %shaft diameter
c=abs(d1-d2);
if (0.2<c<0.5)
p(i)=1
else
p(i)=0
end;
mean(p)
Basically I want to compair each element in matrix c with 0.2 and 0.5. if the data falls in (0.2,0.5) value, accept it as 1. otherwise reject as 0.
MATLAB finds an error "Subscript indices must either be real positive integers or logicals." which I personally don't think is relevant to my problem. I would like to know how to compare each value within matrix c with 0.2 and 0.5.
I tried to put c as c(i), but MATLAB still finds an error.
Can somebody help me out here? I always seem to have trouble with matrix form.
Thanks a lot!
Ying
0 件のコメント
採用された回答
Shiva Arun Kumar
2012 年 2 月 7 日
There may be many ways to do this but does this solution work for you?
p=zeros(size(c));
p(intersect(find(c>.2)',find(c<.5)'))=1
2 件のコメント
Walter Roberson
2012 年 2 月 7 日
0.2<c<0.5 means ((0.2<c)<0.5) which compares the logical true/false result of 0.2<c to the value 0.5
Also, you are testing the entire vector at one time instead of testing one element at a time in a "for" loop.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!