How to get row indices of matching pattern
조회 수: 2 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (1개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!