Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Torsten
Torsten am 7 Sep. 2018
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

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by