Plotting values from a set of nested if-else statements in while loop nested in a for loop

조회 수: 4 (최근 30일)
The code below produces a plot 1000 by 1000 and my goal is to plot each value of length after it iterates through the while loop. what ends up happening here is my function only returns the last value of length.
the purpose of the function is to retrieve to length of each sequence: s(n) = s(n-1)/2 if s(n-1) > 1 and even
s(n) = 3*s(n-1) + 1 if s(n-1) > 1 and odd
If i remove the comments i can loop through 1000 times and have the value of length printed each time, but i need to store that value in order to plot it(i think).
Any help would be appreciated
%%%%%%%%%%%%%%%%%%%%%%
v = 1:1000;
plot(v,xyz(v),'r+-');
%axes' bounds and labels
axis([0 1000 0 1000]);
xlabel('s(x)');
ylabel('Length of s(x)');
%Title
title('XYZ Sequence');
function [length] = xyz(v)
your_int = 1;
count = 1;
for x_= v
length = 1;
your_int = your_int * count;
while( your_int ~= 1) %length of sequence is retrieved when your_int = 1
%fprintf("\nYour integer = %10d", your_int);
%un_comment the line above to see each number in the sequence
if( mod(your_int, 2) == 0)
your_int = your_int / 2; %s(n) = s(n-1)/2 if s(n-1) > 1 and even
else
your_int = 3*your_int + 1; %s(n) = 3s(n-1) + 1 if s(n-1) > 1 and odd
end
length = length + 1; %length of the sequence
end
count = count + 1; %multiply by your_int to increment
%fprintf("\n\tThe length of s(%4d) is %4d", (count-1), length);
%un_comment the line above to see the value of length after each for loop iteration
end
end

채택된 답변

Jyotsna Talluri
Jyotsna Talluri 2019년 9월 30일
Consider an array inside the function and append the length each time to that array in a for loop ..k is the array considered
function k = xyz(v)
your_int = 1;
count = 1;
k=[];
for x_= v
length = 1;
your_int = your_int * count;
while( your_int ~= 1)
if( mod(your_int, 2) == 0)
your_int = your_int / 2;
else
your_int = 3*your_int + 1;
end
length = length + 1;
end
k=[k length];
count = count + 1;
end
end
  댓글 수: 2
Nathan Preslar
Nathan Preslar 2019년 9월 30일
Thank you so much! I knew it was some small change like that. I had beed putting that array inside the function within the loops, so I kept getting 112 - the last number. Thank you so much.
Stephen23
Stephen23 2019년 9월 30일
편집: Stephen23 2019년 9월 30일
"...and append the length each time to that array in a for loop"
The MATLAB documentation recommends preallocating arrays before the loop:
Note also that the code shadows the inbuilt length function.
Note that the code alignment is very inconsistent: irregular code alignment is one way that beginners hide basic bugs in their code. You should use the default alignment of the MATLAB Editor: select all code text, press ctrl+i.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2019년 9월 30일
편집: Stephen23 2019년 9월 30일
An improved version of your script, without the poor practices shown in Jyotsna Talluri's answer:
V = 1:1000;
N = numel(V);
L = ones(1,N); % preallocate output array.
for k = 1:N
s = V(k);
while s>1
if mod(s,2) % odd
s = 3*s + 1;
else % even
s = s/2;
end
L(k) = L(k)+1;
end
end
Giving:
>> L(1:10)
ans =
1 2 8 3 6 9 17 4 20 7

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by