Sum of all even index elements of a 1D array

5 ビュー (過去 30 日間)
Camden Nelson
Camden Nelson 2023 年 5 月 8 日
コメント済み: Rik 2023 年 5 月 8 日
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 Center および File ExchangeGet Started with MATLAB についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by