How to extract the events which have more than 3 consecutive rows

1 view (last 30 days)
Hi,
I have array of data as follows;
idx3=0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 2 1 0 0 1 0 1 1 1 1 2 2 1 1 1 0 0 1 2 2 2 1 0 0 1 1 2 2 2 1 2 2 0 1 1 2 2 2 0 2
I want to find number of events which "2" comes for more or equal 3 consecutive rows. In my data set there are 3 events which show "2" in more than 3 (row # 83-85, 91-93 and 10-102).
How can i find it in matlab?
Thanks

Accepted Answer

Walter Roberson
Walter Roberson on 26 May 2020
r=3; how many repeats
mask = idx3.' == 2; %row
starts = strfind([false mask], [0 ones(1,r)];
stops = strfind([mask, false], [ones(1,r) 0]) + r-1;

More Answers (1)

KSSV
KSSV on 25 May 2020
Edited: KSSV on 25 May 2020
idx3 = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 2 1 0 0 1 0 1 1 1 1 2 2 1 1 1 0 0 1 2 2 2 1 0 0 1 1 2 2 2 1 2 2 0 1 1 2 2 2 0 2 ];
idx = idx3==2 ; % get logical indices of 2
% get the positions of 2
pos = zeros(size(idx)) ;
pos(idx)= find(idx) ;
% Split the positions having twos into cells
wrap = [0, pos, 0] ;
temp = diff( wrap ~= 0 ) ;
blockStart = find( temp == 1 ) + 1 ;
blockEnd = find( temp == -1 ) ;
blocks = arrayfun( @(bId) wrap(blockStart(bId):blockEnd(bId)), ...
1:numel(blockStart), 'UniformOutput', false ) ;
% Get positions which have two's
L = cellfun(@length,blocks) ; % get lengths of each cell
iwant = blocks(L==3) % pick cells of length 3

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!