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

1 次查看(过去 30 天)
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 个评论
Star Strider
Star Strider 2017-2-5
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.

请先登录,再进行评论。

采纳的回答

Sebastian
Sebastian 2017-2-5
编辑:Sebastian 2017-2-5
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by