Indexing matrix with multiplication
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Cris LaPierre
on 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 Comments
Walter Roberson
on 27 Nov 2023
T = randi(50,5)
mask = T>20
out1 = T(mask)
out2 = T.*mask
out3 = nonzeros(out2)
isequal(out1, out3)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!