Effacer les filtres
Effacer les filtres

Setting an element of an array to zero based on the values of a vector

2 vues (au cours des 30 derniers jours)
L'O.G.
L'O.G. le 20 Avr 2022
Modifié(e) : Matt J le 20 Avr 2022
Given a 2D array with elements a(i,j), if either i or j matches any element in another vector, how do you set the value for that a(i,j) = 0?
So, for example, given A = magic(5), how do you set to 0 any element of A given a vector B = [1 2 5 8] if 1, 2, 5, or 8 is an entry in A?
Here, I want to obtain:
A =
17 24 0 0 15
23 0 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 0 9

Réponse acceptée

Matt J
Matt J le 20 Avr 2022
  4 commentaires
L'O.G.
L'O.G. le 20 Avr 2022
I suppose I didn't put it well. I meant in this case, A(1,1) = 17, but 17 is not in B and therefore A(1,1) should be left as it is.
Matt J
Matt J le 20 Avr 2022
Modifié(e) : Matt J le 20 Avr 2022
A = magic(5),
A = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
B = [1 2 5 8];
A( ismember(A,B) )=0
A = 5×5
17 24 0 0 15 23 0 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 0 9

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by