How to Subplot in a for loop
14 次查看(过去 30 天)
显示 更早的评论
I need help with making a subplot for the two histograms this code creates
%% Setting Initial Conditions
clc %Clearing the command window.
clear all %Making output nicer.
%0 is a step to the left and 1 is a step to the right.
Left = 0; %Setting the initial left value.
Right = 0; %Setting the initial right value.
%% Creating the Ensemble of Random Walkers
for N = [25000, 100000] %Creating a vector for all the ensemble sizes.
for x = 1:N %Creating an ensemble of N walkers.
for i = 2:2501 %Setting the for loop to run 2500 times.
Walker = -1 + rand(1)*(2); %Setting the walker to be a random number between -1 or 1.
if Walker > 0 %Saying if the walker takes a step to the right.
Left(i) = Left(i-1); %Left number of steps stays the same.
Right(i) = Right(i-1) + Walker; %Right number of steps goes up by walker value.
elseif Walker < 0 %Saying if the walker takes a step to the left.
Left(i) = Left(i-1) + Walker; %Left number of steps goes up by walker value.
Right(i) = Right(i-1); %Right number of steps stay the same.
end %Ending the if statement.
end %Ending the for loop.
location = Right(end) - abs(Left(end)); %Making the location the right number of steps minus the left.
Finallocation(x) = location; %Setting the location to be the end values of the walkers.
end %Ending the for loop.
%% Plotting the Results
figure('Name','Ensemble Histogram') %Labeling the figure appropriately.
histogram(Finallocation,20) %Making a histogram of the final locations.
z1 = sprintf('Ensemble of step size = %d',N); %Making a title that updates as variables are changed.
title(z1) %Making z1 the title.
xlabel('Final Location') %Labeling the x-axis appropriately.
ylabel('Number of Walkers') %Labeling the y-axis appropriately.
end %Ending the for loop.
2 个评论
采纳的回答
Sriram Tadavarty
2020-4-4
Hi Patrick,
You can the following modifications to the code:
1) To plot it inline the for loops, you can use the count temporary variable, bring the figure command before the for loop, as code below (%%% annotated for the comments placed) :
%% Setting Initial Conditions
clc %Clearing the command window.
clear all %Making output nicer.
%0 is a step to the left and 1 is a step to the right.
Left = 0; %Setting the initial left value.
Right = 0; %Setting the initial right value.
count = 1; %%% Initialized count
figure('Name','Ensemble Histogram') %Labeling the figure appropriately. %%% Brought it before for loop
%% Creating the Ensemble of Random Walkers
for N = [25000, 100000] %Creating a vector for all the ensemble sizes.
for x = 1:N %Creating an ensemble of N walkers.
for i = 2:2501 %Setting the for loop to run 2500 times.
Walker = -1 + rand(1)*(2); %Setting the walker to be a random number between -1 or 1.
if Walker > 0 %Saying if the walker takes a step to the right.
Left(i) = Left(i-1); %Left number of steps stays the same.
Right(i) = Right(i-1) + Walker; %Right number of steps goes up by walker value.
elseif Walker < 0 %Saying if the walker takes a step to the left.
Left(i) = Left(i-1) + Walker; %Left number of steps goes up by walker value.
Right(i) = Right(i-1); %Right number of steps stay the same.
end %Ending the if statement.
end %Ending the for loop.
location = Right(end) - abs(Left(end)); %Making the location the right number of steps minus the left.
Finallocation(x) = location; %Setting the location to be the end values of the walkers.
end %Ending the for loop.;
%% Plotting the Results
subplot(2,1,count) %%% Subplot calling
histogram(Finallocation,20) %Making a histogram of the final locations.
z1 = sprintf('Ensemble of step size = %d',N); %Making a title that updates as variables are changed.
title(z1) %Making z1 the title.
xlabel('Final Location') %Labeling the x-axis appropriately.
ylabel('Number of Walkers') %Labeling the y-axis appropriately.
count = count+1; %%% count variable update
end %Ending the for loop.
2) To plot outside the for loops, here you need to split up and write the code twice as placed below:
%% Setting Initial Conditions
clc %Clearing the command window.
clear all %Making output nicer.
%0 is a step to the left and 1 is a step to the right.
Left = 0; %Setting the initial left value.
Right = 0; %Setting the initial right value.
count = 1;
NVal =[25000, 100000];
%% Creating the Ensemble of Random Walkers
for N = [25000, 100000] %Creating a vector for all the ensemble sizes.
for x = 1:N %Creating an ensemble of N walkers.
for i = 2:2501 %Setting the for loop to run 2500 times.
Walker = -1 + rand(1)*(2); %Setting the walker to be a random number between -1 or 1.
if Walker > 0 %Saying if the walker takes a step to the right.
Left(i) = Left(i-1); %Left number of steps stays the same.
Right(i) = Right(i-1) + Walker; %Right number of steps goes up by walker value.
elseif Walker < 0 %Saying if the walker takes a step to the left.
Left(i) = Left(i-1) + Walker; %Left number of steps goes up by walker value.
Right(i) = Right(i-1); %Right number of steps stay the same.
end %Ending the if statement.
end %Ending the for loop.
location = Right(end) - abs(Left(end)); %Making the location the right number of steps minus the left.
Finallocation{count}(x) = location; %Setting the location to be the end values of the walkers.
end %Ending the for loop.;
%% Plotting the Results
count = count+1;
end %Ending the for loop.
figure('Name','Ensemble Histogram') %Labeling the figure appropriately.
subplot(2,1,1)
histogram(Finallocation{1},20) %Making a histogram of the final locations.
z1 = sprintf('Ensemble of step size = %d',NVal(1)); %Making a title that updates as variables are changed.
title(z1) %Making z1 the title.
xlabel('Final Location') %Labeling the x-axis appropriately.
ylabel('Number of Walkers') %Labeling the y-axis appropriately.
subplot(2,1,2)
histogram(Finallocation{2},20) %Making a histogram of the final locations.
z1 = sprintf('Ensemble of step size = %d',NVal(2)); %Making a title that updates as variables are changed.
title(z1) %Making z1 the title.
xlabel('Final Location') %Labeling the x-axis appropriately.
ylabel('Number of Walkers') %Labeling the y-axis appropriately.
If the values of N are more to use, i suggest the first method itself, to make the plot inline the for. Just update the values in subplot accordingly.
Hope this helps.
Regards,
Sriram
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!