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

1 回表示 (過去 30 日間)
chocho
chocho 2017 年 5 月 25 日
編集済み: Walter Roberson 2017 年 6 月 6 日
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);

採用された回答

Guillaume
Guillaume 2017 年 5 月 25 日
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.
  1 件のコメント
chocho
chocho 2017 年 5 月 25 日
@Guillaume Thanks bro is worked now!

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 5 月 25 日
adj = adjacency(graph(AA(:,1),AA(:,2)));
  22 件のコメント
Walter Roberson
Walter Roberson 2017 年 6 月 1 日
編集済み: Walter Roberson 2017 年 6 月 6 日
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
chocho 2017 年 6 月 6 日
@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!

サインインしてコメントする。

カテゴリ

Help Center および File Exchange稀疏矩阵 についてさらに検索

Community Treasure Hunt

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

Start Hunting!