Info
This question is closed. Reopen it to edit or answer.
I have a script already done I just need to add to the code so that i can run the code and store the index of the first 1 that occurs in a vector g. Can anyone help me add to the code to implement it?
3 views (last 30 days)
Show older comments
for m = 1:200
x = zeros(200,1);
x(m) = 1;
x(1) = input('Enter initial positive integer:');
k = 1;
while ((x(k) ~= 1) && (k < 200))
if rem(x(k),2) == 0
x(k+1) = x(k)/2;
else
x(k+1) = 3*x(k)+1;
end
k = k+1;
end
n = k;
x = x(1:n);
clc
disp(sprintf('x(1:%1.0f) = \n',n))
disp(sprintf('%-8.0f',x))
[xmax,imax] = max(x);
disp(sprintf('\n x(%1.0f) = %1.0f is the max.',imax,xmax))
density = sum(x<=x(1))/x(1);
disp(sprintf(' The density is %5.3f.',density))
close all
figure
plot(x)
title(sprintf('x(1) = %1.0f, n = %1.0f',x(1),n));
figure
plot(-sort(-x))
title('Sequence values sorted.')
I = find(rem(x(1:n-1),2)==1);
if length(I)>1
figure
plot((1:n),zeros(1,n),I+1,x(I+1),I+1,x(I+1),'*')
title('Local Maxima')
end
0 Comments
Answers (1)
Prajith Chilummula
on 23 Feb 2018
I understood that you want to find the first occurrence of 1 in a vector.Can you try this:
index=find(vector==1,1,'first')
0 Comments
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!