MATLAB: How to get the coordinates of the minimum and maximum value

coordinatemaximumminimum

for i=1:n
x=100*rand(1,i);
y=100*rand(1,i); end
d=sqrt((0-x).^2+(0-y).^2);
disp(d)
NP =(min(d)); %how to get coordinates of this
FP =(max(d)); %and this
plot(x,y,'b*')
hold on
how could i get the coordinates of the nearest point (NP) and farthest point (FP)?

Best Answer

Use the second return argument of max and min:
[minValue, indexOfMinValue = min(d);
[maxValue, indexOfMaxValue = max(d);
That essentially gives you [y, index]. To get x and y:
xMin = x(indexOfMinValue);
yMin = minValue;
xMax = x(indexOfMaxValue);
yMax = maxValue;
Related Question