Identify the closest and furthest points from the origin and mark them
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Torsten
el 2 de Oct. de 2022
use this code, and identify the Xmin/Xmax the closest/furthest points from the origin. and evaulate Xmin%Xmax<0.5 and memory.
I don't understand what you write.
Walter Roberson
el 2 de Oct. de 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.
Respuestas (1)
Image Analyst
el 3 de Oct. de 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 comentarios
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!