Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 将邻接矩阵转换为图-拟图_Matlab_Matrix_Graph_Adjacency List_Adjacency Matrix - Fatal编程技术网

Matlab 将邻接矩阵转换为图-拟图

Matlab 将邻接矩阵转换为图-拟图,matlab,matrix,graph,adjacency-list,adjacency-matrix,Matlab,Matrix,Graph,Adjacency List,Adjacency Matrix,首先我要注意的是,这是我关于堆栈溢出的第一个问题,所以请原谅我的问题在表达和格式上的任何错误 我有以下代码将mx3.txt文件转换为邻接矩阵: % Converts edge list to adjacency matrix % INPUTS: edgelist: mx3 % OUTPUTS: adjacency matrix nxn % Note: information about nodes is lost: indices only (i1,...in) remain % Gergana

首先我要注意的是,这是我关于堆栈溢出的第一个问题,所以请原谅我的问题在表达和格式上的任何错误

我有以下代码将mx3.txt文件转换为邻接矩阵:

% Converts edge list to adjacency matrix
% INPUTS: edgelist: mx3
% OUTPUTS: adjacency matrix nxn
% Note: information about nodes is lost: indices only (i1,...in) remain
% Gergana Bounova, Last updated: October 6, 2009

function adj=edgeL2adj(el)   

nodes=sort(unique([el(:,1) el(:,2)]));             % get all nodes, sorted
n=numel(nodes);                                    % number of unique nodes
adj=zeros(n);                                      % initialize adjacency matrix

for i=1:size(el,1)                                 % across all edges
  adj(find(nodes==el(i,1)),find(nodes==el(i,2)))=el(i,3);
end
如果我有一个mx3边列表作为我的输入,让我们称之为网络。我创建了一个矩阵a。然后,我使用matgraph为这个邻接矩阵创建了一个关联图。但我不断遇到如下所述的错误:

A=edgeL2adj(Network);
graph_init;
G=graph;
set_matrix(G,A);
其中“set_matrix.m”是以下函数:

function set_matrix(g,A)
      % set_matrix(g,A) --- set g to be the graph specificed in the matrix A.
      % The matrix is scanned by check_matrix to be sure it is a valid adjacency
      % matrix. If it is not, an error is triggered and the graph is left
      % unchanged. 
if (~check_matrix(A))
   error('Input matrix is not a valid adjacency matrix')
end

fast_set_matrix(g,logical(A));
但我得到了错误

Error using graph/set_matrix (line 8)
Input matrix is not a valid adjacency matrix
然后,我检查了“check_matrix.m”的脚本,其内容如下:

function ok = check_matrix(A)
% check_matrix(A) --- check if A is a valid adjacency matrix for a graph
ok = 0;
% make sure matrix is square
[nr,nc] = size(A);
if (nr ~= nc) 
    return
end
% see if the matrix is zero-one
B = (A>0); % convert to logical (so 0,1).
if (nnz(A-B)>0)
    return
end
% check if symmetric
if (nnz(A-A')>0)
    return
end
% check the diagonal
if (any(diag(A)>0)) 
    return
end
% all tests passed
ok = 1;
依次遍历每个点,我发现A的对角线不是完全由零组成,而是由1和0组成。现在,在主对角线和邻接矩阵中有任何值1是没有意义的,因为不存在从节点到自身的长度为1的行走;在无向网络中,从节点到自身的最短闭合行走为2

有人知道为什么函数“adj=edgeL2adj(el).m”在这里无法将我的.txt文件转换为有效的邻接矩阵吗?我曾想过尝试将主对角线“归零”,但不知道这是否会比它已经改变的矩阵更大,因此导致所有后续分析和工作出错

干杯


编辑:以下是.txt文件的前十行:

1   1   1
4   1   1
2530    1   1
4   2   1
6   2   1
571 2   1
2275    3   1
1   4   1
2   4   1
6   4   1

我无法用我生成的示例边缘列表重现您的问题。您是否有一个(小)样本导致
edgeL2adj
给您一个无效的邻接矩阵?以下是边缘列表.txt文件的前十行:1 1 1 4 1 1 2530 1 1 4 1 2 6 2 1 571 2 1 2275 3 1 4 1 1 2 4 1 6 4 1
1 1 1 1 1 1
看起来它不应该在那里,对吗?请编辑您的问题并添加带有一些格式的边缘列表,以便我们可以看到列。1是.txt文件的第一行。我不知道问题是否出在.txt文件的格式上,因为当我将其加载到matlab中时,列条目彼此“重叠”,如问题的编辑版本所示。如果您将
1
作为一条边来阅读,那么它肯定会在对角线上。您可能应该在输入文件中搜索更多边,其中
el(k,1)==el(k,2)