MATLAB: Index exceeds the number of array elements (1).

lat lonloopsMATLABrandom

Good Afternoon,
I am attempting to generate random Lat/Lon pairs that are a minimum distance away from each other. I need to store these in an array so I can utilize it else where in a program. When I run the following script I recieve this error:
Index exceeds the number of array elements (1).
Error in radians (line 30)
N_Lat = randA(k);
I am not sure how to fix this. Included is the script, please pardon the sloppyness of my coding, I am inexperienced.
clear variables
%---------------User Defined Variable------------------
latA = 1;
latB = 5;
lonA = 15;
lonB = 25;
N_randP = input('how many points in the pattern? ');
D_input = input('Minimum distance required? ');
%---------------Create an Array for N points-----------
arr1 = zeros(3, N_randP);
%--------------Randomize functions---------------------
randA = random('uniform', latA,latB);
randB = random('uniform', lonA, lonB);
%--------------Set the inital buoy --------------------
initLat = randA(1);
disp(initLat)
arr1(1,1) = initLat;
initLon = randB(1);
disp(initLon)
arr1(2,1) = initLon;
%--------------Loop for all points in the array ------------
i = 2;
for k = 2 : N_randP
%get next point
N_Lat = randA(k);
N_Lon = randB(k);
%Place in array
arr1(1,k) = N_Lat;
arr1(2,k) = N_Lon;
%How far away are the points
j = 1;
dlat = (N_Lat-initLat);
dlon = (N_Lon - initLon);
lat1 = (initLat);
lat2 = (N_Lat);
a = (sin(dlat./2)).^2 + cos(lat1) .* cos(lat2) .*(sin(dlon./2)).^2;
c = 2 .*asin(sqrt(a));
minDist = min(c);
if minDist >= D_input
intLat(i) = N_Lat;
intLon(i) = N_Lon;
i = i + 1;
j = j+1;
end
end
plot(intLat, intLon, 'b*')
grid on;

Best Answer

Your random calls are returning only one value because that is all you requested from them. If you want an array of values, you need to pass those arguments to the random function.
Try this:
randA = random('uniform', latA,latB, 1, N_randP);
randB = random('uniform', lonA, lonB, 1, N_randP);
Experiment to get the result you want.