How can compute sequential occurrence of elements in a vector

3 次查看(过去 30 天)
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 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by