How do I get directionality from the departure() function

1 view (last 30 days)
I have two sets of coordinates and I need to calculate the distance between them in longitude converted to meters. The departure() function serves this purpose well. However, it returns all positive values for distance regardless of which coordinate is more east/west than the other. Is there a way I can get directionality from this function, or by altering the output in some indexed way?
lat1=[45.73366;42.30074;42.11033;37.69088];
lon1=[-115.85149;-115.54721;-108.03860;-96.52568];
lat2=[45.94414;41.20918;42.64974;36.66341];
lon2=[-115.28569;-116.87984;-108.61347;-95.06520];
coords=table(lat1,lon1,lat2,lon2)
coords = 4×4 table
lat1 lon1 lat2 lon2 ______ _______ ______ _______ 45.734 -115.85 45.944 -115.29 42.301 -115.55 41.209 -116.88 42.11 -108.04 42.65 -108.61 37.691 -96.526 36.663 -95.065
coords.lon_diff_m=departure(coords.lon1,coords.lon2,(coords.lat1+coords.lat2)./2,wgs84Ellipsoid,'degrees')
coords = 4×5 table
lat1 lon1 lat2 lon2 lon_diff_m ______ _______ ______ _______ __________ 45.734 -115.85 45.944 -115.29 43956 42.301 -115.55 41.209 -116.88 1.1083e+05 42.11 -108.04 42.65 -108.61 47344 37.691 -96.526 36.663 -95.065 1.297e+05
%output in meters since wgs84Ellipsoid uses meters for semi-major axis definition
%average latitudes for longitudinal distance calculation
As you can see, all the distances are positive values. I would like the output to be directional where, if coordinate 1 is west of coordinate 2 the output is negative, and if coordinate 1 is east of coordinate 2 the output is positive.
I suppose I could use an index vector of lat1<lat2 to multiply the rows that qualify by -1.
idx=(coords.lon1<coords.lon2);
coords.lon_diff_m(idx)=coords.lon_diff_m(idx).*-1
coords = 4×5 table
lat1 lon1 lat2 lon2 lon_diff_m ______ _______ ______ _______ __________ 45.734 -115.85 45.944 -115.29 -43956 42.301 -115.55 41.209 -116.88 1.1083e+05 42.11 -108.04 42.65 -108.61 47344 37.691 -96.526 36.663 -95.065 -1.297e+05
I came up with this answer as I am writing this question. However, if anybody has a better way to do this or sees an error in my method, please let me know.

Accepted Answer

William Rose
William Rose on 7 Mar 2023
Multiply the values from departure by sign(lon1-lon2).
Define points:
lat1=[45.73366;42.30074;42.11033;37.69088];
lon1=[-115.85149;-115.54721;-108.03860;-96.52568];
lat2=[45.94414;41.20918;42.64974;36.66341];
lon2=[-115.28569;-116.87984;-108.61347;-95.06520];
Compute longitude difference with +/-:
lon_diff_m=departure(lon1,lon2,(lat1+lat2)/2,wgs84Ellipsoid,'degrees').*sign(lon1-lon2)
lon_diff_m = 4×1
1.0e+05 * -0.4396 1.1083 0.4734 -1.2970
That works.
  1 Comment
William Rose
William Rose on 7 Mar 2023
@Steve K, departure() is useful if you are doing latitude sailing, as was common pre-good-chronometers, i.e. pre-1730s. Nowadays one would sail or fly a great circle track. What is the current usefulness of departure()? Surveying? Hiking the Mason Dixon line, or the 49th parallel?

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!