Using a binary matrix as an index to select values from another matrix
Mostrar comentarios más antiguos
Hi all,
I think this is a really beginner question, but I can't seem to find the relevant answers online.
If I have the following matrix:
% 3x2
index_matrix = [0,0;1,0;1,1;]
% 3x2
p = [13,12;11,19;22,23;]
I wish to select the corresponding value in p if the value at the same position in index_matrix = 1.
If the value in index_matrix is 0, then the corresponding value should be set to 0.
That is, I wish to have the following:
result = [0,0;11,0;22,23;]
Thank you in advance!
Respuesta aceptada
Más respuestas (1)
Stephen23
el 14 de Jun. de 2019
>> X = [1,0;3.5,1;0,6];
>> p = [13,12;11,19;22,23];
>> p.*(X==1)
ans =
13 0
0 19
0 0
1 comentario
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!