Matlab: Find row indexes with common elements

find, matlab, row

Solution

Consider this

A =  [1 2 3;       %Matrix A is a bit different from yours for testing
      4 5 6;
      7 8 1;
      1 2 7;
      4 5 6];

[row col] =size(A)            

answers = zeros(row,row);     %matrix of answers,...
                              %(i,j) = 1 if row_i and row_j have an equal element 

for i = 1:row
    for j = i+1:row                       %analysis is performed accounting for
                                          % symmetry constraint
        C = bsxfun(@eq,A(i,:),A(j,:)');   %Tensor comparison
        if( any(C(:)) )                   %If some entry is non-zero you have equal elements
            answers(i,j) = 1;               %output               
        end
    end
end
answers =  answers + answers';              %symmetric

The output here is

answers =

  0     0     1     1     0
  0     0     0     0     1
  1     0     0     1     0
  1     0     1     0     0
  0     1     0     0     0

of course the `answers` matrix is symmetric because your relation is.

Problem

I have a Matrix: ``` 1 2 3 4 5 6 7 8 1 ``` How may I use matlab to find this: for 1st row: row3 for 2nd row: --- for 3rd row: row1 I want to have row indices for each row witch have common elements.

Original source