Index of the first particular value in a vector

1 visualización (últimos 30 días)
Garrett Duckworth
Garrett Duckworth el 16 de Mzo. de 2018
Respondida: Walter Roberson el 16 de Mzo. de 2018
How should I go about finding the index for the first value in a given vector for a given value. For an example, if I have some vector x = [9, 14, 13, 14], and I want to find the index for the first value in it that is 14, in this case 2, to replace it with a different value, say 1, while letting the subsequent values remain unchanged, to get a final answer of x = [9, 1, 13, 14].
I have thought about simplifying the problem by changing it to a Boolean version, c = x == 14, so c becomes [0, 1, 0, 1] however I still the issue of finding only the first value to change.
I know I can probably just run the whole thing through a loop through each subsequent entry in x until I find the first value but surely there is a more elegant and compact way.

Respuestas (1)

Walter Roberson
Walter Roberson el 16 de Mzo. de 2018
find(x == 14, 1)

Categorías

Más información sobre Matrix Indexing 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