Filter löschen
Filter löschen

How can compute sequential occurrence of elements in a vector

2 Ansichten (letzte 30 Tage)
hanadi abbas
hanadi abbas am 7 Mär. 2021
Bearbeitet: hanadi abbas am 12 Mär. 2021
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].

Akzeptierte Antwort

Jan
Jan am 7 Mär. 2021
Bearbeitet: Jan am 7 Mär. 2021
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))

Weitere Antworten (1)

hanadi abbas
hanadi abbas am 7 Mär. 2021
Bearbeitet: hanadi abbas am 12 Mär. 2021
Thanks . That's work!

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by