How to use a for loop to calculate the sum of a row vector?
조회 수: 69 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
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
추가 답변 (1개)
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
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 Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!