[英]matlab - Accumarray Adjacency Matrix Confusion?
So I'm trying to create an adjacency matrix, and I'm confused on the difference between accumarray(matrix+1,1)
and accumarray(matrix,1)
. 所以我试图创建一个邻接矩阵,而我对
accumarray(matrix+1,1)
和accumarray(matrix,1)
之间的区别感到困惑。
I did: 我做了:
matrix = [ 1 3
4 2
1 3
3 1]
adMatrix1 = accumarray(matrix,1);
adMatrix1=adMatrix1~=0;
adMatrix1 = [0 0 1
0 0 0
1 0 0
0 1 0]
and then: 接着:
adMatrix2 = accumarray(matrix+1,1);
adMatrix2=adMatrix2~=0;
adMatrix2 = [0 0 0 0
0 0 0 1
0 0 0 0
0 1 0 0
0 0 1 0]
I know that with the "matrix+1", there's an extra row and column of zero's, but I don't understand why you would do it that way. 我知道使用“矩阵+1”时,会有额外的零行和零列,但是我不明白为什么您会那样做。 When I looked it up, according to this post I should use "matrix+1", and the best explanation that I got for that was that "because indexing in matlab starts at 1".
当我查找它时,根据这篇文章,我应该使用“ matrix + 1”,对此我最好的解释是“因为在matlab中索引从1开始”。
I don't understand that at all... if I was trying to create an adjacency matrix, which way is correct? 我一点都不明白...如果我试图创建一个邻接矩阵,哪种方法正确? Any help would be greatly appreciated, thanks!
任何帮助将不胜感激,谢谢!
If your node IDs are 0 indexed you need the +1, otherwise you don't. 如果您的节点ID索引为0,则需要+1,否则不需要。 So the question you need to ask is, are your node IDs 0 indexed or 1 indexed?
因此,您需要问的问题是,您的节点ID是0索引还是1索引?
Does your matrix accept multiple links? 您的矩阵接受多个链接吗? If yes, then both results with
accumarray
above are not correct, since node 1 and 3 are connected 2 times. 如果是,则上面的
accumarray
两个结果都不正确,因为节点1和3连接了两次。
Btw, you can consider sparse
and full
: 顺便说一句,您可以考虑
sparse
和full
:
full(sparse(matrix(:,1), matrix(:,2), ones(1, size(matrix, 1))))
ans =
0 0 2
0 0 0
1 0 0
0 1 0
ones(1, size(matrix, 1))
is actually a vector of weight. ones(1, size(matrix, 1))
实际上是一个权重向量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.