sum by recursion without using sum of Matlab

Question: "Write a function with header [S] = mySum(A) where A is a one-dimensional array, and S is the sum of all the elements of A. You can use recursion or iteration to solve the problem, but do not use MATLAB’s function sum." Answer (there is error)
function [S] = mySum(A)
% this program is the sum of all the elements of A
%A is a one dimention array
%get A
[n]=size(A);
if n==1
% base case.
S= A(1);
elseif n==2
%base case
S=A(1) + A(2);
else
%recusrive step
S = mySum(A(n-1)) + mySum(A(n-2));
end
end
Where is the error?

2 件のコメント

Steven Lord
Steven Lord 2016 年 9 月 3 日
The size function, with one output, returns a vector with ndims(A) elements. When you use if (or elseif) with a nonempty vector in the condition, the body of the if or elseif statement is only executed if ALL the elements of the vector are nonzero.
Tho Gonzalez
Tho Gonzalez 2016 年 9 月 4 日
I c

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

 採用された回答

Star Strider
Star Strider 2016 年 9 月 3 日

2 投票

You are making this much more difficult than it needs to be. Think about how you would manually sum a vector. Then write that program in MATLAB.
So if the vector is:
v = [5 7 1 2 9];
in the first iteration, the sum ‘S’ is equal to ‘v(1)’. In the second iteration the sum ‘S’ is equal to ‘S+v(2)’, the third iteration ‘S+v(3)’, and so to the end of the vector.
I’ll let you take it from there.

5 件のコメント

Tho Gonzalez
Tho Gonzalez 2016 年 9 月 3 日
function [S] = mySum(A)
% this program is the sum of all the elements of A %A is a one dimention array %get A [n]=size(A); if n==1 % base case. S= A(1);
else %recusrive step S = A(1) + mySum(n); % it say right here out of memory, I uderstand the manual sum end end
Star Strider
Star Strider 2016 年 9 月 3 日
I was going for something like this, as I initially described:
function S = mySum(A)
Alen = length(A); % ‘A’ Is A Vector, So ‘length’ or ‘numel’ Both Work
S = 0; % Initialise Sum ‘S’
for k1 = 1:Alen
S = S + A(k1); % Loop Through The Elements Of ‘A’
end
Change it as necessary to meet your requirements.
Tho Gonzalez
Tho Gonzalez 2016 年 9 月 4 日
Oh, I see, thank you so much, I should the n= length(A) instead of size (A), the code work already. thank you, I really appreciate this
Star Strider
Star Strider 2016 年 9 月 4 日
My pleasure.
Image Analyst
Image Analyst 2016 年 9 月 4 日
Or you could use numel():
Alen = numel(A);
The advantage of that is that now A is not restricted to 1-D vectors. With length, it will not work with 2-D or higher dimensional arrays. But numel() with let this function work with any dimension of array A. It usually doesn't hurt to be more flexible and robust, though if you need to warn the user if they don't pass in a vector, you can use the ndim() and warndlg() functions.

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

その他の回答 (1 件)

jJnte
jJnte 2017 年 4 月 11 日
編集済み: jJnte 2017 年 4 月 11 日

1 投票

I will give my input for this problem. Me like others who searched on this problem might wanna get an easy overlook of the answer.
My assignment was to write a function which sums all the elements in a vector, with recursion. I want to add that I'm a beginner so don't put too much faith in my coding.
This is how I solved it:
function S = mySum(A)
n = 1:length(A);
if n == 1 % Base case
S = A(1);
else
Last_term = A(end);
Term_before_last_term = mySum(A(1:end-1)); % Recursive step
S = Last_term + Term_before_last_term;
end
end
If someone with greater knowledge than me would be inclined to criticize this, I gladly hear it.

質問済み:

2016 年 9 月 3 日

編集済み:

2017 年 4 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by