How to get row indices of matching pattern

조회 수: 2 (최근 30일)
Mekala balaji
Mekala balaji 2018년 11월 10일
편집: per isakson 2018년 11월 10일

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.

답변 (1개)

per isakson
per isakson 2018년 11월 10일
편집: per isakson 2018년 11월 10일
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.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by