MATLAB: How to extract all the values from a while loop into a vector

MATLABvectorwhile loop data

I have the following function
function c = nice(n)
c = n
while c ~= 1
if rem(c,2) == 0
c = c/2
else c = 3*c+1
end
end
and I am trying to insert all the c values into one vector. Any hints on how can I do it?

Best Answer

AC, use something like
function c = nice(n)
ii = 1;
c(ii) = n;
while c(ii) ~= 1
ii = ii + 1;
if rem(c(ii-1),2) == 0
c(ii) = c(ii-1)/2;
else
c(ii) = 3*c(ii-1) + 1;
end
end
end