问题描述
我需要根据以下论文为图的边缘分配权重:
I need to assign weights to edges of a graph, from the following papers:
L.Xiao和S. Boyd的用于分布平均的快速线性迭代"S. Boyd的图拉普拉斯特征值的凸优化"
"Fast linear iterations for distributed averaging" by L. Xiao and S. Boyd"Convex Optimization of Graph Laplacian Eigenvalues" by S. Boyd
我的图形有一个邻接矩阵(一个50 x 50矩阵),具有512个非零值.
I have the adjacency matrix for my graph (a 50 by 50 matrix), with 512 non-zero values.
我还有一个权重为256 x 1的矢量.
I also have a 256 by 1 vector with the optimal weights.
对于我使用的软件,我需要一个50 x 50的矩阵,其边缘(i,j)的权重在邻接矩阵的相关位置(并且边缘(j,i)的符号相反) .
For the software I'm using, I need a 50 by 50 matrix with the weight of edge (i,j) in the relevant position of the adjacency matrix (and with the opposite sign for edge (j,i)).
我的尝试在下面,但我无法使其正常工作.
My attempt is below, but I can't get it working.
function weights = construct_weight_mtx(weight_list, Adj)
weights = zeros(size(Adj));
positions = find(Adj);
for i=1:length(positions)/2
if Adj(i) == 1
weights(i) = weight_list(i);
end
end
weights = weights - weights';
find(Adj) == find(weights);
end
推荐答案
您正在原始邻接矩阵中找到非零位置,但在其中找到了全部.为了解决这个问题,您只需要占据这些职位的前一半即可.
You're finding the nonzero positions in the original adjacency matrix, but you're finding all of them. To get around this, you then take only the first half of those positions.
for i=1:length(positions)/2 ...
不幸的是,这会从完整的列中获取索引,而不仅仅是对角线以下的位置.因此,如果您的矩阵全为1,则可以采用:
Unfortunately, this takes the indices from complete columns rather than just the positions below the diagonal. So if your matrix was all 1's, you'd be taking:
1 1 1 0 0 ...
1 1 1 0 0 ...
1 1 1 0 0 ...
...
代替:
1 0 0 0 0 ...
1 1 0 0 0 ...
1 1 1 0 0 ...
...
要获取正确的值,我们只需获取Adj
的下三角部分,然后找到该位置的非零位置:
To take the correct values, we just take the lower triangular portion of Adj
and then find the nonzero positions of that:
positions = find(tril(Adj));
现在,我们只有对角线以下的256个位置,并且可以遍历所有位置.接下来,我们需要在循环中修复分配:
Now we have only the 256 positions below the diagonal and we can loop over all of the positions. Next, we need to fix the assignment in the loop:
for i=1:length(positions)
if Adj(i) == 1 %// we already know Adj(i) == 1 for all indices in positions
weights(i) = weight_list(i); %// we need to update weights(positions(i))
end
end
所以它变成:
for i=1:length(positions)
weights(positions(i)) = weight_list(i);
end
但是,如果我们要做的就是将256个值分配给256个位置,那么我们可以在没有for
循环的情况下做到这一点:
But if all we're doing is assigning 256 values to 256 positions, we can do that without a for
loop:
weights(position) = weight_list;
请注意,weight_list
的元素必须以正确的顺序排列,下三角部分的非零元素按列排序.
Note that the elements of weight_list
must be in the proper order with the nonzero elements of the lower-triangular portion ordered by columns.
完整代码:
function weights = construct_weight_mtx(weight_list, Adj)
weights = zeros(size(Adj));
positions = find(tril(Adj));
weights(positions) = weight_list;
weights = weights - weights.'; %// ' is complex conjugate; not a big deal here, but something to know
find(Adj) == find(weights); %// Not sure what this is meant to do; maybe an assert?
end
这篇关于创建权重邻接矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!