How to Count changes in a vector-Challenging problem, help appreciated
조회 수: 2 (최근 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
댓글 수: 0
채택된 답변
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.
댓글 수: 0
추가 답변 (1개)
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!