I am trying to keep track of how many times the function call itself. I have tried setting up num as 0 and putting num = num+1 at the end but I keep getting 1. How do I fix this?
function [out num] = collatz(val)
num = 0;
if val == 1
out = 1;
elseif mod(val, 2) == 0
out = collatz(val/2);
else
out = collatz(3*val+1);
end
num = num+1;
end
Thank you.