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
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
chan 2015 年 10 月 3 日
This row represents nodes and column represent keys of the node.here there are 10 nodes and each node is having 2keys.here node1 1st column is having value 575 and 8th row 2nd column is having 575.thus node 1 is sharing key with node 8.so I ve to create an adjacency matrix 10 by 10.i ve to check all the rows keys which is common to node 1.
Walter Roberson
Walter Roberson 2015 年 10 月 3 日
Then the code I provided in my Answer should do what you want.
chan
chan 2015 年 10 月 4 日
NO sir your code din't work.i have attached a file.the output will be like that based on that M matrix.In first row there are two 1's because node 1 is sharing key with 7 and 8.similarly i have to find for other nodes with whom they are sharing key.Sir could you please help in implementing this using for loop.thank you.
Walter Roberson
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
Walter Roberson 2015 年 10 月 3 日

0 投票

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 ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

質問済み:

2015 年 10 月 3 日

コメント済み:

2015 年 10 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by