To Count the reversal of a series of numbers in a column or row.

조회 수: 5 (최근 30일)
aneps
aneps 2017년 12월 14일
댓글: Jos (10584) 2017년 12월 16일
I have a data with the first column as follows" A = [1 1 1 1 2 2 2 2 3 3 3 4 4 3 3 2 2 2 3 3 3 3 4 4 4 5 5 5 5 6 6 6 6 6 7 7 7 7 8 8 8 7 7 6 6 7 7 7 8 8 8 9 9 9 9 ]; I want to count how many times the number roll back, means, form the beginning the number keep on increasing and after 4 it reversed again after 2 it start increasing. This thing happen after 7 also. So how can I make a code to count how many times it reversed? For example, in this case , the data A reversed two times. In my original data, I have almost 1000 or even more data with several reversals.

채택된 답변

Jos (10584)
Jos (10584) 2017년 12월 14일
dA = diff(A) ;
Nreversals = sum(diff(dA(dA~=0))<0)
  댓글 수: 2
aneps
aneps 2017년 12월 15일
It was looking good to me but when I applied to the real data it is giving wrong result! For eg. in the attached data, the col 2 or col 4 both reverse only once. So, the result of Nreversals should be 1. Instead the code is giving result 6 if I use A(:,2) and 35 if I use A(:,4).
Jos (10584)
Jos (10584) 2017년 12월 16일
Ok! Corrected code:
dA = sign(diff(A)) ;
Nreversals = sum(diff(dA(dA~=0))<0)
Note there are 15 small reversals in column 4 ...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by