Indexing matrix with multiplication
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
user20912
il 27 Nov 2023
Commentato: user20912
il 27 Nov 2023
Hi,
it is the same to apply a mask as an index or multiplying?
Say, I've a 2D matrix T. When I need the values that obey some condition I normally use an index as:
mask = T > 20;
T(mask)
Recently, I came across the problem that I need to keep the dimensions of the original matrix when I apply the index. Hence my question, is the previous code example the same as the following?
mask = T > 20;
T.*mask
Thanks
0 Commenti
Risposta accettata
Cris LaPierre
il 27 Nov 2023
It's not exactly the same. To keep the dimensions, the second option places a zero in each location that does not meet the mask criteria.
T = randi(50,5)
mask = T>20
T(mask)
T.*mask
3 Commenti
Walter Roberson
il 27 Nov 2023
T = randi(50,5)
mask = T>20
out1 = T(mask)
out2 = T.*mask
out3 = nonzeros(out2)
isequal(out1, out3)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!