How can I compile the intermediates of a while loop into a vector

1 view (last 30 days)
I want to be able to graph the values of all the intermediates, but x is overwritten each time the loop is run. Here's what I have so far.
index = 0; %Counter
while x>1 %continues until x=0
if mod(x,2)==0; %if even
x = x/2;
else %if odd
x = 3 * x +1;
end
index = index + 1; %counts how many times for loop runs
end

Accepted Answer

Walter Roberson
Walter Roberson on 30 Nov 2015
After the
index = index + 1
line, add
xlist(index) = x;
then after the loop, xlist will be the list of the x values.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!