How to get the coordinates of my minimum and maximum value?

36 views (last 30 days)
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)?
  1 Comment
Roger Stafford
Roger Stafford on 12 Apr 2015
As you have written the for-loop, it is overwriting each x and y from 1 to n-1. You could just as well have written
x=100*rand(1,n);
y=100*rand(1,n);
without the for-loop. The result would be the same.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 12 Apr 2015
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;

More Answers (0)

Community Treasure Hunt

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

Start Hunting!