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!