How to find elements of first matrix based on second matrix?

1 visualización (últimos 30 días)
Ammy
Ammy el 23 de Feb. de 2022
Comentada: Stephen23 el 23 de Feb. de 2022
I have two matrices A and B
A = [1 2 3 4 5 6 7 8 9 10];
B = [1 0 0 1 0 1 0 1 1 1];
How to find entries of A coresponsing to '1' in B
result = [1 4 6 8 9 10];
  1 comentario
Stephen23
Stephen23 el 23 de Feb. de 2022
Note that FIND is not required, logical indexing is simpler and more efficient:
A = [1 2 3 4 5 6 7 8 9 10];
B = [1 0 0 1 0 1 0 1 1 1];
C = A(B==1)
C = 1×6
1 4 6 8 9 10

Iniciar sesión para comentar.

Respuestas (1)

Arif Hoq
Arif Hoq el 23 de Feb. de 2022
use find function
A = [1 2 3 4 5 6 7 8 9 10];
B = [1 0 0 1 0 1 0 1 1 1];
result=A(find(B==1))
result = 1×6
1 4 6 8 9 10
  2 comentarios
Stephen23
Stephen23 el 23 de Feb. de 2022
Editada: Stephen23 el 23 de Feb. de 2022
FIND is not required, logical indexing is simpler and more efficient:
A = [1 2 3 4 5 6 7 8 9 10];
B = [1 0 0 1 0 1 0 1 1 1];
C = A(B==1)
C = 1×6
1 4 6 8 9 10

Iniciar sesión para comentar.

Categorías

Más información sobre Matrices and Arrays 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!

Translated by