calculating sum of an array by using if-or statement in matlab

5 次查看(过去 30 天)
Hi, I want to calculate the sum of some array elements in matlab by using if-or statement or by using any other code. My inputs are shown in below.
Deger Node1 Node2
20 1 2
25 2 5
27 5 6
28 6 7
31 7 4
32 4 3
33 3 2
34 3 6
I want to identify same values (max=7) in node1 and node 2 and calculate the sum of Deger values corresponding to that values. For example for 1, we have only 20 for değer. so value=20. For 2, we have 3 values 20,25,33. so the sum is 78. For 3, we have 32,33,34 so value=32+33+34=99. How can I wrote this code in matlab ? Thanks for your help.

采纳的回答

Torsten
Torsten 2018-9-7
Deger = [20 25 27 28 31 32 33 34];
Node1 = [1 2 5 6 7 4 3 3];
Node2 = [2 5 6 7 4 3 2 6];
minimum = min(min(Node1),min(Node2))
maximum = max(max(Node1),max(Node2))
result = NaN(maximum-minimum+1,1);
for i=minimum:maximum
result(i+1-minimum) = sum(Deger(find(Node1==i|Node2==i)));
end
result

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by