Syntax for loop to compute distance from a fix point

Hello Everyone,
I am working on a for loop that will compute distance a long a line from a fixed point reference associated with a lat and lon. I have a table of lat and lons, but when I attempt to run the loop the code is only computing the first distance and not running for each value in the lat and lon column. I have noticed that how I am calling the individual values of 'Full_profile_1999' table seem to be wrong but I cannot figure out how to properly call them. If anyone one could assist me with fixing the code so it runs for all then that would be great.
Note: The first few lines load, and concatenate the files together so they are one flight profile of lat and lons. These are then converted to degree and I am attempting to run them through a haversine function I made that uses the points. The problem is in the for loop, variable definition or function part of the code
Data_1 = readtable('Data_19990519_01_014_151949.csv'); %Was shortened in
Data_2 = readtable('Data_19990519_01_015_153633.csv');
Full_profile_1999 = [Data_1; Data_2];
Full_profile_1999.Distance = zeros(742, 1);
Full_profile_1999.LAT = deg2rad(Full_profile_1999.LAT);
Full_profile_1999.LON = deg2rad(Full_profile_1999.LON);
lat_ref = min(Full_profile_1999.LAT(:,1));
lon_ref = min(Full_profile_1999.LON(:,1));
% dlat = Full_profile_1999.LAT - lat_1;
% dlon = Full_profile_1999.LON - lon_1;
len = length(Full_profile_1999.LAT);
for k = 1:len
lat_2 = Full_profile_1999.LAT(k,1);
lon_2 = Full_profile_1999.LON(k,1);
haversine(lat_2, lat_ref, lon_2, lon_ref);
Full_profile_1999.Distance = dist(k);
end
function [dist] = haversine(lat_2, lat_ref, lon_2, lon_ref)
dlat = lat_2 - lat_ref;
dlon = lon_2 - lon_ref;
a = (sin(dlat/2).^2 + cos(lat_ref).*cos(lat_2).*sin(dlon/2).^2);
dist = asin(sqrt(a));
end

1 个评论

Remember that length() does not necessarily refer to the first dimension: it refers to the largest dimension. If you are iterating over rows then you should be using len = size(Full_profile_1999.LAT,1)

请先登录,再进行评论。

 采纳的回答

len = size(Full_profile_1999.LAT, 1);
for k = 1:len
lat_2 = Full_profile_1999.LAT(k,1);
lon_2 = Full_profile_1999.LON(k,1);
dist = haversine(lat_2, lat_ref, lon_2, lon_ref);
Full_profile_1999.Distance(k) = dist;
end
Or simply no loop and
Full_profile_1999.Distance = haversine(Full_profile_1999.LAT, lat_ref, Full_profile_1999.LON, long_ref);
which will work because haversine() is vectorized.

1 个评论

Thank you Walter! I knew I was close, but this is perfect. Thank you for the help!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Vehicle Scenarios 的更多信息

产品

版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by