How to Subplot in a for loop

조회 수: 14 (최근 30일)
Patrick Keaveney
Patrick Keaveney 2020년 4월 3일
댓글: Patrick Keaveney 2020년 4월 6일
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
Image Analyst
Image Analyst 2020년 4월 4일
You have 3 nested for loops. What do you want to plot, and where?
Patrick Keaveney
Patrick Keaveney 2020년 4월 4일
I want to do a subplot for the two histograms of "Finallcation". I would prefer to plot them outside the for loops if possible.

댓글을 달려면 로그인하십시오.

채택된 답변

Sriram Tadavarty
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
  댓글 수: 1
Patrick Keaveney
Patrick Keaveney 2020년 4월 6일
Thank you. This does help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by