How to get the adjacency matrix from a cell array of strings?
Mostra commenti meno recenti
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);
Risposta accettata
Più risposte (1)
Walter Roberson
il 25 Mag 2017
adj = adjacency(graph(AA(:,1),AA(:,2)));
22 Commenti
Andrei Bobrov
il 25 Mag 2017
+1. Our Sensei!
Walter Roberson
il 25 Mag 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
il 25 Mag 2017
Walter Roberson
il 25 Mag 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
il 25 Mag 2017
Guillaume
il 25 Mag 2017
All the powerful graph functions (plotting, searching, construction, etc.) were implemented in R2015b
chocho
il 25 Mag 2017
Walter Roberson
il 25 Mag 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
il 31 Mag 2017
Walter Roberson
il 31 Mag 2017
[r, c, s] = find(YourAdjacencyMatrix);
EdgeList = [unames(r), unames(c)];
Note: this assumes undirected graph.
chocho
il 31 Mag 2017
chocho
il 31 Mag 2017
Walter Roberson
il 31 Mag 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
il 31 Mag 2017
Walter Roberson
il 31 Mag 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
il 1 Giu 2017
Walter Roberson
il 1 Giu 2017
mask = all(Adjacency_Matrix_mod1,1);
new_adj = Adjacency_Matrix_mod1(:,mask);
new_nodenames = nodenames1(mask);
chocho
il 1 Giu 2017
chocho
il 1 Giu 2017
Walter Roberson
il 1 Giu 2017
Modificato: Walter Roberson
il 6 Giu 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
il 6 Giu 2017
Categorie
Scopri di più su Annotations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!