How to extract lengths of sequences from a binary vector?

1 vue (au cours des 30 derniers jours)
Mr M.
Mr M. le 27 Fév 2018
Modifié(e) : Stephen23 le 27 Fév 2018
For example, I have x = [0 0 1 1 1 0 1 0 0 0 0 1 1 0]; and I need 2,3,1,1,4,2,1. Are there any simple way to get this?

Réponses (2)

Matt J
Matt J le 27 Fév 2018
Modifié(e) : Matt J le 27 Fév 2018
If you have the Image Processing Toolbox
S=regionprops(boolean(x),'Area')
sequenceLengths=[S.Area],

Stephen23
Stephen23 le 27 Fév 2018
Modifié(e) : Stephen23 le 27 Fév 2018
>> x = [0 0 1 1 1 0 1 0 0 0 0 1 1 0];
>> idx = find([1,diff(x),1]);
>> idx(2:end)-idx(1:end-1)
ans =
2 3 1 1 4 2 1

Catégories

En savoir plus sur Modeling dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by