MATLAB: Get while loop answer as a vector

vectorwhile loop

While loop code is below. The answer I get for n=6 is n=6, n=3, n=10, etc. However, I want those answers to be listed as a vector so I can get its length. Thank you
while n>1
if mod(n,2)==0
n=n/2
else
n=3*n+1
end
end

Best Answer

n = 12;
while n>1
if mod(n(end),2)==0
n(end+1)=n(end)/2;
else
n(end+1)=3*n(end)+1;
end
end
creates this vector:
>> n
n =
12 6 3 10 5 16 8 4 2 1
Note that this is not a very good way to write MATLAB code, because it expands the array in each loop iteration (clearly highlighted with a warning in the MATLAB editor!):