Compare and replace elements in a matrix
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
aneps
am 26 Dez. 2013
Bearbeitet: Azzi Abdelmalek
am 26 Dez. 2013
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].
0 Kommentare
Akzeptierte Antwort
Azzi Abdelmalek
am 26 Dez. 2013
Bearbeitet: Azzi Abdelmalek
am 26 Dez. 2013
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
0 Kommentare
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!