algorithm - I want replace the values of 1 in an adjacency matrix with weights given in another smaller matrix -
how can replace values of 1 in adjacency matrix weights given in matrix? example:
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] the final matrix should this: [2 0 0 4; 0 0 6 2; 4 0 5 0; 0 1 3 0]
accumarray solution:
>> [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 sparse solution:
[ii,jj] = find(adjacent_matrix.'); out = full(sparse(ii,jj,weight_matrix.')).'
Comments
Post a Comment