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

1 visualizzazione (ultimi 30 giorni)
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 Commento
Stephen23
Stephen23 il 23 Feb 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

Accedi per commentare.

Risposte (1)

Arif Hoq
Arif Hoq il 23 Feb 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 Commenti
Stephen23
Stephen23 il 23 Feb 2022
Modificato: Stephen23 il 23 Feb 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

Accedi per commentare.

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by