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 日間)
Tony Phan
Tony Phan 2018 年 2 月 7 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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

回答 (1 件)

Prajith Chilummula
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')

Community Treasure Hunt

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

Start Hunting!

Translated by