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

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Random Number Generation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by