How to extract all the values from a while loop into a vector
Mostra commenti meno recenti
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?
Risposta accettata
Più risposte (1)
Andrei Bobrov
il 28 Apr 2014
Modificato: Andrei Bobrov
il 28 Apr 2014
function c = nice(n)
c = n;
jj = 0;
while c(end) ~= 1
if rem(c(end),2) == 0
h = c(end)/2;
else
h = 3*c(end)+1;
end
c = [c;h];
jj = jj + 1;
if jj >= 30, break; end
end
Categorie
Scopri di più su Functions in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!