I dont know how to script this operation.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
James T
am 4 Sep. 2017
Bearbeitet: James T
am 6 Sep. 2017
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
2 Kommentare
Akzeptierte Antwort
Wonsang You
am 4 Sep. 2017
Bearbeitet: Wonsang You
am 4 Sep. 2017
Try the following code.
function s = sumUp(X)
s = 0;
for s=1:length(X)
s = s + X(s);
end
end
3 Kommentare
Image Analyst
am 5 Sep. 2017
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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!