Sum of all even index elements of a 1D array
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Rik
le 8 Mai 2023
Is there a reason you are using a recursive function? Is this because it is homework, or was this your own idea?
Réponse acceptée
Stephen23
le 8 Mai 2023
Modifié(e) : Stephen23
le 8 Mai 2023
"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:
- n==5 (user call)
- n==3 (recursive call)
- 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)
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 commentaires
Rik
le 8 Mai 2023
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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!