jarhead
jarhead

Reputation: 1901

Angles between n vectors - Matlab

Consider a set of points (just an example)

x = [0 1 2 5 4 8 5 6];
y = [5 8 4 2 5 6 4 5];

and another reference point:

xc=1;
yc=1;

In which I use to represent these points as vectors:

vec=[x-xc y-yc];

I wish to obtain a matrix with all the angles between all vectors which is obtained by the calculation (for single vectors)

angle = acosd(dot(v,u)/norm(u)*norm(v));

How can I obtain this calculation in a few lines without going vector by vector in a loop? In my calculation the number of points is very very large.

Upvotes: 1

Views: 155

Answers (1)

ViG
ViG

Reputation: 1868

I think you mean vec = [x-xc; y-yc];. To calucate the dotproduct between all rows, you can use

vec.'*vec

The norm (Euclidean) of each vector can be determined as

no = sqrt(sum(vec.*vec,1))

The product of the different norms can be calculated the same as for vec:

no.'*no

The angles can thus be found as

no = sqrt(sum(vec.*vec,1));
angles = acosd(vec.'*vec./(no.'*no));

Upvotes: 6

Related Questions