Identify the closest and furthest points from the origin and mark them

4 views (last 30 days)
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
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.

Sign in to comment.

Answers (1)

Image Analyst
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)
minDistance = 0.0373
indexOfMin = 75
% Plot it.
scatter(x(indexOfMin), y(indexOfMin), 'r', 'filled')
% Find the point farthest from the origin
[maxDistance, indexOfMax] = max(distances)
maxDistance = 0.6770
indexOfMax = 26
% Plot it.
scatter(x(indexOfMax), y(indexOfMax), 'r', 'filled')
grid on;
% Move axes to the origin.
hFig = gca;
hFig.XAxisLocation = 'origin';
hFig.YAxisLocation = 'origin';

Categories

Find more on Strategy & Logic in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!