I need to make a long code that already has loops loop.

1 view (last 30 days)
%I need everything inside the loop (for=k1:L ) to loop L amount of times.
%Each loop should create a new startingpos and do new calculations but it
%does not. The answer (Finalprob) should be an average of all of the loops
%that find probability. It should be average probability, however right now
%it just reprints one value of (probu).
L=10000
for k=1:L
prime=primes(250); %prime numbers less than 250
Prob_prime=0.85; %setup for prime probability
Prob_other=0.3;
startingpos=randi(250,1,1) %makes a random number for position between 0-250. START
position=startingpos;
for f=2:15 %for loop for all moves after the first position.
r=randi(2,1,1);
if (position(f-1)==1) %makes sure that it cannot go to 0
position(f)=2;
elseif (position(f-1)==250) %makes sure it cant go over 250
position(f)=249;
elseif (r==1) %makes a 50% chance that it counts up or down.
position(f)=position(f-1)+1;
else
position(f)=position(f-1)-1;
x1=Prob_prime*position(f-1);
x2=Prob_other*position(f-1);
d1=position(f-1)-x1;
d2=position(f-1)-x2;
end
end
disp('Position = ');
disp(position);
%plot(1:length(position),position)
%xlabel('time')
%ylabel('Position')
probability=abs(startingpos-position(length(position)))/15*100
%%
count=0
TF=isprime(position);
B=nnz(TF(1,:)==1);
if numel(B)>=4 && numel(B)<=9
count=count+1;
end
probu=sum(probability);
FinalProb=probu/L;
end
disp(FinalProb)

Answers (0)

Categories

Find more on Multidimensional Arrays 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!