Enumerate array of zeros and ones

조회 수: 1 (최근 30일)
Erick Carreño
Erick Carreño 2018년 4월 18일
답변: Walter Roberson 2018년 4월 18일
hi, i have an array [0000100111011111110000001111110000011100] and i need obtein all 1s that are before a 0 (including the first 0 after 1) belong to a set... then until a new 1 appears it will belong to another set (until a 0 appears) [0000100111011111110000001111110000011100]-> [----11-222233333333-----4444444----55550] any advice?

답변 (1개)

Walter Roberson
Walter Roberson 2018년 4월 18일
A = '0000100111011111110000001111110000011100';
starts = strfind(['0' A], '01');
stops = strfind(A, '10');

starts is now the index of all the beginnings of runs of 1's, and stop is now the index of all of the ends of runs of 1's. The length of each run is (stops-starts+1)

You can do the same thing with numeric arrays. If A was [0 0 0 0 1 0 0 1 etc] then you could use

starts = strfind([0 A], [0 1]);
stops = strfind(A, [1 0]);

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by