Indexing matrix with multiplication
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
user20912
el 27 de Nov. de 2023
Comentada: user20912
el 27 de Nov. de 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 comentarios
Respuesta aceptada
Cris LaPierre
el 27 de Nov. de 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 comentarios
Walter Roberson
el 27 de Nov. de 2023
T = randi(50,5)
mask = T>20
out1 = T(mask)
out2 = T.*mask
out3 = nonzeros(out2)
isequal(out1, out3)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!