Find the values before an element repeated more than 4 times.

조회 수: 1 (최근 30일)
Serra Aksoy
Serra Aksoy 2020년 1월 27일
댓글: Adam Danz 2020년 1월 27일
Hi everyone,
I have an array like this: A=[ 1 2 3 4 5 0 0 6 7 0 9 0 0 0 10 11 12 0 0 0 0 0 0 0 8 9 10 11 0 0 0 0 0 ]
I would like to find the values which are the ones before 0 repeated more than 4 times.
So here the answer should be 12 and 11.
Any help is appreciated.

채택된 답변

Adam Danz
Adam Danz 2020년 1월 27일
편집: Adam Danz 2020년 1월 27일
Input: A, numeric row vector
Output: valueBefore, a numeric vector of values prior to 4 or more consecutive 0s.
A=[ 1 2 3 4 5 0 0 6 7 0 9 0 0 0 10 11 12 0 0 0 0 0 0 0 8 9 10 11 0 0 0 0 0 ];
dA = diff([inf,A,inf]==0);
zeroCount = find(dA==-1) - find(dA==1);
zeroStart = find(dA==1);
valueBefore = A(zeroStart(zeroCount>=4)-1);
Note, if the A vector starts with 4+ zeros, there will be an error since there are no values prior to the first element.
  댓글 수: 2
Serra Aksoy
Serra Aksoy 2020년 1월 27일
Thank you very much for your quick help. It is definitely what I'm looking for.
I also would like to do this to a mx1 matrix. How can i use this code for it ?
Adam Danz
Adam Danz 2020년 1월 27일
The only requirements for the input is that it's a row vector. If your vector is mx1, you just need to transpose it.
data = data.';

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by