How can I sum edge weight in selected vertices ?
Show older comments
Dear All:
My question should be easy but I've confused about it. For example I have matrix which indicate the weight between nodes.
edgeMarix = [
0 1 0 2 0 4
1 0 7 9 3 0
0 7 0 9 0 5
2 9 9 0 3 0
0 3 0 3 0 5
4 0 5 0 5 0
now I want to calculate the sum of edges in 2(or n) different group in such a way for example first Partition is nodes 1,3,4 and the other is 2,5,6 So obviously with respect to given matrix the total edge of first group should be : (1,3)+(1,4)+(3,4) = 0 + 2 + 9 = 11 and second one (2,5)+(2,6)+(5,6) = 3 + 0 + 5 = 8
so how I could do it in matlab :)? thanks in advance
Accepted Answer
More Answers (1)
Roger Stafford
on 17 May 2014
Edited: Roger Stafford
on 17 May 2014
% Define a group
G = [1,3,4];
% calculate the weight between nodes
C = nchoosek(G,2);
W = sum(edgeMarix(sub2ind(size(C),C(:,1),C(:,2))));
2 Comments
Roger Stafford
on 17 May 2014
Just put above code in an appropriate for-loop.
% Let each row of G be a group of indices for 'edgeMarix'.
n = size(G,1);
W = zeros(n,1);
for k = 1:n;
C = nchoosek(G(k,:),2);
W(k) = sum(edgeMarix(sub2ind(size(C),C(:,1),C(:,2))));
end
This assumes all groups in G have the same number of elements. Otherwise you should store them in a cell array and make the obvious changes in the above code.
Categories
Find more on Object Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!