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

조회 수: 1 (최근 30일)
Ammy
Ammy 2022년 2월 23일
댓글: Stephen23 2022년 2월 23일
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
Stephen23
Stephen23 2022년 2월 23일
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

댓글을 달려면 로그인하십시오.

답변 (1개)

Arif Hoq
Arif Hoq 2022년 2월 23일
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
Stephen23
Stephen23 2022년 2월 23일
편집: Stephen23 2022년 2월 23일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by