Info
此问题已关闭。 请重新打开它进行编辑或回答。
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?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Prajith Chilummula
2018-2-23
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 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!