How to get row indices of matching pattern
1 回表示 (過去 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!