I dont know how to script this operation.
조회 수: 1 (최근 30일)
이전 댓글 표시
function s = sumUp(X)
%Simple function in MATLAB.
% s=sumUp(x) is a function that returns s given s=Sigma(X(i)).
% Caution! X here is a vector: X=[ x1 x2 ... xn].
s = 0;
% ============= YOUR CODE HERE ==============
% Instructions: Return s given s=Sigma(X(i)), X is a vector.
% Use built-in function length(X) to obtain the length of X.
% Use for loop to sum up all elements of X.
% ===========================================
end
채택된 답변
Wonsang You
2017년 9월 4일
편집: Wonsang You
2017년 9월 4일
Try the following code.
function s = sumUp(X)
s = 0;
for s=1:length(X)
s = s + X(s);
end
end
댓글 수: 3
Image Analyst
2017년 9월 5일
He should not have had s be both the accumulating variable and the loop index. It should be:
function s = sumUp(X)
s = 0;
for k = 1 : length(X)
s = s + X(k);
end
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!