Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Point with the closest distance

Subject: Point with the closest distance

From: Deepak

Date: 2 May, 2011 06:32:04

Message: 1 of 8

I have a random set of points in the form:

M = [x; y; z]
where x,y and z are columns of x,y and z co-ordinates.

this data forms a random cylindrical set of points around the Z-axis.

I want to find out the closest point from the set to the axis. Once this is done, i want to know the point and store it in a separate matrix. Then i want to remove this point from the Set and find the next closest point. I want to do this till all the points are exhausted,

Any help would be appreciated. Thanks

Subject: Point with the closest distance

From: ImageAnalyst

Date: 2 May, 2011 10:22:26

Message: 2 of 8

Let's cut to the chase. Here is your final result:
M = [];
No loop is necessary.

Subject: Point with the closest distance

From: Matt J

Date: 2 May, 2011 14:52:05

Message: 3 of 8

"Deepak " <deepak.tss@gmail.com> wrote in message <iplj54$c3t$1@fred.mathworks.com>...
>
> this data forms a random cylindrical set of points around the Z-axis.
>
> I want to find out the closest point from the set to the axis.
==================

If the points are "cylindrical" doesn't that mean they lie on a cylindrical surface centered on the Z-axis. If so, they would be equidistant from the Z-axis, so how could there be a "closest point"?

Subject: Point with the closest distance

From: Nasser M. Abbasi

Date: 2 May, 2011 14:57:58

Message: 4 of 8

On 5/2/2011 7:52 AM, Matt J wrote:
> "Deepak "<deepak.tss@gmail.com> wrote in message<iplj54$c3t$1@fred.mathworks.com>...
>>
>> this data forms a random cylindrical set of points around the Z-axis.
>>
>> I want to find out the closest point from the set to the axis.
> ==================
>


> If the points are "cylindrical" doesn't that mean they lie on a cylindrical surface centered on the Z-axis.

No. It just means the coordinates of the points are expressed in cylinderical coordinates (r,theta,z).

They can be any where.

--Nasser

Subject: Point with the closest distance

From: ImageAnalyst

Date: 2 May, 2011 15:48:39

Message: 5 of 8

No it doesn't since he specifically said they were x,y,z not
r,theta,z. And he said they're random so I guess they're only
approximately lying along a cylinder. So all he has to do it
calculate x.^2 + y.^2 and use standard intuitive basic programming
techniques to find the min of those squared distances, store it
somewhere and remove it from the M list. Keep looping until there are
no more points left in M.

Alternatively he might add a column to M that is the squared distance
for each (x,y,z) row, then sort on that column. This is probably the
easiest way. Just two lines of code (not including comments which
should always be added).

Subject: Point with the closest distance

From: Deepak

Date: 2 May, 2011 19:32:22

Message: 6 of 8

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <41a34a6d-b68d-44e2-8352-9e696d33341a@d19g2000prh.googlegroups.com>...
> No it doesn't since he specifically said they were x,y,z not
> r,theta,z. And he said they're random so I guess they're only
> approximately lying along a cylinder. So all he has to do it
> calculate x.^2 + y.^2 and use standard intuitive basic programming
> techniques to find the min of those squared distances, store it
> somewhere and remove it from the M list. Keep looping until there are
> no more points left in M.
>
> Alternatively he might add a column to M that is the squared distance
> for each (x,y,z) row, then sort on that column. This is probably the
> easiest way. Just two lines of code (not including comments which
> should always be added).

Hi,

Am sorry I should have explained more specifically.

n = 40;

h = rand(n,1)*100;
r = 10+(rand(n,1)-0.9)*3;
theta = 360*rand(n,1);
x=r.*cos(theta);
y=r.*sin(theta);
X=ones(n,3);
for i=1:n
    X(i,1)=x(i,1);
    X(i,2)=y(i,1);
    X(i,3)=h(i,1);
end

These are the set of points i generate. Then i find the least squares axis. Translate and rotate the axis to allign it to the Z-Axis. Then i do the same transformations to the surrounding points so that they arrange themselves around the Z-axis as before.

Now i need to find the closest point from the axis to the point cloud.

Subject: Point with the closest distance

From: ImageAnalyst

Date: 2 May, 2011 21:13:37

Message: 7 of 8

On May 2, 3:32 pm, "Deepak " <deepak....@gmail.com> wrote:
> Now i need to find the closest point from the axis to the point cloud.
------------------------------------------------------------------------------------
I'm not sure why you didn't try my sorting suggestion. Any reason?
Just add this to the end of your code:

% Plot it
scatter3(X(:,1), X(:,2), X(:,3));

% Calculate radius squared.
radiiSquared = X(:,1).^2 + X(:,2) .^2
% Sort from smallest radius to largest radius.
[sortedRadii, sortIndexes] = sort(radiiSquared)
% Create the output array.
output = X(sortIndexes, :)

It's a simple matter to subtract off the value of the origin (your
least squares center line) to get the radii squared in case you want
to do that.

Subject: Point with the closest distance

From: Roger Stafford

Date: 3 May, 2011 01:36:04

Message: 8 of 8

"Deepak " <deepak.tss@gmail.com> wrote in message <ipn0s6$gdn$1@fred.mathworks.com>...
> ......
> These are the set of points i generate. Then i find the least squares axis. Translate and rotate the axis to allign it to the Z-Axis. Then i do the same transformations to the surrounding points so that they arrange themselves around the Z-axis as before.
>
> Now i need to find the closest point from the axis to the point cloud.
- - - - - - - -
  As I see it, your key problem is to "find the least squares axis" which you mentioned. It would really be the least squares axis/radius combination, wouldn't it? The remaining steps would be relatively easy to perform. An important question is, what quantity are you to find the least squares of? Is it the squares of the orthogonal distances from your points to the surface of some cylinder? There are five degrees of freedom in defining any circular cylindrical surface, and minimizing the above mean square does not appear to be a linear problem that you could solve with, say, 'svd' or matrix division. It looks as though it would require the use of some optimizing function in the Optimization Toolbox with perhaps a five-parameter optimization. Note that you could presumably not carry your proposed point-by-point elimination below five points, because with any five points, in
general they would determine a unique cylinder with zero mean square deviation for the points.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us