How do I make the output of a loop the new input value, and loop it 15 times.

2 Ansichten (letzte 30 Tage)
%SITUATION.
%straight line of squares, 1-250. you start at random point.
%move left or right (50 50 chance).
%If on 1, only move to 2. If on 250, only move to 249.
%Look at NEW number, and bleat or not.
%If on prime number, 'bleat' 85% of the time.
%If not prime, 'bleat' 30% of the time
%PROBABILITY that in a 15 move sequence, 'bleat' 4<times<9 times in a row.
%Probability should converge to approx 19%.
%% QUESTIONS
%How do I the loop movement with FrstMove being the new starting point for 15
%times? Tried setting FirstMove=start but it didnt work.
%What command do I use to loop this so it moves 15 times.
%% Making the first move.
Bleat=0;
i=1e6
for N=1:i;
for FirstTile= randi(250,1,1) %for a random starting tile
start=FirstTile;
G=rand;
if G>0.5;
FrstMove=start+1 %moves left or right 50% chance
else
FrstMove=start-1
end
if start==1; %to ensure that 1 can only move to 2
FrstMove=2
end
if start==250; %to ensure that 250 can only move to 249
FrstMove=249
end
FrstMove=start %does this override the start value, so the next time it loops FrstMove is the new starting tile.
%need to somehow make FrstMove the new start and loop it for 15
%times.
%% Bleat or not
H=rand;
if ismember(FrstMove,[2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 233 239 241]) && H<=0.85; %prime number (?)
Bleat=Bleat+1 %bleats 85% of the time if prime.
elseif not(ismember(FrstMove,[2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 233 239 241])) && H<=0.3;
Bleat=Bleat+1 %bleats 30% of the time if NOT prime.
end
%% Probability
True=0;
if Bleat>4 && Bleat<9; %how can this line be written so it counts 4<x<9 sequential bleats instead.
True=True+1;
end
ProbTrue=True/i %number of trials the 15 move sequence has been executed and the condition is met.
end
end

Antworten (1)

Prahlad Gowtham Katte
Prahlad Gowtham Katte am 16 Feb. 2022
Hello,
I understood that you want to know how to pass the FrstMove to start for 15 iterations.In the code you have tried to assign the start to FrstMove but you need to assign FrstMove to start i.e.
start=FrstMove;
If you need to generate new start using random function after every 15 iterations you can add the following code snippet.
if mod(iteration_number,15)==1
start=randi(250,1,1);
else
start=FrstMove;
end
Hope it resolves the problem.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by