How to extract lengths of sequences from a binary vector?

2 ビュー (過去 30 日間)
Mr M.
Mr M. 2018 年 2 月 27 日
編集済み: Stephen23 2018 年 2 月 27 日
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?

回答 (2 件)

Matt J
Matt J 2018 年 2 月 27 日
編集済み: Matt J 2018 年 2 月 27 日
If you have the Image Processing Toolbox
S=regionprops(boolean(x),'Area')
sequenceLengths=[S.Area],

Stephen23
Stephen23 2018 年 2 月 27 日
編集済み: Stephen23 2018 年 2 月 27 日
>> 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

カテゴリ

Help Center および File ExchangeModeling についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by