Replace values in matrix based on array values

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:
x1=randi([0,10],[4,4])
y1=[3,4]
x1(x1==y1)=0;

답변 (2개)

madhan ravi
madhan ravi 2020년 6월 4일
편집: madhan ravi 2020년 6월 4일

0 개 추천

x1(ismember(x1,y1)) = 0
Rik
Rik 2020년 6월 4일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품

질문:

N/A
2020년 6월 4일

편집:

2020년 6월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by