You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to define G=graph(s,t,weights,NodeTable)
1 view (last 30 days)
Show older comments
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.
Accepted Answer
Walter Roberson
on 24 Apr 2018
>> 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 Comments
phdcomputer Eng
on 26 Apr 2018
Edited: Walter Roberson
on 26 Apr 2018
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?
Walter Roberson
on 26 Apr 2018
In your excel file, what data types are Assignee and Reporter ?
phdcomputer Eng
on 30 Apr 2018
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
on 30 Apr 2018
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
on 4 May 2018
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
on 4 May 2018
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
on 4 May 2018
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
on 7 May 2018
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
on 7 May 2018
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
on 8 May 2018
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
on 8 May 2018
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
on 8 May 2018
My Matlab version is R2016a. I received this error: "Undefined function or variable 'detectImportOptions'."
Walter Roberson
on 8 May 2018
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
on 8 May 2018
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
on 8 May 2018
It looks plausible to me.
You might also want to try with
G2 = digraph(adj .', nodenames);
plot(G2)
phdcomputer Eng
on 11 May 2018
The resulting graph doesn't show weights values on its edges. Is it right?
Walter Roberson
on 11 May 2018
plot(G2, 'EdgeLabel', G2.Edges.Weight)
phdcomputer Eng
on 16 May 2018
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
on 16 May 2018
Edited: Walter Roberson
on 16 May 2018
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
on 16 May 2018
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
on 16 May 2018
In your opinion which method is better for my graph: installing the community detection toolbox or writing codes for graph clustering?
Walter Roberson
on 16 May 2018
I do not know anything about that toolbox. Typically using an existing toolbox is easier than writing your own code.
More Answers (0)
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)