How to use for loop structure to calculate two vectors to dot product
7 次查看(过去 30 天)
显示 更早的评论
How to use for loop structure to calculate two vectors to dot product.
Please give an example.
0 个评论
回答(1 个)
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 Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!