Find if two sparse matrix have 1 in same position

5 ビュー (過去 30 日間)
Lorenzo Lasagni
Lorenzo Lasagni 2017 年 11 月 1 日
コメント済み: Cedric 2017 年 11 月 1 日
Hi everybody, I'm new here and new in use of matlab. I have two sparse matrix of different sizes and i want to know if both have 1 in position (x,y). There is a way to compare without make a comparison for each elements? (the matrix is a 10^7*10^7).

採用された回答

Cedric
Cedric 2017 年 11 月 1 日
編集済み: Cedric 2017 年 11 月 1 日
Do you need something along this line?
% - Build small test case.
A = sprand( 4, 5, 0.5 ) > 0 ; % 4x5 sparse.
B = sprand( 4, 4, 0.5 ) > 0 ; % 4x4 sparse.
% - Find intersect of matching locations of non-zero elements.
[A_r, A_c] = find( A ) ;
[B_r, B_c] = find( B ) ;
matching_rc = intersect( [A_r,A_c], [B_r,B_c], 'rows' )
Which outputs e.g.:
>> A
A =
4×5 sparse logical array
(4,1) 1
(1,2) 1
(1,3) 1
(2,3) 1
(4,4) 1
(1,5) 1
(2,5) 1
>> B
B =
4×4 sparse logical array
(2,1) 1
(4,1) 1
(2,2) 1
(4,2) 1
(1,4) 1
(2,4) 1
(4,4) 1
>> matching_rc
matching_rc =
4 1
4 4
  2 件のコメント
Lorenzo Lasagni
Lorenzo Lasagni 2017 年 11 月 1 日
thank you so much! It's exactly what i would to do!
Cedric
Cedric 2017 年 11 月 1 日
My pleasure!

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

その他の回答 (1 件)

Cam Salzberger
Cam Salzberger 2017 年 11 月 1 日
Hello Lorenzo,
Sparse matrices in MATLAB are nice because you can often treat them as regular matrices. Specifically, indexing still works just fine:
rng default % For repeatability
A = sparse(randi(2,10)-1);
B = sparse(randi(2,10)-1);
A(3, 8) == 1 && B(3, 8) == 1
A(3, 9) == 1 && B(3, 7) == 1
Hope this helps!
-Cam
  1 件のコメント
Lorenzo Lasagni
Lorenzo Lasagni 2017 年 11 月 1 日
The answer below is what i was looking for, thank you anyway!

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by