Hi given the following code,
figure(1);
scatter(x(:,1),x(:,2));
hold on;
scatter(member_value(:,1),member_value(:,2),'r');
legend({'Data','Pareto Frontier'})
I obtain a graph like this
untitled.jpg
And I want to select the red point that is closest to the origin.
May someone help me with the code?

2 件のコメント

Adam
Adam 2019 年 10 月 9 日
What do you mean by 'select' it? You can click on it and select it if you wish, but that depends what you want to do having 'selected' it.
If you mean programmatically find it then isn't it just a simple case of pythagoras, having subtracted your origin from all points? (Or some built-in distance function that does the maths for you anyway)
luca
luca 2019 年 10 月 9 日
Yes the idea is to use something like pythagoras, that able me to find the point with the minimum distance from the origin. But do you know how can I implement it?

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

 採用された回答

Adam Danz
Adam Danz 2019 年 10 月 9 日
編集済み: Adam Danz 2019 年 10 月 9 日

0 投票

To find the coordinate closest to the origin (0,0),
d = hypot(member_value(:,1),member_value(:,2));
[~, minIdx] = min(d);
plot(member_value(minIdx,1),member_value(minIdx,2),'ks','MarkerSize',12);
hypot() method avoids potential under/overflow: https://www.mathworks.com/help/matlab/ref/hypot.html

8 件のコメント

luca
luca 2019 年 10 月 9 日
Due to the fact that the red point are the one farther from (0,0)
in case I want to find the fartermost point, how can I modify the code?
Adam Danz
Adam Danz 2019 年 10 月 9 日
Simply exchange min() with max().
[~, maxIdx] = max(d);
Adam
Adam 2019 年 10 月 9 日
Change min to max...
luca
luca 2019 年 10 月 9 日
thanks Adam
Adam Danz
Adam Danz 2019 年 10 月 9 日
Thanks Adams. :D
@luca, the hypot() function merely computes the hypotenuse ('h') of a right triangle with sides 'a' and 'b'. All of your points form a right triangle if you imagine a line going straight down to the x axis and across to the y axis. The line from (0,0) to each of your points in the hypotenuse.
191009 091331-Figure 1.png
luca
luca 2019 年 10 月 9 日
I don't get what you mean Adam
Adam Danz
Adam Danz 2019 年 10 月 9 日
編集済み: Adam Danz 2019 年 10 月 9 日
Let's say you have 4 coordinates (black dots, below).
Each of those coordinates forms a right-triangle where 1 side of the triangle is the 'x-coordinate and the other side of the triangle is the y-coordinate. The hypotenuse of the triangle is the line that extends from (0,0) to the dot.
hypot() inputs are the x and y coordinates which are the sides of each triangle. It outputs the length of the hypotenuse which is the distance of the dot from (0,0). The hypotenuse are the diagonal lines in the image below.
Hypotenuse demo code for the image above:
clf()
x = rand(4,2).*8+1;
scatter(x(:,1),x(:,2));
hold on;
axis equal
xlim([0,9])
ylim([0,9])
grid on
leg = plot(x(:,[1,1])', [x(:,2),zeros(size(x,1),1)]', '-','LineWidth',2);
hyp = plot([x(:,1),zeros(size(x,1),1)]', [x(:,2),zeros(size(x,1),1)]', '-','LineWidth',2);
set(hyp,{'Color'},{leg.Color}')
d = hypot(x(:,1),x(:,2));
labels = strsplit(sprintf('h=%.1f\n',d'));
t = text(x(:,1)-.2,x(:,2),labels(1:end-1),'HorizontalAlignment','right');
luca
luca 2019 年 10 月 9 日
Thanks Adam ! now its clear how it works

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

その他の回答 (1 件)

Turlough Hughes
Turlough Hughes 2019 年 10 月 9 日

0 投票

You can do the following:
[~,ind]=min(sqrt(member_value(:,1).^2+member_value(:,2).^2)); %find index for point closest to origin
hold on; plot(member_value(ind,1),member_value(ind,2),'.k');
Note, that if x was arranged as a row vector this will not work, but this is not the case for you.

カテゴリ

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

製品

リリース

R2019b

質問済み:

2019 年 10 月 9 日

コメント済み:

2019 年 10 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by