How can I sum edge weight in selected vertices ?
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
the cyclist
le 17 Mai 2014
Modifié(e) : the cyclist
le 17 Mai 2014
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];
partition = {[1 3 4],[2 5 6]};
numberPartitions = numel(partition);
s = cell(1,numberPartitions);
for np = 1:numberPartitions
s{np} = 0;
currentPartition = partition{np};
numberIndices = numel(currentPartition);
for i1 = 1:numberIndices
for i2=i1:numberIndices
s{np} = s{np} + edgeMarix(currentPartition(i1),currentPartition(i2));
end
end
end
Plus de réponses (1)
Roger Stafford
le 17 Mai 2014
Modifié(e) : Roger Stafford
le 17 Mai 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 commentaires
Roger Stafford
le 17 Mai 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.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!