How can I compile the intermediates of a while loop into a vector
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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.
0 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!