how to create adjacency matrix
이전 댓글 표시
How to create adjacency matrix 10 by 10 based on this developed commands
X=randi([1,1000],10,1); M=[X,X(randperm(length(X)))];
Whenever there is a common value in the matrix developed assign 1 in the matrix 10 by 10 else 0. (using for loop)
댓글 수: 5
Walter Roberson
2015년 10월 3일
Please explain what you mean by "a common value" ?
For example for this M, what would you expect the result to be?
575 84
846 626
739 730
586 667
247 586
667 739
84 661
626 575
661 247
730 846
chan
2015년 10월 3일
Walter Roberson
2015년 10월 3일
Then the code I provided in my Answer should do what you want.
chan
2015년 10월 4일
Walter Roberson
2015년 10월 4일
You did not attach a file.
When you used randperm() like that, each element will appear exactly once, and so node 1 should share "keys" with only one other node.
Read the adjacency matrix I created down the rows. Or reverse the order of the indices. Makes no difference to me.
답변 (1개)
Walter Roberson
2015년 10월 3일
My guess as to what you want is:
X = randi([1,1000],10,1);
idx = [(1:length(X)).', randperm(length(X)).'];
M = X(idx);
Adj = zeros(length(X),length(X));
Adj(sub2ind(size(Adj), idx(:,1), idx(:,2))) = 1;
That last line can be more efficiently written as
Adj(length(X)*(idx(:,2)-1)+idx(:,1)) = 1;
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!