フィルターのクリア

How can I solve this problem using for loop?

2 ビュー (過去 30 日間)
Manav Divekar
Manav Divekar 2021 年 11 月 10 日
回答済み: Emmanuel 2024 年 1 月 23 日
for the given vector [2 2 5 8], without using sum() and diff() how can i perform 2*2 + 2*5 + 5*8 = 54. Using for loop. here the consicutive number are multiplied and then addition is performed.

回答 (2 件)

Matt J
Matt J 2021 年 11 月 11 日
v=[2 2 5 8];
for i=1
result=v(1:end-1)*v(2:end).'
end
result = 54
  7 件のコメント
Manav Divekar
Manav Divekar 2021 年 11 月 11 日
this is giving a matrix, not the summation.
Matt J
Matt J 2021 年 11 月 11 日
編集済み: Matt J 2021 年 11 月 11 日
I demonstrated to you in my original answer that it does give the summation. This is assuming the vector is a row vector, which it was in your original post.

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


Emmanuel
Emmanuel 2024 年 1 月 23 日
total = 0;
x = [2,2,5,8];
n = length(x);
for i =1:n-1
total = total + x(i)*x(i+1);
end
disp(total)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by