フィルターのクリア

How to find out the location difference of nonzero entries in two sparse matrices?

1 回表示 (過去 30 日間)
Benson Gou
Benson Gou 2019 年 2 月 23 日
コメント済み: Stephan 2019 年 2 月 24 日
Hello, All,
I have two sparse matries which have the same size. I know every nonzero entry in A must correspond to a nonzero entry in B, but some of nonzero entries in B which correspond to zero entries in A. For example, A=[0 1 0 2;3 0 0 4;0 5 6 0]; B=[0 1 0 2;3 7 0 4;8 5 6 0]. I want to find out the location of nonzero entries in B which correspind to zero entries in A. The answer to this example is: (2, 2) and (3, 1). Their correspinding entries are 7 and 8 in B.
Thanks.
Benson

採用された回答

Stephan
Stephan 2019 年 2 月 23 日
Hi,
this are not sparse matrices. However you find the locations given as row and column values and the corresponding values with:
A=[0 1 0 2;3 0 0 4;0 5 6 0];
B=[0 1 0 2;3 7 0 4;8 5 6 0];
[row, col] = find(A~=B)
vals = B(A~=B)
results in:
row =
3
2
col =
1
2
vals =
8
7
which is exactly the desired result.
Best regards
Stephan
  2 件のコメント
Benson Gou
Benson Gou 2019 年 2 月 24 日
Hi, Stephan,
Thanks for your response. It is very good. How about the following example?
A=[0 1 0 2;3 0 0 4;0 5 6 0];B=[0 2.43 0 1.09;4.97 7.30 0 3.3;8.03 5.43 9.12 0];
The locations of nonzero entries in A and B are the same (but their values are different) except (2,2) and (3,1), their values are 7.30 and 8.03.
Our real case is like this. Thanks a lot for your great help.
Benson
Stephan
Stephan 2019 年 2 月 24 日
[row, col] = find(A==0 & B~=0)
vals = B(A==0 & B~=0)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeParticle & Nuclear Physics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by