How can compute sequential occurrence of elements in a vector

조회 수: 6 (최근 30일)
hanadi abbas
hanadi abbas 2021년 3월 7일
편집: hanadi abbas 2021년 3월 12일
I would like to compute the number of each sequential sequence in a vector
for example i have x=[ 0 0 0 0 1 1 1 1 1 0 0 0 2 2 2 2 0 0 0 0 0 3 3 3 3 3 3]
So, I need to compute the lengths of the following sequences
[0 0 0 0 1 1 1 1 1]=9 ,
[0 0 0 2 2 2 2]=7,
[0 0 0 0 0 3 3 3 3 3 3]=11.
The expected result is [9 7 11].

채택된 답변

Jan
Jan 2021년 3월 7일
편집: Jan 2021년 3월 7일
x = [ 0 0 0 0 1 1 1 1 1 0 0 0 2 2 2 2 0 0 0 0 0 3 3 3 3 3 3];
change = [true, diff(x) < 0, true];
index = diff(find(change))

추가 답변 (1개)

hanadi abbas
hanadi abbas 2021년 3월 7일
편집: hanadi abbas 2021년 3월 12일
Thanks . That's work!

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by