How to get the coordinates of my minimum and maximum value?
36 views (last 30 days)
Show older comments
Jervin Jamilla
on 12 Apr 2015
Commented: Roger Stafford
on 12 Apr 2015
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
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.
Accepted Answer
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;
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!