从数据集创建矩阵
creating matrix from data-set
我有一个有边和点的网络。 FID
显示由 Start_Point
和 End_Point
创建的每条边的 ID 号。例如,边 3
位于网络中 2
和 3
两点之间。
FID Start_Point End_Point
1 1 2
2 1 4
3 2 3
4 2 4
我想创建一个 4-by-4 这些点的矩阵。如果两点之间有一条边,则值为 1
否则为 inf
:
[inf, 1, inf, 1;
1, inf, 1, 1;
inf, 1, inf, inf;
1, 1, inf, inf]
如何在 MATLAB 中创建这样的矩阵?
所以您基本上是想从边的邻接列表创建邻接矩阵?你的边数(即你的 FID
列)是无关紧要的,所以我假设你的输入数据的形式是
edges = [1 2
1 4
2 3
2 4]
现在边的第一列是邻接矩阵的行,第二列是列(反之亦然,因为矩阵是对称的,所以没关系)
最简单的解决方案是使用通过 sub2ind
函数获得的线性索引:
adj = inf(size(edges,2));
idx = sub2ind(size(adj),edges(:,1), edges(:,2))
adj(idx) = 1;
我怀疑您的 edges
矩阵已经是对称的,但如果不是,则只需使用
edges_sym = [edges; fliplr(edges)]
而不是edges
可以将其转化为稀疏矩阵,然后使用full命令获取邻接矩阵。
edges= [1 2;
3 4;
3 1
2 3];
n=size(edges,1);
% create sparse matrix with given edges and their reverse direction
A = sparse([edges(:,1); edges(:,2)],[edges(:,2); edges(:,1)],[ones(n,1); ones(n,1)]);
% create adjacency matrix
B=full(A);
% set zeros to inf
B(B==0)=inf;
这是结果:
A =
(2,1) 1
(3,1) 1
(1,2) 1
(3,2) 1
(1,3) 1
(2,3) 1
(4,3) 1
(3,4) 1
>> B
B =
Inf 1 1 Inf
1 Inf 1 Inf
1 1 Inf 1
Inf Inf 1 Inf
编辑:
sparse 命令创建一个稀疏矩阵,其中包含其元素的寻址值。该命令的一个原型如下:
A=sparse(rows,cols,values);
例如A=sparse([1;2],[1,3],[10,5])
是A(1,1)=10
和A(2,3)=5
等元素为零的矩阵:
A=sparse([1;2],[1,3],[10,5]);
>> full(A)
ans =
10 0 0
0 0 5
在您的情况下,您必须向稀疏矩阵(对称)添加两个方向,并且所有值都是一个。所以你需要构造稀疏矩阵为:
A = sparse([edges(:,1); edges(:,2)],[edges(:,2); edges(:,1)],[ones(n,1); ones(n,1)]);
full 命令将稀疏矩阵转换为密集矩阵。
您可以使用 accumarray
:
edges1 = accumarray([startpoint endpoint]),1);
edges2 = edges1.'; % transpose your matrix, to obtain both edges
edges = edges1+edges2;
edges(edges==0)=inf;
accumarray
收集所有具有公共索引的点,将值 1
粘贴到这些索引上。 edges1
是edges2
的转置,这样转置,然后把两者相加。找到矩阵为 0
的所有索引,然后用 inf
.
填充这些值
选择:
edges= [1 2;
3 4;
3 1
2 3];
matrix = accumarray([edges;fliplr(edges)],1,[],[],inf);
fliplr
从左到右翻转矩阵,以获得所有所需的索引组合。然后使用accumarray
在edges
指定的所有位置设置一个1
并在其他位置设置inf
。
如果您确定您的矩阵是对称的,请不要使用 fliplr
,如果您确定您的矩阵是非对称的,请使用 fliplr
如果您不确定使用此:
matrix = accumarray([edges;fliplr(edges)],1,[],@mean,inf);
其中 @mean
确保将双项设置为 1。对于加权边执行以下操作,其中 weights
是包含权重的 Nx1
数组,N
是边数。
matrix = accumarray([edges;fliplr(edges)],weights,[],@mean,inf);
我有一个有边和点的网络。 FID
显示由 Start_Point
和 End_Point
创建的每条边的 ID 号。例如,边 3
位于网络中 2
和 3
两点之间。
FID Start_Point End_Point
1 1 2
2 1 4
3 2 3
4 2 4
我想创建一个 4-by-4 这些点的矩阵。如果两点之间有一条边,则值为 1
否则为 inf
:
[inf, 1, inf, 1;
1, inf, 1, 1;
inf, 1, inf, inf;
1, 1, inf, inf]
如何在 MATLAB 中创建这样的矩阵?
所以您基本上是想从边的邻接列表创建邻接矩阵?你的边数(即你的 FID
列)是无关紧要的,所以我假设你的输入数据的形式是
edges = [1 2
1 4
2 3
2 4]
现在边的第一列是邻接矩阵的行,第二列是列(反之亦然,因为矩阵是对称的,所以没关系)
最简单的解决方案是使用通过 sub2ind
函数获得的线性索引:
adj = inf(size(edges,2));
idx = sub2ind(size(adj),edges(:,1), edges(:,2))
adj(idx) = 1;
我怀疑您的 edges
矩阵已经是对称的,但如果不是,则只需使用
edges_sym = [edges; fliplr(edges)]
而不是edges
可以将其转化为稀疏矩阵,然后使用full命令获取邻接矩阵。
edges= [1 2;
3 4;
3 1
2 3];
n=size(edges,1);
% create sparse matrix with given edges and their reverse direction
A = sparse([edges(:,1); edges(:,2)],[edges(:,2); edges(:,1)],[ones(n,1); ones(n,1)]);
% create adjacency matrix
B=full(A);
% set zeros to inf
B(B==0)=inf;
这是结果:
A =
(2,1) 1
(3,1) 1
(1,2) 1
(3,2) 1
(1,3) 1
(2,3) 1
(4,3) 1
(3,4) 1
>> B
B =
Inf 1 1 Inf
1 Inf 1 Inf
1 1 Inf 1
Inf Inf 1 Inf
编辑: sparse 命令创建一个稀疏矩阵,其中包含其元素的寻址值。该命令的一个原型如下:
A=sparse(rows,cols,values);
例如A=sparse([1;2],[1,3],[10,5])
是A(1,1)=10
和A(2,3)=5
等元素为零的矩阵:
A=sparse([1;2],[1,3],[10,5]);
>> full(A)
ans =
10 0 0
0 0 5
在您的情况下,您必须向稀疏矩阵(对称)添加两个方向,并且所有值都是一个。所以你需要构造稀疏矩阵为:
A = sparse([edges(:,1); edges(:,2)],[edges(:,2); edges(:,1)],[ones(n,1); ones(n,1)]);
full 命令将稀疏矩阵转换为密集矩阵。
您可以使用 accumarray
:
edges1 = accumarray([startpoint endpoint]),1);
edges2 = edges1.'; % transpose your matrix, to obtain both edges
edges = edges1+edges2;
edges(edges==0)=inf;
accumarray
收集所有具有公共索引的点,将值 1
粘贴到这些索引上。 edges1
是edges2
的转置,这样转置,然后把两者相加。找到矩阵为 0
的所有索引,然后用 inf
.
选择:
edges= [1 2;
3 4;
3 1
2 3];
matrix = accumarray([edges;fliplr(edges)],1,[],[],inf);
fliplr
从左到右翻转矩阵,以获得所有所需的索引组合。然后使用accumarray
在edges
指定的所有位置设置一个1
并在其他位置设置inf
。
如果您确定您的矩阵是对称的,请不要使用 fliplr
,如果您确定您的矩阵是非对称的,请使用 fliplr
如果您不确定使用此:
matrix = accumarray([edges;fliplr(edges)],1,[],@mean,inf);
其中 @mean
确保将双项设置为 1。对于加权边执行以下操作,其中 weights
是包含权重的 Nx1
数组,N
是边数。
matrix = accumarray([edges;fliplr(edges)],weights,[],@mean,inf);