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

1 visualización (últimos 30 días)
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

Respuesta aceptada

Walter Roberson
Walter Roberson el 26 de Mayo de 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;

Más respuestas (1)

KSSV
KSSV el 25 de Mayo de 2020
Editada: KSSV el 25 de Mayo de 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

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by