Square matrix with relationships among equal rows.

2 ビュー (過去 30 日間)
GEORGIOS BEKAS
GEORGIOS BEKAS 2017 年 10 月 26 日
コメント済み: Cedric 2017 年 10 月 27 日
I have a matrix with the following form:
A = [ 9 9 9; 5 6 5; 9 9 9; 4 4 2; 5 6 5; 5 6 5; 4 4 4; 9 9 9]
If a particular row is equal to another, I am searching for a square matrix that contains ones, when a particular row is equal to another. Therefore if row 3 is equal to row 1, I want the elements B(1,3) and B(3,1) of a new matrix B, to be equal to 1.

採用された回答

Andrei Bobrov
Andrei Bobrov 2017 年 10 月 26 日
B = ~squareform(pdist(A));
  2 件のコメント
Jos (10584)
Jos (10584) 2017 年 10 月 27 日
nice one, Andrei! +1
Andrei Bobrov
Andrei Bobrov 2017 年 10 月 27 日
Thank you, Jos!

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

その他の回答 (2 件)

Jos (10584)
Jos (10584) 2017 年 10 月 26 日
編集済み: Jos (10584) 2017 年 10 月 26 日
Use ismember to loop through the rows of A, and work backwards to induce automatic pre-allocation. Note that the diagonal contains 1s as well.
A = [ 9 9 9; 5 6 5; 9 9 9; 4 4 2; 5 6 5; 5 6 5; 4 4 4; 9 9 9];
B = [] ;
for k=size(A,1):-1:1
B(ismember(A,A(k,:),'rows'),k) = 1 ;
end

Cedric
Cedric 2017 年 10 月 26 日
編集済み: Cedric 2017 年 10 月 26 日
B = all(permute(A, [1,3,2]) == permute(A, [3,1,2]), 3) ;
and if you have a version of MATLAB < R2016b:
B = all(bsxfun(@eq, permute(A, [1,3,2]), permute(A, [3,1,2])), 3) ;
  2 件のコメント
Andrei Bobrov
Andrei Bobrov 2017 年 10 月 27 日
+1. My favorite "plows"!
Cedric
Cedric 2017 年 10 月 27 日
Thank you Andrei :)

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by