How to extract the events which have more than 3 consecutive rows
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Sewwandhi Chadrasekara
le 25 Mai 2020
Commenté : Sewwandhi Chadrasekara
le 26 Mai 2020
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
0 commentaires
Réponse acceptée
Walter Roberson
le 26 Mai 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;
Plus de réponses (1)
KSSV
le 25 Mai 2020
Modifié(e) : KSSV
le 25 Mai 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
Reference: https://in.mathworks.com/matlabcentral/answers/104614-grouping-continuous-nonzero-in-a-row-vector
Voir également
Catégories
En savoir plus sur Characters and Strings 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!