Algorithm 我想用另一个较小矩阵中给出的权重替换邻接矩阵中1的值
如何用另一个矩阵中给定的权重替换邻接矩阵中1的值? 例如:Algorithm 我想用另一个较小矩阵中给出的权重替换邻接矩阵中1的值,algorithm,matlab,adjacency-matrix,Algorithm,Matlab,Adjacency Matrix,如何用另一个矩阵中给定的权重替换邻接矩阵中1的值? 例如: adjacent_matrix = [1 0 0 1; 0 0 1 1; 1 0 1 0; 0 1 1 0 ] weight_matrix = [ 2 4 6 2; 4 5 1 3] 最后的矩阵应该是这样的:[2 0 0 4;0 0 6 2;4 0 5 0;0 1 3 0]code- out = adjacent_matrix'; out(out==1) = reshape(weight_matrix',1,numel(weight_
adjacent_matrix = [1 0 0 1; 0 0 1 1; 1 0 1 0; 0 1 1 0 ]
weight_matrix = [ 2 4 6 2; 4 5 1 3]
最后的矩阵应该是这样的:[2 0 0 4;0 0 6 2;4 0 5 0;0 1 3 0]
code-
out = adjacent_matrix';
out(out==1) = reshape(weight_matrix',1,numel(weight_matrix))';
out = out';
输入“相邻矩阵”和“权重矩阵”保持不变,如@chappjc所建议。
accumarray
解决方案:
>> [ii,jj] = find(adjacent_matrix.');
>> out = accumarray([ii jj],reshape(weight_matrix.',[],1)).'
out =
2 0 0 4
0 0 6 2
4 0 5 0
0 1 3 0
[ii,jj] = find(adjacent_matrix.');
out = full(sparse(ii,jj,weight_matrix.')).'
sparse
解决方案:
>> [ii,jj] = find(adjacent_matrix.');
>> out = accumarray([ii jj],reshape(weight_matrix.',[],1)).'
out =
2 0 0 4
0 0 6 2
4 0 5 0
0 1 3 0
[ii,jj] = find(adjacent_matrix.');
out = full(sparse(ii,jj,weight_matrix.')).'
您还需要转置
权重矩阵
。为OP添加一些解释也很好。@Praetorian不这么认为。@Praetorian将其与OP的期望结果进行比较。它匹配,因为我无意中发布了正确的代码。请再看一次。@Divakar不要将相邻的_矩阵
作为输出。因为输入发生了变化,所以会引起混乱@但裁判官是对的。