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
Ammy
Ammy 2022 年 2 月 23 日
@Arif Hoq, @Stephen many thanks

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by