How to replace a certain value for another one once in a vector without using the index?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Eduardo Alfaro
el 5 de Feb. de 2017
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
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.
Respuesta aceptada
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
Más respuestas (0)
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!