How to get the adjacency matrix from a cell array of strings?

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

If you're using a very ancient matlab version such as R2012a, then mention it in your question.
This would work on R2012a:
[nodenames, ~, id] = unique(AA(:));
adj = accumarray(reshape(id, size(AA)), 1, [numel(nodenames), numel(nodenames)]);
rows and columns of adj are in order of nodenames.

More Answers (1)

adj = adjacency(graph(AA(:,1),AA(:,2)));

22 Comments

@Walter Roberson, Thanks bro for your quick reply. PLZ check the A.mat i have shared with you.
I got this error Undefined function 'graph' for input arguments of type 'cell'.
Error in adj (line 5) adj = adjacency(graph(AA(:,1),AA(:,2)));
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.
hi, friend, i'm using Matlab R2012a
[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.
@Walter Roberson, Thanks bro your code work now! and sorry to don't let you know the version of my system. could you tell me "adj = adjacency(graph(AA(:,1),AA(:,2)));" in wich version it can work ???
All the powerful graph functions (plotting, searching, construction, etc.) were implemented in R2015b
@Guillaume Thank you too much
Though building it with unique() and sparse() is probably more efficient if you do not happen to be using those newer graph routines.
@Guillaume, is it possible to do the revere operation? means how to turn this adjacency or any transition matrix to a graph by using Matlab R2012a based on the same list of nodes that i have shared before?
[r, c, s] = find(YourAdjacencyMatrix);
EdgeList = [unames(r), unames(c)];
Note: this assumes undirected graph.
@Walter Roberson,i got this bug:
Undefined function 'unames' for input arguments of type 'double'.
Error in disjoint (line 17) EdgeList = [unames(r), unames(c)];
@Walter Roberson, these all what i have. .
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.
@Walter Roberson, these all what i have. .
[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.
@Walter Roberson, Thank you! Now worked well!
Last questions if you don't mind it! In this Adjacency_Matrix_mod1, i found some zeros-columns (no nodes are connected at any cell) which i want to delete, what command can do that?
mask = all(Adjacency_Matrix_mod1,1);
new_adj = Adjacency_Matrix_mod1(:,mask);
new_nodenames = nodenames1(mask);
I have tried this matrix and it produces [].
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
@Walter Roberson, I think you are good at graphs,i opened a new question ,could you Plz check the adjacency matrix and transition i shared. With my respect to you!

Sign in to comment.

Asked:

on 25 May 2017

Edited:

on 6 Jun 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!