Summation with Nested For Loop

9 ビュー (過去 30 日間)
Umer Khalid
Umer Khalid 2022 年 4 月 27 日
コメント済み: Umer Khalid 2022 年 4 月 27 日
Hi,
I am trying to perform summation using for loop, where I have to change two different variables, theta and theta_m. Thetha_m has to run for 5 iterations (values given) and Theta has to run for 19 iterations. However, at the end I am getting same numbers in whole array. I think I am doing something wrong at the end. Can someone please look at the equation and the code and point out what I am doing wrong?
I have to store these 19 values somewhere for further operation on another formula.
Any help or assistance will be highly appreciated.
Thank you.
theta_doa = 0:20:360;
theta_m = [0 60 144 216 288];
k=1:19;
for i=1:19
j=0;
c_square = 0;
for j=1:5
c_eqn = cosd(theta_doa(i)-theta_m(j)).^2 % internal equation for c_square
c_square = c_square + c_eqn
end
c_square(k)=c_square;
end

採用された回答

Torsten
Torsten 2022 年 4 月 27 日
編集済み: Torsten 2022 年 4 月 27 日
theta_doa = 0:20:360;
theta_m = [0 60 144 216 288];
c_square = zeros(1,numel(theta_doa));
for i = 1:numel(theta_doa)
for j = 1:numel(theta_m)
c_square(i) = c_square(i) + (cosd(theta_doa(i)-theta_m(j)))^2 ;
end
end
  1 件のコメント
Umer Khalid
Umer Khalid 2022 年 4 月 27 日
@Torsten thank you so much it worked.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by