From: Steve on
Hi!

Can somebody please help me?

I calculate the minimal distance between a reference point and several line segments.

% Generate random lines L and one Reference Point P
L = rand(5,4); %[x1, y1, x2, y2]
P = rand(1,2); %[x, y]
%
% Calculate the squared lengths of all lines
Px = L(:,3) - L(:,1);
Py = L(:,4) - L(:,2);
Ls = Px.^2 + Py.^2;
%
% Calculate the minimal distances between the Reference point and the
% lines.
ua = ( (P(1)-L(:,1)).*Px + (P(2)-L(:,2)).*Py ) ./ Ls;
ua(ua>1) = 1;
ua(ua<0) = 0;
Cx = L(:,1) + ua.*Px;
Cy = L(:,2) + ua.*Py;
C = [Cx, Cy];

Cx and Cy are now the coordinates of the points on the line segments, that have the minimal distance to my reference point.

So, my problem is that I would need that for more than one Reference points. I packed this code into a loop and calculate it for every single Point, but I need to solve it in a more efficient way.

Can somebody please show me how I can get this coordinates for several points and line segments without a loop? I use this function in a loop very often with a lot of lines and points, so every Millisecond counts :-)!

Thanks very much!
From: Yi Cao on
"Steve " <stefan.griesser(a)alumni.unileoben.ac.at> wrote in message <i3qssn$915$1(a)fred.mathworks.com>...
> Hi!
>
> Can somebody please help me?
>
> I calculate the minimal distance between a reference point and several line segments.
>
> % Generate random lines L and one Reference Point P
> L = rand(5,4); %[x1, y1, x2, y2]
> P = rand(1,2); %[x, y]
> %
> % Calculate the squared lengths of all lines
> Px = L(:,3) - L(:,1);
> Py = L(:,4) - L(:,2);
> Ls = Px.^2 + Py.^2;
> %
> % Calculate the minimal distances between the Reference point and the
> % lines.
> ua = ( (P(1)-L(:,1)).*Px + (P(2)-L(:,2)).*Py ) ./ Ls;
> ua(ua>1) = 1;
> ua(ua<0) = 0;
> Cx = L(:,1) + ua.*Px;
> Cy = L(:,2) + ua.*Py;
> C = [Cx, Cy];
>
> Cx and Cy are now the coordinates of the points on the line segments, that have the minimal distance to my reference point.
>
> So, my problem is that I would need that for more than one Reference points. I packed this code into a loop and calculate it for every single Point, but I need to solve it in a more efficient way.
>
> Can somebody please show me how I can get this coordinates for several points and line segments without a loop? I use this function in a loop very often with a lot of lines and points, so every Millisecond counts :-)!
>
> Thanks very much!

help bsxfun

hth
Yi
From: Steve on
> help bsxfun
>
> hth
> Yi

I know bsxfun but it doesn't help me here...
From: Yi Cao on
"Steve " <stefan.griesser(a)alumni.unileoben.ac.at> wrote in message <i3re42$en8$1(a)fred.mathworks.com>...
> > help bsxfun
> >
> > hth
> > Yi
>
> I know bsxfun but it doesn't help me here...

Well, here are some hints:

1. precalculate, PLx = Px./Ls; PLy = Py./Ls; LPLx = L(:,1).*PLX; LPLy = L(:,2).*PLy;
2. calculate uax=P(:,1)*PLx'; and uay=P(:,2)*PLy';.
3. Apply bsxfun to get ua as a matrix ua = bsxfun(@minus, usx, LPLx) + bsxfun(@minus, usx, LPLx).

The rest should be easy.

HTH
Yi
 | 
Pages: 1
Prev: Help on error
Next: Need Google AdSense Account