COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Re: fast angle calculation between 2D vectors

• Email
• Follow

```This function calculates angels between to groups of vectors

function angle=Angle(vects1, vects2)%Unit vectors
beam_dot=sum(vects1'.*vects2');
angle= acos(beam_dot)*180/pi;
end
```
 0
Reply ramzib2 (12) 2/9/2011 9:03:38 PM

See related articles to this posting

```Ramzi <ramzib2@gmail.com> wrote in message <21f0eea5-4684-4cd9-bf8a-24034a868818@t8g2000vbd.googlegroups.com>...
> This function calculates angels between to groups of vectors
>
>     function angle=Angle(vects1, vects2)%Unit vectors
>       beam_dot=sum(vects1'.*vects2');
>       angle= acos(beam_dot)*180/pi;
>     end

You can do it faster this way (probably):

function angle=Angle(vects1, vects2)%Unit vectors

angle= acosd(vects1(:).'*vects2(:));

end

```
 0

```On Feb 9, 4:03=A0pm, Ramzi <ramz...@gmail.com> wrote:
> This function calculates angels between to groups of vectors
>
> =A0 =A0 function angle=3DAngle(vects1, vects2)%Unit vectors
> =A0 =A0 =A0 beam_dot=3Dsum(vects1'.*vects2');
> =A0 =A0 =A0 angle=3D acos(beam_dot)*180/pi;
> =A0 =A0 end

Why not just

function angle=3DAngle(vects1, vects2
angle=3D acos(vects1.*vects2)*180/pi;
end

Hope this helps.

Greg
```
 0

```Dear Ramzi,

> This function calculates angels between to groups of vectors
>
>     function angle=Angle(vects1, vects2)%Unit vectors
>       beam_dot=sum(vects1'.*vects2');
>       angle= acos(beam_dot)*180/pi;
>     end

Do you have a question?
What sizes do the inputs have?

Add parenthesis around 180/pi, if beam_dot is a large vector:
angle = acos(beam_dot)*(180 / pi);
Otherwise you multiply the vector at first with 180 and divide the vector afterwards by pi. With the parenthesis only one division is performed.

Kind regards, Jan
```
 0

```"Jan Simon" wrote in message <iiva3g\$1gd\$1@fred.mathworks.com>...
> Dear Ramzi,
>
> > This function calculates angels between to groups of vectors
> >
> >     function angle=Angle(vects1, vects2)%Unit vectors
> >       beam_dot=sum(vects1'.*vects2');
> >       angle= acos(beam_dot)*180/pi;
> >     end
>
> Do you have a question?

Perhaps the OP was thinking this is the FEX.
```
 0

```On Feb 10, 1:52=A0am, "Matt Fig" <spama...@yahoo.com> wrote:
> "Jan Simon" wrote in message <iiva3g\$1g...@fred.mathworks.com>...
> > Dear Ramzi,
>
> > > This function calculates angels between to groups of vectors
>
> > > =A0 =A0 function angle=3DAngle(vects1, vects2)%Unit vectors
> > > =A0 =A0 =A0 beam_dot=3Dsum(vects1'.*vects2');
> > > =A0 =A0 =A0 angle=3D acos(beam_dot)*180/pi;
> > > =A0 =A0 end
>
> > Do you have a question?
>
> Perhaps the OP was thinking this is the FEX.

Thanks for all
I want a fastest way to find angles between two groups of vectors (one
by one), because I have a large number of vectors.
```
 0

```On Feb 9, 10:22=A0pm, "Matt J " <mattjacREM...@THISieee.spam> wrote:
> Ramzi <ramz...@gmail.com> wrote in message <21f0eea5-4684-4cd9-bf8a-24034=
> > This function calculates angels between to groups of vectors
>
> > =A0 =A0 function angle=3DAngle(vects1, vects2)%Unit vectors
> > =A0 =A0 =A0 beam_dot=3Dsum(vects1'.*vects2');
> > =A0 =A0 =A0 angle=3D acos(beam_dot)*180/pi;
> > =A0 =A0 end
>
> You can do it faster this way (probably):
>
> =A0 =A0 function angle=3DAngle(vects1, vects2)%Unit vectors
>
> =A0 =A0 =A0 angle=3D acosd(vects1(:).'*vects2(:));
>
> =A0 =A0 end

I test that, but it gives wrong result! thanks
```
 0

```On Feb 10, 1:05=A0am, "Jan Simon" <matlab.THIS_Y...@nMINUSsimon.de>
wrote:
> Dear Ramzi,
>
> > This function calculates angels between to groups of vectors
>
> > =A0 =A0 function angle=3DAngle(vects1, vects2)%Unit vectors
> > =A0 =A0 =A0 beam_dot=3Dsum(vects1'.*vects2');
> > =A0 =A0 =A0 angle=3D acos(beam_dot)*180/pi;
> > =A0 =A0 end
>
> Do you have a question?
> What sizes do the inputs have?
>
> Add parenthesis around 180/pi, if beam_dot is a large vector:
> =A0 angle =3D acos(beam_dot)*(180 / pi);
> Otherwise you multiply the vector at first with 180 and divide the vector=
afterwards by pi. With the parenthesis only one division is performed.
>
> Kind regards, Jan

This is a good remark! but is acosd faster?
```
 0

```Ramzi <ramzib2@gmail.com> wrote in message <9fa6747f-10a4-448d-8027-cb0b80943712@k16g2000vbq.googlegroups.com>...
>
> > You can do it faster this way (probably):
> >
> >     function angle=Angle(vects1, vects2)%Unit vectors
> >
> >       angle= acosd(vects1(:).'*vects2(:));
> >
> >     end
>
> I test that, but it gives wrong result! thanks
===============

Works fine for me. Below is the code that I used to test it. It gives excellent agreement between our two implementations.

function test

v1=rand(1,5); v1=v1/norm(v1);
v2=rand(1,5); v2=v2/norm(v2);

AngleRamzi(v1,v2) - AngleMatt(v1,v2) % -1.7764e-014

end

function angle=AngleMatt(vects1, vects2)%Unit vectors
angle= acosd(vects1(:).'*vects2(:));
end

function angle=AngleRamzi(vects1, vects2)%Unit vectors
beam_dot=sum(vects1'.*vects2');
angle= acos(beam_dot)*180/pi;
end

```
 0

```Ramzi <ramzib2@gmail.com> wrote in message <21f0eea5-4684-4cd9-bf8a-24034a868818@t8g2000vbd.googlegroups.com>...
> This function calculates angels between to groups of vectors
>
>     function angle=Angle(vects1, vects2)%Unit vectors
>       beam_dot=sum(vects1'.*vects2');
>       angle= acos(beam_dot)*180/pi;
>     end

Almost certainly slower, but more accurate and numerically robust is Roger Stafford's suggested implementation

angle = atan2(norm(cross(a,b)),dot(a,b))

which also includes the normalizing step.

It is restricted to 3D, but the 2D case is trivial and I can't imagine angles having a physical meaning in higher dimensions.
```
 0

```On Feb 10, 5:43=A0pm, "Matt J " <mattjacREM...@THISieee.spam> wrote:
> Ramzi <ramz...@gmail.com> wrote in message <9fa6747f-10a4-448d-8027-cb0b8=
>
> > > You can do it faster this way (probably):
>
> > > =A0 =A0 function angle=3DAngle(vects1, vects2)%Unit vectors
>
> > > =A0 =A0 =A0 angle=3D acosd(vects1(:).'*vects2(:));
>
> > > =A0 =A0 end
>
> > I test that, but it gives wrong result! thanks
>
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
>
> Works fine for me. Below is the code that I used to test it. It gives exc=
ellent agreement between our two implementations.
>
> function test
>
> v1=3Drand(1,5); v1=3Dv1/norm(v1);
> v2=3Drand(1,5); v2=3Dv2/norm(v2);
>
> AngleRamzi(v1,v2) - AngleMatt(v1,v2) % -1.7764e-014
>
> end
>
> =A0 =A0 function angle=3DAngleMatt(vects1, vects2)%Unit vectors
> =A0 =A0 =A0 angle=3D acosd(vects1(:).'*vects2(:));
> =A0 =A0 end
>
> =A0 =A0 function angle=3DAngleRamzi(vects1, vects2)%Unit vectors
> =A0 =A0 =A0 beam_dot=3Dsum(vects1'.*vects2');
> =A0 =A0 =A0 angle=3D acos(beam_dot)*180/pi;
> =A0 =A0 end

Thanks
but vector is determind in 2D by two coord so you must test for
v1=3Drand(5,2); v1=3Dnormr(v1);
> v2=3Drand(5,2); v2=3Dnormr(v2);
and you will find what I said
BR
```
 0

```"Mark Shore" wrote in message <ij1937\$g35\$1@fred.mathworks.com>...
> I can't imagine angles having a physical meaning in higher dimensions.

Why that? The angle between subspaces are fundamental notion in linear algebra, statistics, etc... Vectors can be considered subspace of dimension 1.

Bruno
```
 0

```"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ij1f81\$qvf\$1@fred.mathworks.com>...
> "Mark Shore" wrote in message <ij1937\$g35\$1@fred.mathworks.com>...
> > I can't imagine angles having a physical meaning in higher dimensions.
>
> Why that? The angle between subspaces are fundamental notion in linear algebra, statistics, etc... Vectors can be considered subspace of dimension 1.
======

But do we ever measure the angle between N>3 dimensional spaces in degrees?
```
 0

```"Matt J" wrote in message <ij1fug\$cco\$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ij1f81\$qvf\$1@fred.mathworks.com>...
> > "Mark Shore" wrote in message <ij1937\$g35\$1@fred.mathworks.com>...
> > > I can't imagine angles having a physical meaning in higher dimensions.
> >
> > Why that? The angle between subspaces are fundamental notion in linear algebra, statistics, etc... Vectors can be considered subspace of dimension 1.
> ======
>
> But do we ever measure the angle between N>3 dimensional spaces in degrees?

Exactly. While the scalar product is easily envisioned in arbitrary dimensions, I personally have great difficulty understanding what is meant by an angle between higher-dimensional vectors and suspect esoteric n-dimensional spaces were not what the OP intended.
```
 0

```"Matt J" wrote in message <ij1fug\$cco\$1@fred.mathworks.com>...

>
> But do we ever measure the angle between N>3 dimensional spaces in degrees?

Now when I think about it, I actually did. When I codded my conjugate method I did a while a go, I believe the angle between the gradient and conjugate gradient direction are logged in degree.

Bruno
```
 0
Reply b.luong5955 (6403) 2/10/2011 10:18:04 PM

```"Mark Shore" wrote in message <ij1lvq\$djv\$1@fred.mathworks.com>...
>
> >
> > But do we ever measure the angle between N>3 dimensional spaces in degrees?
>
> Exactly. While the scalar product is easily envisioned in arbitrary dimensions, I personally have great difficulty understanding what is meant by an angle between higher-dimensional vectors and suspect esoteric n-dimensional spaces were not what the OP intended.
=================

Although .... even in (N>3)-dimensional space, two vectors V1 and V2 will occupy a two dimensional plane that is 1-1 wtih R^2. That means there are corresponding vectors v1 and v2 in R^2, separated by an angle that _is_ something we know how to visualize physically. Moreover, the angle between v1 and v2 will be the same as if calculated using V1 and V2

angle(v1,v2)=acosd(  dot(V1,V2)/norm(V1)/norm(V2) )
```
 0