Select a point on the graph

3 views (last 30 days)
luca on 9 Oct 2019
Commented: luca on 9 Oct 2019
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 And I want to select the red point that is closest to the origin.
May someone help me with the code?
luca on 9 Oct 2019
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 on 9 Oct 2019
Edited: Adam Danz on 9 Oct 2019
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
luca on 9 Oct 2019
Thanks Adam ! now its clear how it works

Turlough Hughes on 9 Oct 2019
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.

R2019b

Community Treasure Hunt

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

Start Hunting!