How manage for loop to calculate distances?

Helo,
I am trying to calculate distance in km using two variables with a set of latitude and longitude. I tried to use the "distance" funtion inside of two for loop but it is giving an error. Code is in follow:
lat= [-8, -8.2, -8.21, -8.34, -8.9];
long = [-18, -18.2, -18.3, -18.4, -18.45];
total=length(lat);
i=0;
j=0
j = 0
for i=1:total
for j=1:total
i=i+1;
j=j+1;
all_distances(:,k)=distance(lat(:,i,j),long(:,i,j),lat(:,i+1,j+1),long(:,i+1,j+1),wgs84Ellipsoid("km"));
end
end
Index in position 3 exceeds array bounds. Index must not exceed 1.
I should have a new "all_distances" variable with 5 values of distance, but it is only showing the error:
Index in position 3 exceeds array bounds (must not exceed 1).
Someone know what is the problem? Thanks in advance!

1 Comment

Don't try to change the loop variables inside a for loop. On one hand, any changes you make will be thrown away when the next iteration of the loop starts. On the other hand, during the last iteration of the j loop you increment i and j and make j one greater than the length of lat, which seems wrong the way you're trying to use it. You're taking one step off the end of the gangplank, and MATLAB doesn't want to get wet!

Sign in to comment.

 Accepted Answer

As the goal here is to find the distance between consecutive points -
%Data
lat= [-8, -8.2, -8.21, -8.34, -8.9];
long = [-18, -18.2, -18.3, -18.4, -18.45];
%Define a reference to find distance as linear distance in km
wgs84 = wgs84Ellipsoid("km");
len = distance(lat(1:end-1), long(1:end-1), lat(2:end), long(2:end), wgs84)
len = 1×4
31.2277 11.0741 18.1132 62.1796

4 Comments

I do not think it is correct. You code is calculating all distance comparing to the initial point (first two coordinayes). The distance should be calculated idenpendently. Ex:
wgs84 = wgs84Ellipsoid("km");
distance(-8, -18, -8.2, -18.2, wgs84)
ans = 31.2277
and
wgs84 = wgs84Ellipsoid("km");
distance(-8.2, -18.2, -8.21, -18.3, wgs84)
ans = 11.0741
and
wgs84 = wgs84Ellipsoid("km");
distance(-8.21, -18.3,-8.34,-18.4, wgs84)
ans = 18.1132
and
wgs84 = wgs84Ellipsoid("km");
distance(-8.34,-18.4, -8.9, -18.45, wgs84)
ans = 62.1796
My goal is to get these four ans number as output.
"My goal is to get these four ans number as output."
Alright, it's clear now.
In that case -
%Data
lat= [-8, -8.2, -8.21, -8.34, -8.9];
long = [-18, -18.2, -18.3, -18.4, -18.45];
%Define a reference to find distance as linear distance in km
wgs84 = wgs84Ellipsoid("km");
len = distance(lat(1:end-1), long(1:end-1), lat(2:end), long(2:end), wgs84)
len = 1×4
31.2277 11.0741 18.1132 62.1796
Yes, its perfect. It worked on my personal code. Thank you very much. Maybe you could edit your main answer to the public understand better the problem. I will accept your answer.
@Guilherme Weber Sampaio de Melo, I've edited my main answer. Please check it.

Sign in to comment.

More Answers (1)

Torsten
Torsten on 17 Nov 2023
Edited: Torsten on 17 Nov 2023
I should have a new "all_distances" variable with 5 values of distance, but it is only showing the error:
Why 5 ? You should have 4 + 3 + 2 + 1 = 10 distances if you have 5 positions.
You get a symmetric matrix distance(i,j) where distance(i,j) is the distance from position i to position j.
Maybe "pdist2" is the correct code to use in your case.
In your code, lat and long are arrays of size 1x5. So why do you suddenly use them as three-dimensional arrays in
all_distances(:,k)=distance(lat(:,i,j),long(:,i,j),lat(:,i+1,j+1),long(:,i+1,j+1),wgs84Ellipsoid("km"));
?

3 Comments

5 because I have 5 lat and 5 long. Then, I would take the first pair of lat/long and measure the first distance. Second lat/long pair and measure the second distance. Measure the third distance with the third lat/long pair. Then, it do the same until complete all lat and long. I have on code example 5 lat/long, so, it should give in total 5 distances. I am not sure if my code is correct. That is what I had in my mind.
How is "distance" defined in this context?
Is there a mathematical formula you are using as a reference?
Well, distance in km from geographical coordinates (my two variable Lat/Long). Do you know "distance" function of MATLAB? https://www.mathworks.com/help/map/ref/distance.html

Sign in to comment.

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!