Clear Filters
Clear Filters

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

1 view (last 30 days)
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 Comment
Star Strider
Star Strider on 5 Feb 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.

Sign in to comment.

Accepted Answer

Sebastian
Sebastian on 5 Feb 2017
Edited: Sebastian on 5 Feb 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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!