Borrar filtros
Borrar filtros

Sum of all even index elements of a 1D array

5 visualizaciones (últimos 30 días)
Camden Nelson
Camden Nelson el 8 de Mayo de 2023
Comentada: Rik el 8 de Mayo de 2023
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 comentario
Rik
Rik el 8 de Mayo de 2023
Is there a reason you are using a recursive function? Is this because it is homework, or was this your own idea?

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 8 de Mayo de 2023
Editada: Stephen23 el 8 de Mayo de 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:
  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 comentarios
Camden Nelson
Camden Nelson el 8 de Mayo de 2023
This is makes much more sense to me now. Thank you!
Rik
Rik el 8 de Mayo de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by