how to do product summation

37 ビュー (過去 30 日間)
adarsh
adarsh 2018 年 1 月 7 日
編集済み: Jan 2018 年 2 月 22 日
L1=1;L2=2; L3=3; D1=2;D2=3; D3=5;
Then how will i do
C=(L1*D1)+(L2*D2)+(L3*D3)
in MATLAB using programming (similarly i have to do 350 number of L and D). Kindly help me out.
i was trying like this one;
sum=0
for i=1:3
C=Li*Di
end
but couldn't succeed. kindly help
  2 件のコメント
Stephen23
Stephen23 2018 年 1 月 7 日
編集済み: Stephen23 2018 年 1 月 7 日
"similarly i have to do 350 number of L and D"
Then you should put your data into matrices, and so make your task trival. The name MATLAB comes from "MATrix LABoratory", and not from "lets put data into lots of separate numbered variables and make our code slow and complex".
Star Strider
Star Strider 2018 年 1 月 7 日
I intended to imply that in my Answer.

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

採用された回答

Star Strider
Star Strider 2018 年 1 月 7 日
I would create vectors out of the ‘L’ and ‘D’ values, then multiply them using vector/matrix multiplication:
L1=1; L2=2; L3=3; D1=2; D2=3; D3=5;
L = [L1 L2 L3];
D = [D1 D2 D3];
C = L * D';
Note the transpose operator (') for ‘D’. That is necessary to produce the result you want.
  3 件のコメント
Star Strider
Star Strider 2018 年 1 月 8 日
As always, my pleasure.
Jan
Jan 2018 年 2 月 22 日
編集済み: Jan 2018 年 2 月 22 日
@adarsh: Of course it would have been much easier, if you did not create numbers variables from the beginning. It is much easier to work with vectors, matrices and arrays. Please consider Stephen's valuable comment and read the link carefully.

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

その他の回答 (0 件)

カテゴリ

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