필터 지우기
필터 지우기

How to use a for loop to calculate the sum of a row vector?

조회 수: 69 (최근 30일)
Michelle S
Michelle S 2015년 11월 25일
댓글: Yadu Bhusal 2021년 8월 14일
I have the values x-[1, 23, 43, 72, 87, 56, 98, 33] How do you calculate the sum (which should equal 413) using a for loop?

채택된 답변

Stalin Samuel
Stalin Samuel 2015년 11월 25일
편집: Stephen23 2018년 12월 21일
x = [1, 23, 43, 72, 87, 56, 98, 33] ;
y = 0;
for n = 1:length(x)
y = y + x(n);
end
  댓글 수: 8
Christopher Vargas
Christopher Vargas 2020년 4월 6일
disp(y) %this will display the result
Lexi Finke
Lexi Finke 2020년 10월 5일
how do you make this loop into a function??

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Aza Xongo
Aza Xongo 2019년 4월 16일
편집: Aza Xongo 2019년 4월 16일
Write a program (using loops) that calculates and displays the sum of all the elements of x=[2 -5 6 7; -4 8 -5 6];(its a question)
  댓글 수: 1
Yadu Bhusal
Yadu Bhusal 2021년 8월 14일
X =['enter an array>']; m = length(x); Sum = 0; For i = 1:m Sum = sum + X(i); end Disp(sum)
IF you want single sum. It you wish iterations remove semicolon from end of X(i).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by