You don't need pdist2() because you aren't asking for the distance of every point to every other point. You're only asking for the distance from every point to the single point at (0, 0). So you can simply use sqrt()! Try this:
xy = rand(10, 2);
x = xy(:, 1);
y = xy(:, 2);
plot(x, y, 'bo', 'MarkerSize', 10);
grid on;
distances = sqrt(xy(: , 1) .^ 2 + xy(:, 2) .^ 2)
[minDistance, indexOfMin] = min(distances);
closestX = x(indexOfMin);
closestY = y(indexOfMin);
hold on;
plot(closestX, closestY, 'r*', 'MarkerSize', 8, 'LineWidth', 2);
line([0, closestX], [0, closestY], 'LineWidth', 2, 'Color', 'r');
Best Answer