필터 지우기
필터 지우기

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 CenterFile Exchange에서 Creating 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