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

7 vues (au cours des 30 derniers jours)
Petch Anuwutthinawin
Petch Anuwutthinawin le 20 Juil 2021
Modifié(e) : Vidhi Agarwal le 4 Déc 2024 à 5:46
%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)

Réponses (1)

Vidhi Agarwal
Vidhi Agarwal le 4 Déc 2024 à 5:46
Modifié(e) : Vidhi Agarwal le 4 Déc 2024 à 5:46
I understand you are attempting to work with a piece of code that contains nested loops, and based on the code provided, you are attempting to calculate the average probability over a number of iterations. You want to make sure that these loops operate appropriately across a number of iterations.
To achieve the same you might need to address below given modifications:
  • Ensure that the probability calculations are done correctly and accumulated over each iteration.
  • Ensure that variables are properly initialized and updated within the loop.
  • Compute the average probability after all iterations.
The modified code for the same is given below:
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); % Random starting position between 1 and 250
position = zeros(1, 15); % Preallocate the position array for efficiency
position(1) = startingpos; % Set the first position
for f = 2:15 % Loop for moves after the first position
r = randi(2, 1, 1);
if position(f-1) == 1
position(f) = 2;
elseif position(f-1) == 250
position(f) = 249;
elseif r == 1
position(f) = position(f-1) + 1;
else
position(f) = position(f-1) - 1;
end
% Calculate probabilities
x1 = Prob_prime * position(f-1);
x2 = Prob_other * position(f-1);
d1 = position(f-1) - x1;
d2 = position(f-1) - x2;
end
% Calculate probability of ending position
probability = abs(startingpos - position(end)) / 15 * 100;
% Accumulate the probability
totalProbability = totalProbability + probability;
end
% Compute the average probability
FinalProb = totalProbability / L;
Hope this helps!

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by