Recursive nested function gives different result on first run
Mostrar comentarios más antiguos
I have a recursive function that is nested in my main function and when assigning its output to some variable it gives a different result (for certain inputs). Here's an example showing the behaviour
function arr = foo( )
% clear all % makes it give the same (possibly wrong) result every time
function arrB = foorecurse( currIds )
disp(currIds) % correct
disp(cellfoo{currIds(1)}) % correct
fprintf('---------\n')
if length(currIds) == 1
arrB = cellfoo{currIds}
else
arrB = [cellfoo{currIds(1)} ; foorecurse(currIds(2:end))]
end
end
cellfoo = { eye(2), [0 1 ; 1 0], [0 -i ; i 0], [1 0 ; 0 -1] };
% foorecurse([2 3 2]) % not assigning the output to any variable "fixes" it
arr = foorecurse([2 3 2]) % gives a column of cellfoo{[3 3 2]} first time it's run
% arr = foorecurse([2 1 2]) % this is fine
end
I have not tested it on another machine. I am at a total loss as to what could be happening. I know there's a way to achieve the same thing in many other ways (and they work) but for the sake of understanding what ML does, can anyone figure this out?
1 comentario
Matt J
el 19 de Feb. de 2017
Bizarre!
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!