```Path: news.mathworks.com!not-for-mail
From: "Bruno Luong" <b.luong@fogale.findmycountry>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Angle between two vectors
Date: Thu, 10 Mar 2011 07:56:04 +0000 (UTC)
Organization: FOGALE nanotech
Lines: 42
Message-ID: <ila06k\$t8g\$1@fred.mathworks.com>
NNTP-Posting-Host: www-01-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1299743764 29968 172.30.248.46 (10 Mar 2011 07:56:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 10 Mar 2011 07:56:04 +0000 (UTC)
Xref: news.mathworks.com comp.soft-sys.matlab:714967

"Marwan N" wrote in message <il9t55\$en6\$1@fred.mathworks.com>...
> hey guys,
>
> I went thro the codes u all gave several times....all are different ways for the same pupose....which finally gave me a head spin after all those cods in my head. As a simple conclusion can someone answer me the following:
>
> 1. how do we calculate the angle (consider both the obtuse and acute angle) between two vectors (consider that they intersect at origin or in other words, they originate from origin)
>
> 2.the vectors start at the origin and terminate at the points X=[coresponding coeff of i j k] and Y=[corresponding coeff of i j k]
>
> 3.the results (obtuse and acute angle) should lie between 0 and pi.
>
> any bright ideas anyone??

>> a=randn(2,1)

a =

1.4897
1.4090

>> b=randn(2,1)

b =

1.4172
0.6715

% Pad 0 to extend in 3D if needed
>> a(3)=0
>> b(3)=0

% Roger method

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

angle =

0.3151

angle will be [0,pi] since norm(cross(a,b)) >= 0.

Bruno
```