How to Count changes in a vector-Challenging problem, help appreciated

2 ビュー (過去 30 日間)
dantheman413
dantheman413 2012 年 2 月 26 日
Hello,
I have a vector, v of random numbers between 0 and 10. I need to be able to count how many times the values of the vector passes over 5. For example if I had a vector of random variables as such:
v=(1,2,2,3,7,4,7,3)
The answer I'm looking for above is 4.
(3 to 7 passes over 5, and 7 to 4 passes over 5, 4 to 7 passes over 5, and 7 to 3 passes over 5)- A total of 4 relative to the value 5.
How could i come up with code for this? Thanks
Dan

採用された回答

Image Analyst
Image Analyst 2012 年 2 月 26 日
Maybe this:
v=[1,2,2,3,7,4,7,3]
vt = v>5
d=diff(vt)
s = sum(abs(d))
Of course you could compact that all into a single line if you wanted to.

その他の回答 (1 件)

yang
yang 2012 年 2 月 28 日
V1=V-5;%small than 5 become small then 0 ,big 5 to big than 0 num=0,%count number D=zeros(6); for k=1:6 D(k)=V1(k+1)*V1(k) if D(k)<0 num=num+1;%while V passes 0,count number add 1 end end k

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by