MATLAB Answers

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

1 view (last 30 days)
Petch Anuwutthinawin
Petch Anuwutthinawin on 20 Jul 2021
Answered: VBBV on 20 Jul 2021
%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 (1)

VBBV
VBBV on 20 Jul 2021
%f true
FinalProb(k) = probu/L
Change this line in your code to view probability of all loops

Community Treasure Hunt

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

Start Hunting!