Folks, please let me know how to complete the sum of products in the "for loop" as shown below in order to get the correct answer. Thank you. HDaniel

1 回表示 (過去 30 日間)
clear all; % S=2i*3j; S1=0; S2=0; for i=0:1:2; for j=1:1:3; S1=2*i; S2=3*j; S=S1*S2; end end y=Sum(S)

採用された回答

aborghes
aborghes 2017 年 8 月 9 日
編集済み: aborghes 2017 年 8 月 9 日
Hi Hani,
A few tips first:
  • When incrementing by one, you can simply do i=0:2, the increment value is only necessary if it is something other than 1
  • You can simplify your code to do all the calculations on one line
  • It is simpler to just add to S every loop iteration.
Doing as i suggest, the code looks as follows:
clear all;
% S=2i*3j;
S = 0;
for i=0:2
for j=1:3
S= S + 2*i * 3*j;
end
end
y=S
  2 件のコメント
hani daniel
hani daniel 2017 年 8 月 10 日
Hi Andrei, Thank you very much. Hani Daniel
hani daniel
hani daniel 2017 年 8 月 10 日
Thank you very much for your help. Hani Daniel

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2017 年 8 月 9 日
k = (0:2)'*(1:3)*6;
S = sum(k(:));

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by