Compare and replace elements in a matrix

I have a matrix say A=[1 2 3 4 5 6 7 8 9 10] B=[2 5 10]. I want to compare A and B so that 2,5 and 10 (in B) should be replaced with zeros in A. The result should be [1 0 3 4 0 6 7 8 9 0].

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 12월 26일
편집: Azzi Abdelmalek 2013년 12월 26일

1 개 추천

A=[1 2 3 4 5 6 7 8 9 10];
B=[2 5 10];
for k=1:numel(B)
A(ismember(A,B(k))=0;
end
or
A(ismember(A,B))=0
or
A(any(bsxfun(@eq,A,B')))=0

추가 답변 (1개)

Walter Roberson
Walter Roberson 2013년 12월 26일

0 개 추천

Have a look at ismember()

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

질문:

2013년 12월 26일

편집:

2013년 12월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by