finding distance from text data for latitude/longitude and creating new column of data for distance

3 visualizzazioni (ultimi 30 giorni)
Hello all,
I have a series of text files with latitude/longitude data, in seperate columns, and time in another column. I would liek to use the latitude/longitude data to make a distance vs. time plot.
formula for distance based on lat/long:
R = earths radius (mean radius = 6,371km)
Δlat = lat2lat1
Δlong = long2long1
a = sin²(Δlat/2) + cos(lat1).cos(lat2).sin²(Δlong/2)
c = 2.atan2(a, (1a))
d = R.c
The syntax isnt correct above as its not matlab code, just a formula on the internet.
How would I go about defining a new column of data (distance) by having it find the difference between every point of lat and long and then run the other basic operations to find distance?
the data in the text would follow this format;
Latitude Longitude
51.8885857 -2.159813307
51.888588 -2.159810551
51.88858751 -2.159811237
51.88858772 -2.159810981
51.88858774 -2.159810887
51.88858765 -2.159811144
51.88858779 -2.159810991
51.88858747 -2.159811489
51.88858758 -2.159811597
51.88858755 -2.15981158
51.88858725 -2.159811985
51.88858732 -2.159812218
  1 Commento
douglas
douglas il 13 Apr 2012
would a double for loop work?
say
for i = 2:length(Latitude);
for j = 1:length(Latitude);
dellat = Latitude(i)-Latitude(j);
dellong = Longitude(i)-Longitude(j);
end
end
and then relate dellat and dellong with the above formula to find an array for the value of d? I haven't gotten the chance to test it yet.

Accedi per commentare.

Risposta accettata

bym
bym il 13 Apr 2012
the diff() function will calculate the difference for you without a loop. Here is a start:
loc = [51.8885857 -2.159813307;...
51.888588 -2.159810551;...
51.88858751 -2.159811237;...
51.88858772 -2.159810981;...
51.88858774 -2.159810887;...
51.88858765 -2.159811144;...
51.88858779 -2.159810991;...
51.88858747 -2.159811489;...
51.88858758 -2.159811597;...
51.88858755 -2.15981158;...
51.88858725 -2.159811985;...
51.88858732 -2.159812218];
dloc = diff(loc);
a = sind(dloc(:,1)./2).^2 + ...
cosd(loc(1:end-1,1)).*cosd(loc(2:end,1)).*sind((dloc(:,2)./2).^2);
  2 Commenti
douglas
douglas il 18 Apr 2012
This seemed to work out for me, is there a way to add each new data point as a sum of the previous ones? like mapping out a flight and having the last data point be the added sum? my results right now give me the distance traveled per data point, so it fluctuates based on speed etc. I'd like a total distance traveled

Accedi per commentare.

Più risposte (1)

Andrei Bobrov
Andrei Bobrov il 13 Apr 2012
use function distance from Mapping Toolbox
LatLon = [51.8885857 -2.159813307
51.888588 -2.159810551
51.88858751 -2.159811237
51.88858772 -2.159810981
51.88858774 -2.159810887
51.88858765 -2.159811144
51.88858779 -2.159810991
51.88858747 -2.159811489
51.88858758 -2.159811597
51.88858755 -2.15981158
51.88858725 -2.159811985
51.88858732 -2.159812218]
s1 = size(LatLon,1)
k = nchoosek(1:s1,2)
d = deg2km(distance(LatLon(k(:,1),1),LatLon(k(:,1),2),...
LatLon(k(:,2),1),LatLon(k(:,2),2))) ;
dout = zeros(s1);
dout(tril(true(s1),-1)) = d;
dout = dout + dout';
variant without Mapping Toolbox
L = [51.8885857 -2.159813307
51.888588 -2.159810551
51.88858751 -2.159811237
51.88858772 -2.159810981
51.88858774 -2.159810887
51.88858765 -2.159811144
51.88858779 -2.159810991
51.88858747 -2.159811489
51.88858758 -2.159811597
51.88858755 -2.15981158
51.88858725 -2.159811985
51.88858732 -2.159812218];
R = 6371;
dla = abs(bsxfun(@minus,L(:,1),L(:,1)'));
dlo = abs(bsxfun(@minus,L(:,2),L(:,2)'));
lac = cosd(L);
a = sind(dla/2).^2 + lac(:,1)*lac(:,1)'.*sind(dlo/2).^2;
d = R*2*atan2(sqrt(a), sqrt(1 - a)) ;
on Douglas's comment
out1 = diag(d,1)
out2 = cumsum(out1)

Categorie

Scopri di più su Data Import and Export in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by