I dont know how to script this operation.

1 回表示 (過去 30 日間)
James T
James T 2017 年 9 月 4 日
編集済み: James T 2017 年 9 月 6 日
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 件のコメント
Image Analyst
Image Analyst 2017 年 9 月 4 日
>> doc for
James T
James T 2017 年 9 月 5 日
Thank you, for that. One of the examples shown solved it, but I need to clean it up a bit.

サインインしてコメントする。

採用された回答

Wonsang You
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
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
James T
James T 2017 年 9 月 6 日
編集済み: James T 2017 年 9 月 6 日
I discovered it was the right script, but a few letters needed to be changed inside the statement after looking at the documents and noticing one of them used a different identifier. I am new to this style of scripting, but it's been fun so far! Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by