Select a point on the graph
7 views (last 30 days)
Hi given the following code,
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?
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);
hypot() method avoids potential under/overflow: https://www.mathworks.com/help/matlab/ref/hypot.html
More Answers (1)
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.