How to get row indices of matching pattern

2 vues (au cours des 30 derniers jours)
Mekala balaji
Mekala balaji le 10 Nov 2018
Modifié(e) : per isakson le 10 Nov 2018

Hi,

I have cell array matrix as below:

Standard      Manual  179  
Standard      Auto    1
Nonstandard   Auto    86  
Nonstandard   Auto    86     
Standard      Manual  179  
Standard      Auto    1 
Standard      Manual  250
Standard      Manual  179  
Standard      Auto    1 
Nonstandard   Manual  179
Standard      Manual  179  
Standard      Auto    1 

I want get the row indices where the it match the given pattern. The given pattern is:

Standard      Manual  250
Standard      Manual  179  
Standard      Auto    1 

The first column should be Standard & second column should be Manual. This pattern should appear twice continuously, next immediate row should be (column 1 & 2) Standard & Auto.

The row indices I desired is the starting row index and end row index. In this example it is 7 & 8.

Réponses (1)

per isakson
per isakson le 10 Nov 2018
Modifié(e) : per isakson le 10 Nov 2018
Try
%%I expanded the example with a couple of edge cases
cac = { 'Standard', 'Manual', 250
'Standard', 'Manual', 179
'Standard', 'Auto', 1
'Standard', 'Auto', 1
'Nonstandard', 'Auto', 86
'Nonstandard', 'Auto', 86
'Standard', 'Manual', 179
'Standard', 'Auto', 1
'Standard', 'Manual', 250
'Standard', 'Manual', 179
'Standard', 'Auto', 1
'Nonstandard', 'Manual', 179
'Standard', 'Manual', 179
'Standard', 'Auto', 1
'Standard', 'Manual', 250
'Standard', 'Manual', 179
'Standard', 'Auto', 1
'Standard', 'Manual', 250
'Standard', 'Manual', 179
'Standard', 'Auto', 1
};
pattern = { 'Standard', 'Manual', 250
'Standard', 'Manual', 179
'Standard', 'Auto', 1
};
ixStart = [];
jj = 1;
while jj+2 <= size(cac,1)
if all(all( cellfun( @strcmp, cac(jj:jj+2,1:2), pattern(:,1:2) ) ))
ixStart(end+1) = jj; %#ok<SAGROW>
jj = jj+3;
else
jj = jj+1;
end
end
% ixEnd = ixStart+2;
"In this example it is 7 & 8." I assume you mean 7 & 9.

Catégories

En savoir plus sur Matrix Indexing 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!

Translated by