-1

I have a 4x6 matrix (directed network) similar to the following example:So first column represent individuals and each rows represent individual friendship with other individuals so first row represent individual 1 friendship with 2 and 5 and similarly row 3 represent individual 5 friendship with individuals 2 and 6. Zeros have no meaning.

[ 1 2 0 5 0 0; 
  2 1 0 0 5 6; 
  5 2 6 0 0 0; 
  6 1 2 0 0 5; ]

So now since there are four individuals I want **4x4 adjacency matrix representing friendship meaning 0 indicating no friendship and 1 indicating friendship**. So first row in the following represent individual 1 friendship with 2,5 as 1 meaning they are friend and 0 for 6 they are not friend. Similarly row two represent friendship individual 2 with 1,5 and 6 as 1 meaning he/she is friend with all the individuals.

[ 0 1 1 0; 
  1 0 1 1;  
  0 1 0 1; 
  1 1 1 0; ]

So how can I get this matrix in MATLAB?

  • 6
    As with your previous question, I have no idea what the input matrix is supposed to represent and how you're getting from the input matrix to the result matrix. Please clarify with a [mcve]. – beaker Aug 28 '18 at 14:33
  • Does it helps now? – Vishnu Kant Aug 29 '18 at 08:57
  • Yes, that's clearer. Are you sure you want a 4x4 matrix instead of a 6x6? It seems to me that with 6x6 it'd be easier to keep the labels straight, and making it into a 4x4 adds an extra step, but it's definitely possible either way. I've got some work I need to get to, but I'll check back and work on an answer if nobody has gotten to it in the meantime. – beaker Aug 29 '18 at 14:39

1 Answers1

2

The most straightforward approach is to modify the answer found here:

A = [1 2 0 5 0 0; 
     2 1 0 0 5 6; 
     5 2 6 0 0 0; 
     6 1 2 0 0 5];

adjMat = zeros(max(A(:)));   % create full 6x6 matrix, then chop it down
for ind = 1:size(A,1)
    % Flag 1 on each row 'ind' at the indices mentioned in col 2-5
    adjMat(A(ind,1), nonzeros(A(ind,2:end))) = 1;
end
% use only rows/columns with indices in first column of A
adjMat = adjMat(A(:,1), A(:,1));

If it turns out you want to use the full 6x6 matrix, just stop before the last line or use the 6x6 input matrix with zeros in the unwanted rows (in which case the last line is again unnecessary).

beaker
  • 16,331
  • 3
  • 32
  • 49