Replace values in matrix based on array values

2 views (last 30 days)
Philipp Heinirch
Philipp Heinirch on 4 Jun 2020
Edited: madhan ravi on 4 Jun 2020
Hey guys,
I got a question and feel really dumb for not figuring it out myself.
So lets say I have a random matrix and I want to set all elements that match a specific value given by an array to zero. How do I do it ?
I know that I can do it for single elements, but I have no idea how to do it for several elements at the same time (to avoid for loops).
Example code:

Answers (2)

madhan ravi
madhan ravi on 4 Jun 2020
Edited: madhan ravi on 4 Jun 2020
x1(ismember(x1,y1)) = 0

Rik on 4 Jun 2020
Nobody is born knowing about ismember and ismembertol. (use the latter if you expect float rounding errors)

Community Treasure Hunt

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

Start Hunting!

Translated by