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

1 vue (au cours des 30 derniers jours)
dantheman413
dantheman413 le 26 Fév 2012
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

Réponse acceptée

Image Analyst
Image Analyst le 26 Fév 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.

Plus de réponses (1)

yang
yang le 28 Fév 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

Catégories

En savoir plus sur Random Number Generation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by