現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
How to define G=graph(s,t,weights,NodeTable)
4 ビュー (過去 30 日間)
古いコメントを表示
My method is to create a social network based on a dataset in Matlab. I attached my excel dataset to this question. Each node represents a stakeholder, the edge shows communication among two stakeholders and its weight indicates the total instance of communication between them. I think the following method is appropriate to my subject: G=graph(s,t, weights, NodeTable); How can I define the above variables with this regards that my dataset includes letters? I will be grateful to have any similar examples or related link in this field.
採用された回答
Walter Roberson
2018 年 4 月 24 日
>> S = {'harold', 'harold', 'lucy'}, T = {'sally', 'maude', 'maude'}
S =
1×3 cell array
{'harold'} {'harold'} {'lucy'}
T =
1×3 cell array
{'sally'} {'maude'} {'maude'}
>> G = graph(S,T)
G =
graph with properties:
Edges: [3×1 table]
Nodes: [4×1 table]
>> plot(G)
22 件のコメント
phdcomputer Eng
2018 年 4 月 26 日
編集済み: Walter Roberson
2018 年 4 月 26 日
Thank you very much S & T are not apparent in my graph because in your example you know that Harold and sally have a communication so you can write them in the same place in S & T, but in my graph, I don't have this information.
I wrote my question that is related to this subject at the following link: https://nl.mathworks.com/matlabcentral/answers/397497-how-to-define-cell-arrays-that-their-elements-are-extracted-from-a-dataset I'll be very grateful if suggest me what is the correct method for this problem?
phdcomputer Eng
2018 年 4 月 30 日
Their data type is character, I attached my excel file to this comment. My desired output is to create an undirected weighted graph based on this excel dataset in Matlab. I will be grateful to have any similar examples or related link in this field.
Walter Roberson
2018 年 4 月 30 日
file = 'Firefox.xlsx';
opts = detectImportOptions(file); %detect that "null" is used for some times
data = readtable(file, opts);
G = graph(data.Assignee, data.Reporter);
plot(G);
phdcomputer Eng
2018 年 5 月 4 日
Thank you very much I attached the output graph here. I'll be very grateful if suggest me how to calculate the edges' weights from a dataset?
Steven Lord
2018 年 5 月 4 日
Define what you mean by "edges' weights" in this context. Usually the weights are part of the data you use to create the graph, not something you compute.
Although ... do you perhaps mean "importance" (like the importance of the nodes that the edge connects?) in this context? If so look at the centrality function.
Walter Roberson
2018 年 5 月 4 日
It would not be uncommon to define "weight" according to the repetition count. But it happens that with that data, all of the entries form unique pairs -- the repetition count is 1 for each.
You can build the graph this way:
[nodenames, ~, nodeidx] = unique([data.Assignee; data.Reporter]);
nument = height(data);
numnodes = size(nodenames,1);
adj = accumarray([nodeidx(1:nument),nodeidx(nument+1:end)], 1, [numnodes numnodes]);
G = digraph(adj, nodenames);
plot(G)
The weight matrix is then already in adj, but it can also be recovered later by adjacency(G)
For the particular data you posted, the adj entries will all be 0 and 1.
phdcomputer Eng
2018 年 5 月 7 日
Thank you very much what are nodeidx and nument in the graph? what does accumarray do? Is it possible to give more explanations about the lines of code? very grateful
Walter Roberson
2018 年 5 月 7 日
The first output of unique() is the list of unique values. The third output of unique() is the same length as the input, and for each original input value, gives the index at which the value appears in the list of unique values. So after that unique() call, if you were to do nodenames(nodeidx) then you would have recovered the original input. The alphabetically first name would appear first in the list of unique node names, and every place that nodeidx comes out as 1 is a place in the original input that had that alphabetically-first node name.
nument = height(data) is the same nument = size(data,1) -- it is the number of rows in the table.
numnodes = size(nodenames,1) is the number of unique nodes.
Now, I constructed the input to unique as [data.Assignee; data.Reporter] . That is size(data,1) entries of Assignee followed by the same number of entries of Reporter. So when we look at nodeidx the first size(data,1) entries are related to the Assignee information and the rest are related to the Reporter information.
We can now count the number of times an Assignee was matched with a Reporter by constructing a 2D array in which the first index corresponds to who the item was assigned to, and the second item corresponds to the reporter. When can then go through the input pairs of Assignee and Reporter and add 1 to the corresponding entry in the 2d table, and keep doing that. At the end, the numbers in the table would correspond to the number of times a particular Assignee was associated with a particular Reporter. That in turn is the same as the edge count between the Assignee and Reporter nodes.
This function is implemented by using accumarray(), which is designed for that kind of counting. For the first index (rows) we use the index number into the list of unique names that the Assignee had, and for the second index (columns) we use the index number into the list of unique names that the Reporter had.
Afterwards, adj (the output of accumarray) will be 0 where two nodes did not have the Assignee / Reporter connection, and would be a non-zero count of the number of times that connection occurred in the input otherwise. It is, in other words, and adjacency matrix containing appropriate weights.
We then use the adjacency matrix and the list of node names to create a directed graph.
phdcomputer Eng
2018 年 5 月 8 日
Thanks for your patience and spending your precious time on my question I used the codes but the following line shows error: G=digraph(adj,nodenames); "The expression to the left of the equals sign is not a valid target for an assignment."
Walter Roberson
2018 年 5 月 8 日
I just tried it and it worked for me:
file = 'Firefox.xlsx';
opts = detectImportOptions(file); %detect that "null" is used for some times
data = readtable(file, opts);
[nodenames, ~, nodeidx] = unique([data.Assignee; data.Reporter]);
nument = height(data);
numnodes = size(nodenames,1);
adj = accumarray([nodeidx(1:nument),nodeidx(nument+1:end)], 1, [numnodes numnodes]);
G = digraph(adj, nodenames);
plot(G)
Could you remind me which MATLAB version you are using?
phdcomputer Eng
2018 年 5 月 8 日
My Matlab version is R2016a. I received this error: "Undefined function or variable 'detectImportOptions'."
Walter Roberson
2018 年 5 月 8 日
Provided that you do not need the ResolveDate information, change to
file = 'Firefox.xlsx';
data = readtable(file);
[nodenames, ~, nodeidx] = unique([data.Assignee; data.Reporter]);
nument = height(data);
numnodes = size(nodenames,1);
adj = accumarray([nodeidx(1:nument),nodeidx(nument+1:end)], 1, [numnodes numnodes]);
G = digraph(adj, nodenames);
plot(G)
If you need the ResolveDate than a bit more work is required, because some of the entries contain 'null' instead of a valid time.
phdcomputer Eng
2018 年 5 月 8 日
I used your codes for 3500 rows of the dataset I attached the resulting graph in this comment. It's a directed graph without weights on edges and the shape of the graph is wired. I wanted to ask your opinion about the result.
Walter Roberson
2018 年 5 月 8 日
It looks plausible to me.
You might also want to try with
G2 = digraph(adj .', nodenames);
plot(G2)
phdcomputer Eng
2018 年 5 月 11 日
The resulting graph doesn't show weights values on its edges. Is it right?
phdcomputer Eng
2018 年 5 月 16 日
Thank you very much I wanted to apply a hierarchical clustering algorithm (average-link) on the resulting graph, I'll be grateful if suggest me how to define the graph as input for the clustering algorithm? because usually, I load the dataset (.mat file) as input.
phdcomputer Eng
2018 年 5 月 16 日
編集済み: Walter Roberson
2018 年 5 月 16 日
I found the following link for community detection toolbox: https://nl.mathworks.com/matlabcentral/fileexchange/45867-community-detection-toolbox
If I install the toolbox with the method shown in the link below, is it possible to disable the Matlab program installed on my computer? https://nl.mathworks.com/videos/package-a-custom-matlab-toolbox-106803.html
Walter Roberson
2018 年 5 月 16 日
No, programs packaged that way require MATLAB to execute. In order to not require MATLAB to execute, you would need to use either MATLAB Compiler or MATLAB Coder.
phdcomputer Eng
2018 年 5 月 16 日
In your opinion which method is better for my graph: installing the community detection toolbox or writing codes for graph clustering?
Walter Roberson
2018 年 5 月 16 日
I do not know anything about that toolbox. Typically using an existing toolbox is easier than writing your own code.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Graph and Network Algorithms についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)