필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Gokhan Kayan
Gokhan Kayan 2018년 9월 7일
댓글: Gokhan Kayan 2018년 9월 7일
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에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by