Square matrix with relationships among equal rows.

1 visualización (últimos 30 días)
GEORGIOS BEKAS
GEORGIOS BEKAS el 26 de Oct. de 2017
Comentada: Cedric el 27 de Oct. de 2017
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.

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 26 de Oct. de 2017
B = ~squareform(pdist(A));
  2 comentarios
Jos (10584)
Jos (10584) el 27 de Oct. de 2017
nice one, Andrei! +1
Andrei Bobrov
Andrei Bobrov el 27 de Oct. de 2017
Thank you, Jos!

Iniciar sesión para comentar.

Más respuestas (2)

Jos (10584)
Jos (10584) el 26 de Oct. de 2017
Editada: Jos (10584) el 26 de Oct. de 2017
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 el 26 de Oct. de 2017
Editada: Cedric el 26 de Oct. de 2017
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 comentarios
Andrei Bobrov
Andrei Bobrov el 27 de Oct. de 2017
+1. My favorite "plows"!
Cedric
Cedric el 27 de Oct. de 2017
Thank you Andrei :)

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by