How to use for loop structure to calculate two vectors to dot product

23 ビュー (過去 30 日間)
Shuming Ye
Shuming Ye 2021 年 2 月 20 日
コメント済み: Matt J 2021 年 2 月 20 日
How to use for loop structure to calculate two vectors to dot product.
Please give an example.

回答 (1 件)

William
William 2021 年 2 月 20 日
a = 1:5;
b = 6:10;
c = 0;
for j = 1:5
c = c + a(j)*b(j);
end
However, this calculation can be done more simply with the dot() function:
c = dot(a,b);
or with the expression:
c = sum(a.*b);

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by