How to save the results of an operation performed 1000 times in one vector?
1 次查看(过去 30 天)
显示 更早的评论
Hello,
When I run following code, each time would get different result (random), so I am trying to find the results (variable: total ) 1000 times and I want to save the reusults in a vector. How can I do this by only one run without having to run it 1000 times and save each value manually. I tried many things but every time I get an error. Hope someone can help me.
Thank you.
close all;
clear;
%Simulation window parameters :Eves
xMin=0;xMax=1;
yMin=0;yMax=1;
xDelta=xMax-xMin;yDelta=yMax-yMin; %rectangle dimensions
areaTotal=xDelta*yDelta;
PL=2;
lambda=1; %intensity (ie mean density) of the Poisson process, so we have three points we want to distribute randomly and now we will locate them in the rectangle of area=1.
%Simulate Poisson point process
numbPoints=poissrnd(areaTotal*lambda);%Poisson number of points
xx=xDelta*(rand(numbPoints,1))+xMin;%x coordinates of Poisson points. Generate values from the uniform distribution on the interval [xMax andxMin].
%matrix size generated by rand is: numbPoints * 1
yy=xDelta*(rand(numbPoints,1))+yMin;%y coordinates of Poisson points
%Plotting
% scatter(xx,yy,'LineWidth',2.0);
% hold on;
X=[xx , yy]; %matrix with eaves locations (as a pair)
%------------------------------------------------
%neareset one point to one source(P): Alice
xs=0;%
ys=0;
zs=[xs,ys ];
P=zs;
%compute Euclidean distances:
distances = sqrt(sum(bsxfun(@minus, P, X).^2,2));
%find the smallest distance and use that as an index into B:
closest = X(find(distances==min(distances)),:); %location of the closest eave (the pair)
min_distance =sqrt(closest(1).^2+closest(2).^2);%the min distance
total =min_distance .^PL
0 个评论
采纳的回答
Image Analyst
2020-4-11
Make it into a function
function total = GetTheTotal( ...some inputs......)
% code.....
total = whatever......
end
Then call it in a loop 1000 different times storing the totals in a vector:
for k = 1 : 1000
totals(k) = GetTheTotal( ...some inputs......);
end
6 个评论
Image Analyst
2020-4-11
No, you still didn't use my suggestion of putting it into a function and then calling it in a loop. Here is the fixed code. Just click the copy button and paste it into a new script in MATLAB and press the green run triangle.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
fprintf('Beginning to run %s.m ...\n', mfilename);
hFig = figure;
hFig.WindowState = 'maximized'
numberOfIterations = 1000;
totals = zeros(1, numberOfIterations);
for k = 1 : numberOfIterations
totals(k) = GetTheTotal(2);
end
plot(totals, 'b.', 'MarkerSize', 14);
grid on;
title('Totals', 'FontSize', fontSize);
xlabel('Iteration Number', 'FontSize', fontSize);
ylabel('Total', 'FontSize', fontSize);
fprintf('Done running %s.m ...\n', mfilename);
function total = GetTheTotal(PL)
total = 0; % Initialize so we'll have a value in case of error.
%Simulation window parameters :Eves
xMin = 0;
xMax = 1;
yMin = 0;
yMax = 1;
xDelta = xMax-xMin;
yDelta = yMax-yMin; %rectangle dimensions
areaTotal = xDelta*yDelta;
% PL = 2;
lambda = 1; %intensity (ie mean density) of the Poisson process, so we have three points we want to distribute randomly and now we will locate them in the rectangle of area = 1.
%Simulate Poisson point process
numbPoints = poissrnd(areaTotal*lambda);%Poisson number of points
if numbPoints <= 0
fprintf('Skipping this one because numbPoints = 0!\n');
return;
else
fprintf('Doing this one because numbPoints = %d!\n', numbPoints);
end
xx = xDelta*(rand(numbPoints,1))+xMin;%x coordinates of Poisson points. Generate values from the uniform distribution on the interval [xMax andxMin].
%matrix size generated by rand is: numbPoints * 1
yy = xDelta*(rand(numbPoints,1))+yMin;%y coordinates of Poisson points
%Plotting
% scatter(xx,yy,'LineWidth',2.0);
% hold on;
X = [xx , yy]; %matrix with eaves locations (as a pair)
%------------------------------------------------
%neareset one point to one source(P): Alice
xs = 0.0001;%
ys = 0.0001;
zs = [xs,ys ];
P = zs;
% scatter(xs,ys,'LineWidth',2.0); %we want this to be the soource
%compute Euclidean distances:
distances = sqrt(sum(bsxfun(@minus, P, X).^2,2));
%find the smallest distance and use that as an index into B:
closest = X(find(distances==min(distances)),:); %location of the closest eave (the pair)
min_distance = sqrt(closest(1).^2+closest(2).^2);%the min distance
total = min_distance .^PL
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/283747/image.png)
更多回答(1 个)
Ameer Hamza
2020-4-11
Your code gives an error when poissrnd(areaTotal*lambda) generates a zero because X is empty matrix and the line
min_distance =sqrt(closest(1).^2+closest(2).^2);%the min distance
fails. I added a condition in your code if 'closest' is empty, then the min_distance for that iteration will become infinity (change it according to your logic).
Check the following code, it uses a for loop to run your code 1000 time and save values in array
close all;
clear;
%Simulation window parameters :Eves
xMin=0;xMax=1;
yMin=0;yMax=1;
xDelta=xMax-xMin;yDelta=yMax-yMin; %rectangle dimensions
areaTotal=xDelta*yDelta;
PL=2;
total = zeros(1, 1000);
for i=1:1000
lambda=1; %intensity (ie mean density) of the Poisson process, so we have three points we want to distribute randomly and now we will locate them in the rectangle of area=1.
%Simulate Poisson point process
numbPoints=poissrnd(areaTotal*lambda);%Poisson number of points
xx=xDelta*(rand(numbPoints,1))+xMin;%x coordinates of Poisson points. Generate values from the uniform distribution on the interval [xMax andxMin].
%matrix size generated by rand is: numbPoints * 1
yy=xDelta*(rand(numbPoints,1))+yMin;%y coordinates of Poisson points
%Plotting
% scatter(xx,yy,'LineWidth',2.0);
% hold on;
X=[xx , yy]; %matrix with eaves locations (as a pair)
%------------------------------------------------
%neareset one point to one source(P): Alice
xs=0;%
ys=0;
zs=[xs,ys ];
P=zs;
%compute Euclidean distances:
distances = sqrt(sum(bsxfun(@minus, P, X).^2,2));
%find the smallest distance and use that as an index into B:
closest = X(distances==min(distances),:); %location of the closest eave (the pair)
if isempty(closest)
min_distance = inf;%the min distance
total(i) = min_distance .^PL;
else
min_distance =sqrt(closest(1).^2+closest(2).^2);%the min distance
total(i) = min_distance .^PL;
end
end
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!