Creating adjacency from the matrix containing not sequence number

조회 수: 1 (최근 30일)
Vishnu Kant
Vishnu Kant 2018년 8월 29일
답변: Bruno Luong 2018년 9월 4일
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; ]

채택된 답변

Sachin Meena
Sachin Meena 2018년 9월 4일
You can use the function containers.Map to map individuals to indices (in this case, 1 maps to 1, 2 to 2, 5 to 3, and 6 to 4), and then traverse your relationship matrix and assign values to adjacency matrix accordingly. Use the map to convert related person to index in adjacency matrix.
c=containers.Map;
c('2')=2;
>> c('2')
ans =
2

추가 답변 (1개)

Bruno Luong
Bruno Luong 2018년 9월 4일
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; ]
u=unique(A(:,1));
[~,J]=ismember(A(:,2:end),u);
[i,~]=find(J);
j=J(J>0);
accumarray([i,j],1)

카테고리

Help CenterFile Exchange에서 Networks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by