I have to calculate the distance of all UK postcodes against each other, then sum the population of all postcodes within 1 mile. The postcode & population list are stored in a text file. I'm most familiar with matlab, but I also have Stata & PSPP available. The program is currently scheduled to take about 2 weeks. Is there anything I can do to accelerate this process??? Here is my code. Matlab generated the script to import the text data. The distance function is from the mapping toolbox, and performs the great circle formula.
Any help is greatly appreciated.
function pcdistance(postcode, pop, lat, lon)
%Finds total population for UK postcode within 1 mile radius
fid = fopen('PPC.txt','a');
n = length(postcode);
%Calculates distance of 1 postcode at a time, against all others
%All data that doesn't meet rules is deleted
for i = 1:n;
dist = [];
dist(:,1)= pop;
for j = 1:n;
dist(j,2) = distance(lat(i),lon(i),lat(j),lon(j),3963.17);
good = dist(1:j,2)<= 1;
end
dist = dist(good,:);
tot = sum(dist(:,1));
fprintf(1,'%s,%d;',postcode{i},tot)
end
%Find sum of population within 1 mile
fclose(fid);
end
Here's a small sample of the inputs, from the txt file. The columns are "postcode, pop, lat, long" respectively.
"BD7 1DB",749,53.79,-1.76
"M15 6AA",748,53.46,-2.24
"WR2 6AJ",748,52.19,-2.24
"M15 6PF",745,53.46,-2.23
"IP7 7RA",741,52.12,0.96
"CF62 4WA",740,51.41,-3.41
"M1 2AR",738,53.47,-2.22
"NG1 4BR",737,52.95,-1.14
"ST16 3AW",735,52.81,-2.11
"AB25 1LE",733,57.15,-2.10
"WF2 9AG",730,53.68,-1.50
"DT11 8RH",730,50.86,-2.12
"CW1 5NP",729,53.09,-2.41
"TR12 7RH",724,50.08,-5.25
"ST5 5DY",723,53.00,-2.27
"HA1 3HP",723,51.57,-0.33
"DL10 7NP",722,54.37,-1.62
"M1 7HR",719,53.47,-2.23
"B18 4AS",719,52.49,-1.93
"OX13 6JB",716,51.68,-1.30
Here's the corrected code.
function pcdistance4(postcode, pop, lat, lon)
%Finds total population for UK postcode within 1 mile radius
fid = fopen('PPC.txt','A');
n = length(postcode);
% Pre-allocation
dist = zeros(n,2);
tot = zeros(n,1);
tic
for i = 1:n;
dist(:,1)= pop;
dist(:,2) = distance(lat(i),lon(i),lat(:),lon(:),3963.17);
good = dist(:, 2) <= 1 & dist(:,2) ~=0;
tot(i) = sum(dist(good, 1));
tot(i) = tot(i) + pop(i);
end
toc
tic
for j = 900001:n;
fprintf(fid,'%s,%d;\n',postcode{j},tot(j));
end
toc
fclose(fid);
end