フィルターのクリア

Use one matrix to change values of another

1 回表示 (過去 30 日間)
Michael King
Michael King 2019 年 7 月 4 日
コメント済み: Michael King 2019 年 7 月 4 日
I have two matrices, A and B which are something like:
A = [1; 2; 3; 4; 16; 17; 18; 19 ....] (not a repeating formula just numbers from 1 to 8976 with a lot missing in between)
B = [1 2 3 4 8 9 10 11; 1 2 5 6 7 8 16 17; 3 4 5 6 18 19 20 21]
I want to make it so that the numbers in B that are not in A are change to 0, so that B would look like
[1 2 3 4 0 0 0 0; 1 2 0 0 0 0 16 17; 3 4 0 0 18 19 0 0]

採用された回答

Stephen23
Stephen23 2019 年 7 月 4 日
編集済み: Stephen23 2019 年 7 月 4 日
Method one: indexing:
>> X = ismember(B,A)
>> B(~X) = 0
B =
1 2 3 4 0 0 0 0
1 2 0 0 0 0 16 17
3 4 0 0 18 19 0 0
Method two: multiplication:
>> X = ismember(B,A)
>> B = B.*X
B =
1 2 3 4 0 0 0 0
1 2 0 0 0 0 16 17
3 4 0 0 18 19 0 0
  1 件のコメント
Michael King
Michael King 2019 年 7 月 4 日
Thank you

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

その他の回答 (1 件)

Guillaume
Guillaume 2019 年 7 月 4 日
編集済み: Guillaume 2019 年 7 月 4 日
Simply:
B(~ismember(B, A)) == 0
  1 件のコメント
Michael King
Michael King 2019 年 7 月 4 日
Thank you

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by