이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
How to define G=graph(s,t,weights,NodeTable)
조회 수: 9 (최근 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 (한국어)