How to get row indices of matching pattern
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
per isakson
el 10 de Nov. de 2018
Editada: per isakson
el 10 de Nov. de 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.
0 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!