Find distances between all points

Hi,
I have four landmarks which are A,B,C,D
These landmarks have the coordinates in x,y vectors respectively:
x = [1, 2, 3, 4] y = [1, 3, 4, 3]
Now I want to find the distance (using distance formula for two points) between all the points but avoid repetition. So for example I want to find the distances:
AB/AC/AD/BC/BD/CD
Any help is appreciated thanks.

回答 (1 件)

Image Analyst
Image Analyst 2014 年 6 月 1 日

1 投票

Can I assume you tried the obvious, simple, straightforward, intuitive brute force approach?
x = [1, 2, 3, 4];
y = [1, 3, 4, 3]
for xIndex = 1 : length(x)
for yIndex = xIndex : length(y)
distances(xIndex, yIndex) = sqrt((x(xIndex)-x(yIndex))^2 + (y(xIndex)-y(yIndex))^2)
end
end
distances % Report to the command window.
If you already tried this obvious brute force approach and didn't like it for some reason then say what's wrong with it?

1 件のコメント

Isuru
Isuru 2014 年 6 月 2 日
Thanks. I managed to use pdist(X) instead. Thanks for the reply anyway.

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

カテゴリ

ヘルプ センター および File ExchangeLanguage Fundamentals についてさらに検索

質問済み:

2014 年 6 月 1 日

コメント済み:

2014 年 6 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by