How to sum values from a cycle

Hello, I have a loop, and in every cycle it gives me an angle, I would like to sum this angle. For example I get 30 in first loop and 25 in another so I would like the code gives me 55.
Thanks for helping.
for x = 1:h
y=x+1;
Salfa=[htabulka(x,3),htabulka(x,4)]; %střed
b1=[htabulka(x,1),htabulka(x,2)]; %bod 1
b2=[htabulka(y,1),htabulka(y,2)]; %bod 2
ang(x) = atan2(abs(det([b2-Salfa;b1-Salfa])),dot(b2-Salfa,b1-Salfa))
end

回答 (2 件)

Matt
Matt 2023 年 3 月 9 日

1 投票

I think you are looking for this https://fr.mathworks.com/help/matlab/ref/sum.html
angle = [30 25]; % 2 angles
sum(angle)
You can also do it manually to understand the logic :
angle = rand(1,10); % 10 random angles
my_sum = 0;
for ii=1:length(angle)
my_sum = my_sum + angle(ii);
end
Voss
Voss 2023 年 3 月 9 日

0 投票

After the loop:
ang_sum = sum(ang);

3 件のコメント

Eliska Paulikova
Eliska Paulikova 2023 年 3 月 9 日
Thank you
Voss
Voss 2023 年 3 月 9 日
You're welcome!
Voss
Voss 2023 年 3 月 9 日
Make sure you have this in the loop, like you showed in the code in the question:
ang(x) = atan2(...)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2022b

タグ

質問済み:

2023 年 3 月 9 日

コメント済み:

2023 年 3 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by