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

1 次查看(过去 30 天)
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