Difficulties exceeding array limit with random walk
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I need to have multiple simulations run of the 2500 step program but if I try to do more than that, it reverts to zero. What can i do to get a proper histogram?
clc;
clear;
%% First we need an area to define our items
% Define the larger collection, so what we are going to be doing, commas
% seperate each "run"
Ensembles = [2550,5500,7500];
StepsT = 2500;
%% For the greater if-else statement within a foor loop
% Create a greater for statement in order to do each if else statement 7
% times
for Runs = 1:length(Ensembles);
%% Determining the Ensembles and all
% We need to define the final position since that is what we are
% tracking
P = zeros(1,Ensembles(Runs)); %The zeros is to make an empty matrix for us to add to
% Then the procedure will run for however much we want it to which is
% length of the particular simulation
FP = zeros(1,Ensembles(Runs));
for IRun = 1:Ensembles(Runs)
% Now we just reiterate the last program but I wont use cumsum this
% time for 1 easy program. I tried to do it but it hurts my head
NP = 0;
Walk = rand(1,StepsT);
for Steps = 1:StepsT
if Walk(Steps) < 0.5
NP = -1;
elseif Walk(Steps) > 0.5
NP = 1;
end
if Steps == 1
P(Steps) = NP;
else
P(Steps) = NP + P(Steps-1);
end
%% CAPTURE TIME!
%ok so we are going to try a captured thing because thats a good
%thing
if P(Steps) >= 25 & P(Steps) <= 50
dwell = dwell + 1; %We are going to add one for each time we are in this area
else
dwell = 0; %We will break the streak when we "escape"
end
%Now we need to break the set once they get "caught"
if dwell >= 3
break
end
end
FP(IRun)=P(end);
% Last position of the walker which will then be added to the
% this empty matrix
end
%% Plotting each histogram
% Thankfully we can now plot the histogram with our awesome data and the
% 50 is the bin size of the last one, but we will change it to 35 since
% the numbers should be a bit smaller
figure,hist(FP,35),
% Then we will use num2str in order to display what we want for each
% histogram
title([num2str(Ensembles(Runs)),' steps taken in our random walk'])
end
0 commentaires
Réponses (1)
Walter Roberson
le 22 Avr 2020
Ensembles = [2550,5500,7500];
StepsT = 2500;
You fix the number of steps at 2500 no matter what the size of ensemble.
P = zeros(1,Ensembles(Runs)); %The zeros is to make an empty matrix for us to add to
You initialize P according to the size of the ensemble. At this point, P(end) is 0.
for Steps = 1:StepsT
You take 2500 steps no matter what the size of ensemble.
P(Steps) = ...
You assign into P according to the current step. As Steps is at most 2500, the highest you will assign is P(2500)
FP(IRun)=P(end);
You extract the last entry in P, no matter how many steps you took. But P is the length of the ensemble, not the number of steps. When you took fewer steps than the length of your ensemble, then P(end) would not have been written to, and so will still be zero.
0 commentaires
Voir également
Catégories
En savoir plus sur Dimensionality Reduction and Feature Extraction 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!