Find Command after certain Value

1 visualización (últimos 30 días)
Dario Walter
Dario Walter el 3 de Mzo. de 2020
Respondida: Reshma Nerella el 13 de Mzo. de 2020
Dear Community Members,
I am looking for a function to return the rowindex of the following problem: Assuming a matrix A, I need the index of the first "2" in A that follows a "3". Hence, the required index in the following example is "8". The length of A might differ between different cases.
A = [1, 1, 2, 2, 3, 3, 4, 2, 2, 5, 6, 2, 2]'
Unfortunately, I do not know how to solve this problem in Matlab.

Respuesta aceptada

Reshma Nerella
Reshma Nerella el 13 de Mzo. de 2020
Hi,
The following code will return the required index .
flag = 0;
val = find(A == 3,1); % finding the index of first occurrence of 3
for i = val+1 : size(A,1)
if A(i)== 2 % checking if the element is 2
flag = 1;
break;
end
end
if flag
index = i; % first occurrence of ‘2’ after a ‘3’ occurred
end
If flag is 0 then the element is not present in the array.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by