Filter löschen
Filter löschen

Performing calculation across large data set

2 Ansichten (letzte 30 Tage)
Erik J
Erik J am 29 Jan. 2017
Bearbeitet: Guillaume am 30 Jan. 2017
I have a large matrix of latitude and longitude coordinates (343212 x 2 double). I want to calculate the distance between each set of coordinates in this matrix and a single reference coordinate. I am using the LatLon Distance function.
What I do not know how to do is loop through each pair of coordinates in the data set so that it returns a vector with the distance between each pair of coordinates and the reference point.
Any help is much appreciated, thank you.

Akzeptierte Antwort

Guillaume am 29 Jan. 2017
Bearbeitet: Guillaume am 30 Jan. 2017
Assuming you're using R2016b, this modified function will work straight away with your matrix and single reference
function [d1km, d2km] = betterlldistkm(latlon1, latlon2)
% latlon1: either a single point (1x2) or a matrix of points (nx2)
% latlon2: either a single point (1x2) or a matrix of points (nx2)
% if both latlon1 and latlon2 are matrices of point, both must have the same number of points
radius = 6371;
latlon1 = latlon1 * pi/180;
latlon2 = latlon2 * pi/180;
delta = latlon1 - latlon2;
a = sin(delta(:, 1)/2).^2 + cos(latlon1(:, 1)).*cos(latlon2(:, 1)).*sin(delta(:, 2)/2).^2;
c = 2*atan2(sqrt(a), sqrt(1-a));
d1km = radius*c;
x = delta(:, 2) .* cos(latlon1(:, 1)+latlon2(:, 1))/2;
y = delta(:, 1);
d2km = radius * hypot(x, y);
Usage example:
a = rand(34312, 2);
b = rand(1);
[d1, d2] = betterlldistkm(a, b)
That's all.

Weitere Antworten (1)

Iddo Weiner
Iddo Weiner am 29 Jan. 2017
Bearbeitet: Iddo Weiner am 29 Jan. 2017
a = rand(10,2); %change this to your data
out = nan(length(a));
for i = 1:(length(a))
out(i,1) = abs(mean([a(i,1),a(i,2)]) - constant part;
% change this function to the one you want to use, I just invented
% something for the example
  1 Kommentar
Guillaume am 29 Jan. 2017
Please, do not use length on matrices. Your code will fail if the input matrix only has one row (since length will then return the number of columns). Use size with a explicit dimension, in this case size(a, 1).
I wouldn't recommend length for vectors either. numel is safer.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by