MATLAB: I need to display the max value in this function.

functionMATLAB

I have been working on a script for a function 'collatz' and I originally made it way more complex than it needed to be. I've managed to simplify it down to this:
function [s,m]=collatz(num)
cnt=0;
m=-inf;
while num>1
cnt=cnt+1;
if mod(num,2);
num=num*3+1;
else
num=num/2;
end
end
The only problem I have is getting 'm' to represent the maximum value. The 's' displays the number of steps it takes to complete the while loop. That part of the script works fine. Example of what I would need: >>[s,m]=collatz(3) %which would take 7 steps in my function with a max value of 16. s= 7 m= 16

Best Answer

function [s,m]=collatz(num)
cnt=0;
X = zeros([],1) ;
while num>1
cnt=cnt+1;
if mod(num,2)
num=num*3+1;
X(cnt) = num ;
else
num=num/2;
X(cnt) = num ;
end
end
m = max(X) ;
fprintf('Maximum values is:%f\n',m)