angle between two vectors in matlab

To find the angle (in degrees) between two vectors u and v in Matlab, you can use the dot product formula as follows:

main.m
angle_in_radians = acos(dot(u, v) / (norm(u) * norm(v)));
angle_in_degrees = angle_in_radians * 180 / pi;
106 chars
3 lines

Here, dot(u, v) computes the dot product of u and v, and norm(u) and norm(v) compute their Euclidean (L2) norms. The acos function returns the inverse cosine in radians, which is then converted into degrees by multiplying with 180/pi.

Note that the above formula assumes that both u and v are column vectors of the same size. If u and v are row vectors or have different sizes, you may need to transpose or reshape them accordingly before computing the dot product or norms.

gistlibby LogSnag