How to get the adjacency matrix from a cell array of strings?
Show older comments
Hi friends, I'm so sorry for asking multiple questions in a short time. i have this cell array of size(60*2), and i want to get the adjacency matrix from this cell array. i have tried sparse and accumarray but i get the following error :
accumarray(AA+1,1)
or
sparse(AA(:,1)+1,AA(:,2)+1,1)
Undefined function 'plus' for input arguments of type 'cell'.
Error in adj (line 5) sparse(AA(:,1)+1,AA(:,2)+1,1) Error in adj (line 5) accumarray(AA+1,1);
Accepted Answer
More Answers (1)
Walter Roberson
on 25 May 2017
adj = adjacency(graph(AA(:,1),AA(:,2)));
22 Comments
Andrei Bobrov
on 25 May 2017
+1. Our Sensei!
Walter Roberson
on 25 May 2017
chocho, I did test. If you are using an especially old version of MATLAB, it is up to you to tell us which version so that we do not waste our time giving answers that are unusable on your system.
chocho
on 25 May 2017
Walter Roberson
on 25 May 2017
[unames, ~, uidx] = unique(AA);
adj = sparse(uidx(1:end/2), uidx(end/2+1:end), 1);
The node names are now in unames, and the adjacency matrix is now in adj . The order of rows and columns will be the same as the alphabetic order of the node names.
chocho
on 25 May 2017
Guillaume
on 25 May 2017
All the powerful graph functions (plotting, searching, construction, etc.) were implemented in R2015b
chocho
on 25 May 2017
Walter Roberson
on 25 May 2017
Though building it with unique() and sparse() is probably more efficient if you do not happen to be using those newer graph routines.
chocho
on 31 May 2017
Walter Roberson
on 31 May 2017
[r, c, s] = find(YourAdjacencyMatrix);
EdgeList = [unames(r), unames(c)];
Note: this assumes undirected graph.
chocho
on 31 May 2017
chocho
on 31 May 2017
Walter Roberson
on 31 May 2017
It comes from
[unames, ~, uidx] = unique(AA);
In other words, unames should be a cell array of strings in order by node number, that name each of the nodes.
chocho
on 31 May 2017
Walter Roberson
on 31 May 2017
[r, c, s] = find(Adjacency_Matrix_mod1);
EdgeList = [nodenames1(r),nodenames1(c)];
The result will be a cell array of strings with two columns, indicating that an edge exists from the first item to the second item.
chocho
on 1 Jun 2017
Walter Roberson
on 1 Jun 2017
mask = all(Adjacency_Matrix_mod1,1);
new_adj = Adjacency_Matrix_mod1(:,mask);
new_nodenames = nodenames1(mask);
chocho
on 1 Jun 2017
chocho
on 1 Jun 2017
Walter Roberson
on 1 Jun 2017
Edited: Walter Roberson
on 6 Jun 2017
Change the all() to any()
However! You are using directed graphs, and just because nothing links to a particular node does not mean that the node is isolated: it might itself have links to other nodes.
When you remove columns but not rows then you distort the meaning.
If you have a square matrix perhaps you should be using
mask = any(Adjacency_Matrix_mod1,1) | any(Adjacency_Matrix_mod1,2);
new_adj = Adjacency_Matrix_mod1(:,mask);
new_nodenames = nodenames1(mask);
This will not remove an all-zero column unless the corresponding row is also empty
chocho
on 6 Jun 2017
Categories
Find more on Annotations 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!