Indexing matrix with multiplication
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Cris LaPierre
2023-11-27
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 个评论
Walter Roberson
2023-11-27
T = randi(50,5)
mask = T>20
out1 = T(mask)
out2 = T.*mask
out3 = nonzeros(out2)
isequal(out1, out3)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!