How to identify the closest and furthest coordinate from the origin in 3D?

11 views (last 30 days)
Hi, I am sorry if there is couple of posts already on this.
I have further queries regarding this current project I am working on. I explained my query in this POST. I would like to identify the coordinate of the furthest and shortest points from the origin in this deformed sphere to calculate the maximum and minimum diameter.
I would like to identify these points in each of the 3 planes, (XY, YZ, XZ).
I came across this where Image Analyst suggested something similar. I changed some parts of the codes to use the X,Y,Z coordinates of my own sphere and to calculate the distance between the points using the 3D vector. The code I used is attached with this post.
However, the code calculates the distance of the furthest point from each of the points. Would it be possible to calculate the furthest distance of the points from the origin.
I.e:
At the moment, the code outputs the results as:
Point #761 at (-0.0, 0.0) is farthest away from point #1574 at (0.0, -0.0) and is at a distance of 0.010000
  • Can we define the "away from point" as the origin? So the distances could be calculated from that central origin to the other points. This should allow me to identify which point is the furthest and which point is the closest. From this I can calculate the minimum and maximum diameter?
  • The command window outputs the coordinates of the points in 2D
(eg _Point #761 at (-0.0, 0.0)_)
Can it be changed so it displays the 3D coordinates? I changed the code to include the Z axis when calculating the distance but can not change the output display in the command window.
  • In addition to this, can the number of decimal places be increased in the output answer? Since the distances are very small, it require higher number of decimal places. I tried
format long
but it did not increase the decimal places in the results.
I tried another method to calculate the minimum and maximum radius from the origin [0,0,0,] using this code:
[dmax,p] = max(((X1-0)).^2 + ((Y1)-0).^2 + ((Z1)-0).^2);
dmax = sqrt(dmax)
[dmin,p] = min(((X1-0)).^2 + ((Y1)-0).^2 + ((Z1)-0).^2);
dmin = sqrt(dmin)
This displays the minimum and maximum distance but I can not identify the coordinates of the point which is the furthest or shortest.
Thank you.

Accepted Answer

Walter Roberson
Walter Roberson on 6 Feb 2016
d = X1.^2 + Y1.^2 + Z1.^2;
[dmax, maxidx] = max(d);
[dmin, minidx] = min(d);
xmin = X1(minidx); ymin = Y1(minidx); zmin = Z1(minidx);
xmax = X1(maxidx); ymax = Y1(maxidx); zmax = Z1(maxidx);
fprintf('The closest point is (%g,%g,%g) which is distance %g\n', xmin, ymin, zmin, dmin);
fprintf('The furthest point is (%g,%g,%g) which is distance %g\n', xmax, ymax, zmax, dmax);
  1 Comment
Sharunyan Thava
Sharunyan Thava on 12 Feb 2016
Thank you Walter.
It works after I added the "sqrt" to the function "d" in order to the find the distance.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!