Identify the closest and furthest points from the origin and mark them
5 views (last 30 days)
Show older comments
hi, im students for electric engineering, and matlab beginner. I want to solve this problem. use this code, and identify the Xmin/Xmax the closest/furthest points from the origin. and evaulate Xmin%Xmax<0.5 and memory. help me please.
code:
poissrnd(100,5,6)
x=-0.5+(0.5+0.5)*rand(1,100);
y =-0.5+(0.5+0.5)*rand(1,100);
axis([-0.5 0.5 -0.5 0.5])
scatter(x,y)
hold on;
idxmin = find(y == max(x));
idxmax = find(y == min(y));
2 Comments
Walter Roberson
on 2 Oct 2022
I would tend to think that you should be finding the Euclidean distance to the origin, not just look at min() and max() of the individual coordinates.
Answers (1)
Image Analyst
on 3 Oct 2022
Try this:
% Original code:
poissrnd(100,5,6);
x=-0.5+(0.5+0.5)*rand(1,100);
y =-0.5+(0.5+0.5)*rand(1,100);
axis([-0.5 0.5 -0.5 0.5])
scatter(x,y)
hold on;
idxmin = find(y == max(x));
idxmax = find(y == min(y));
% Find the distances
distances = sqrt(x .^ 2 + y .^ 2);
% Find the point closest to the origin
[minDistance, indexOfMin] = min(distances)
% Plot it.
scatter(x(indexOfMin), y(indexOfMin), 'r', 'filled')
% Find the point farthest from the origin
[maxDistance, indexOfMax] = max(distances)
% Plot it.
scatter(x(indexOfMax), y(indexOfMax), 'r', 'filled')
grid on;
% Move axes to the origin.
hFig = gca;
hFig.XAxisLocation = 'origin';
hFig.YAxisLocation = 'origin';
0 Comments
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!