How to Count changes in a vector-Challenging problem, help appreciated
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Image Analyst
on 26 Feb 2012
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.
0 Comments
More Answers (1)
yang
on 28 Feb 2012
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
0 Comments
See Also
Categories
Find more on Random Number Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!