Borrar filtros
Borrar filtros

How to replace a certain value for another one once in a vector without using the index?

1 visualización (últimos 30 días)
vec = [89.7000 9.1000 68.0000 87.7000 91.7000 9.1000]
rep = 50
I have to replace one of the 9.1000s in vec for rep without using indices.
  1 comentario
Star Strider
Star Strider el 5 de Feb. de 2017
I have to replace one of the 9.1000s in vec for rep without using indices.
That does not seem possible to me. I cannot imagine a way to address the elements of a vector without using some sort of indexing.

Iniciar sesión para comentar.

Respuesta aceptada

Sebastian
Sebastian el 5 de Feb. de 2017
Editada: Sebastian el 5 de Feb. de 2017
Use this to replace all elements with the value of 9.1 with rep:
vec(vec == 9.1) = rep
vec =
89.7000 50.0000 68.0000 87.7000 91.7000 50.0000
Alternatively use this to find the indices of the elements and then replace them:
ind = find(vec == 9.1)
ind =
2 6
vec(ind) = rep
vec =
89.7000 50.0000 68.0000 87.7000 91.7000 50.0000
If you only want to replace the first element, use this:
vec(find(vec == 9.1, 1)) = rep
vec =
89.7000 50.0000 68.0000 87.7000 91.7000 9.1000
  2 comentarios
Sebastian
Sebastian el 5 de Feb. de 2017
Editada: Sebastian el 5 de Feb. de 2017
I know but I think there is no way around find if you want to replace only certain elements and not all of them.

Iniciar sesión para comentar.

Más respuestas (0)

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