Loop to carry out calculation for pairs 1 and 2, then pairs 2 and 3 and so on.

2 ビュー (過去 30 日間)
BOB
BOB 2018 年 8 月 21 日
コメント済み: Torsten 2018 年 8 月 21 日
Hi,
I have two 8x1 matrices. One matrix is list of longitude coordinates (Final_Longitude_Values). The other matrix is the corresponding latitude coordinates (Final_Latitude_Values).
I want to calculate the distance between the first pair and the second pair, and then the second pair and the third pair, and so on until I have come to the end of each matrix.
I have written the following code in an attempt to automate this process:
{for j = 1:length(Final_Latitude_Values)
latlon1=[(Final_Latitude_Values(j)) (Final_Longitude_Values(j))];
latlon2=[(Final_Latitude_Values(j+1) (Final_Longitude_Values(j+1))];
end
[d1km d2km]=distance(latlon1,latlon2);
Segment_Distance = d2km}
However, the index exceeds the matrix dimensions, and I can't seem to figure out how to work around this issue. If anyone could correct my code it would be much appreciate.
Thanks

採用された回答

Torsten
Torsten 2018 年 8 月 21 日
Use
for j = 1:length(Final_Latitude_Values)-1
instead of
for j = 1:length(Final_Latitude_Values)
Best wishes
Torsten.

その他の回答 (2 件)

Fangjun Jiang
Fangjun Jiang 2018 年 8 月 21 日
You have 8 locations thus 7 distances. When j is 8, Final_Latitude_Values(j+1) exceeds the matrix dimension.
Change the for-loop to
for j = 1:length(Final_Latitude_Values)-1

BOB
BOB 2018 年 8 月 21 日
Cheers folks! Works great.
  1 件のコメント
Torsten
Torsten 2018 年 8 月 21 日
I don't know if this is intended, but note that you only calculate the distance between the last two components of the vectors, namely
[(Final_Latitude_Values(j)) (Final_Longitude_Values(j))]
and
[(Final_Latitude_Values(j+1)) (Final_Longitude_Values(j+1))]
for
j = length(Final_Latitude_Values)-1.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by