Sum of all even index elements of a 1D array

5 次查看(过去 30 天)
This is the code that I have so far to sum all the even index elements of a 1D array:
function [out] = mySumEven(A)
n = length(A);
if n == 1
disp('No numbers in even positions'); % out = 'No numbers in even positions';
out = 0; % added
elseif n == 2
out = A(2);
else
out = A(2) + mySumEven(A(3:n));
end
end
My first question is as follows: Why does it output 'No numbers in even positions' when the length of the array is greater than or equal to three and odd?
Secondly, the problem explains that the recursion relation will be different if the length of the array is odd versus even. From what I can tell the above code should work regardless, but how would I implement braches to handle when the length of the array is odd or even?
  1 个评论
Rik
Rik 2023-5-8
Is there a reason you are using a recursive function? Is this because it is homework, or was this your own idea?

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2023-5-8
编辑:Stephen23 2023-5-8
"Why does it output 'No numbers in even positions' when the length of the array is greater than or equal to three and odd?"
Because you wrote your code to evalute the n==1 case for every vector with an odd number of elements. Consider some vector, lets say with 5 elements, then these are the three function calls:
  1. n==5 (user call)
  2. n==3 (recursive call)
  3. n==1 (recursive call, prints "no numbers in even positions")
If you remove the semi-colon and print n, you will see that every odd-lengthed vector results in a function call where n==1. Lets try it right now:
mySumEven(1:5)
n = 5
n = 3
n = 1
No numbers in even positions
ans = 6
What does your function do when n==1? (hint: it prints something)
The most important step in debugging code is to actually look at what your code is doing, not just relying on what it is doing in your head. Code does not care what you think it is doing, it is your job to look at what it really is doing.
"the problem explains that the recursion relation will be different if the length of the array is odd versus even. From what I can tell the above code should work regardless, but how would I implement braches to handle when the length of the array is odd or even?"
I also don't see why branching would be required: that seems like the kind of thing that could be trivially achieved using indexing, much like you have done.
Note that to make your code more robust, you should also consider what happens with empty arrays.
function [out] = mySumEven(A)
n = length(A)
if n == 1
disp('No numbers in even positions'); % out = 'No numbers in even positions';
out = 0; % added
elseif n == 2
out = A(2);
else
out = A(2) + mySumEven(A(3:n));
end
end
  6 个评论
Camden Nelson
Camden Nelson 2023-5-8
This is makes much more sense to me now. Thank you!
Rik
Rik 2023-5-8
One of the caveats of recursive functions is that you can quickly run into problems with the maximum function depth:
mymain(1:1e5) % takes 19GB of memory (on my machine)
While a solution using indexing is much faster and has a negligable memory footprint.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by