How to define G=graph(s,t,weights,NodeTable)
1 次查看(过去 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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 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 (한국어)