How to detect row index

1 visualización (últimos 30 días)
Mekala balaji
Mekala balaji el 13 de Abr. de 2018
Comentada: Rik el 14 de Abr. de 2018
Hi, I have the cell array of column as below:
VA
Star
/
:
Status
Star
/
end
:
positive
Status
1
4
12
det
STDEV
/
end
:
finish
Star
/
end
:
positive
Status
:
KLA
STU
end
status
Now, I want to detect the row index of "Star" when below strings appears in consecutive rows.
Star
/
end
:
positive
Status
In this case, 7 (6th row white space)

Respuesta aceptada

Rik
Rik el 14 de Abr. de 2018
The code below should do the trick:
a={'VA';'Star';'/';':';'Status';'';'Star';'/';'end';':';'positive';...
'Status';'';'1';'4';'12';'det';'STDEV';'/';'end';':';'finish';'';...
'Star';'/';'end';':';'positive';'Status';':';'KLA';'STU';'end';'status'};
b={'Star';'/';'end';':';'positive';'Status'};
[~,index]=ismember(a(:),b(:));
starting_rows=strfind(index',1:length(b));
  2 comentarios
Mekala balaji
Mekala balaji el 14 de Abr. de 2018
How does this works:
starting_rows=strfind(index',1:length(b))
Rik
Rik el 14 de Abr. de 2018
The trick is that ismember finds the positions where elements of b can be found in a. This means that you are looking for the positions in index where a subvector [1 2 3 4 5 6] starts. You can easily do this by pretending the vectors are strings and using strfind. You don't even need a conversion to char (in Octave you might). The transpose is there to convert the column vector to a row vector.
This method requires the elements of b to be unique.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by