Remake this without a loop
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
How to rewrite this without using a loop?
Basically I want to replace elements in matrix A with 0, that are equal to any of element in vector "vector"
vector = [A(A >= 1.5)].' % Vector, don't change.
for i = 1:vector
A(A == vector(i)) = 0;
end
0 commentaires
Réponse acceptée
the cyclist
le 17 Oct 2021
% Pretend data
A = 1 : 7;
vector = [2 3 5];
% Replace values of A with 0, if they are in vector
A(ismember(A,vector)) = 0
0 commentaires
Plus de réponses (1)
DGM
le 17 Oct 2021
Modifié(e) : DGM
le 17 Oct 2021
What's wrong with just using the test you're already doing? In the case you've given, the vector is unnecessary.
A(A >= 1.5) = 0;
Or more generally, if you actually need to test against another set,
A = randi(5,5,8)
v = [3 4];
A(ismember(A,v)) = 0;
0 commentaires
Voir également
Catégories
En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!