MATLAB: How to save the results of an operation performed 1000 times in one vector

MATLABrandom number generatorsimulationvectors

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

Best Answer

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