Version 3.0 (3.11 KB) by Seth Wagenman
Returns signed angle between two 3-D vectors in plane spanning them with direction determined either by the right hand rule or specification
Updated 2 Jul 2021

View License

Calculate the angle (in radians) between two three-D vectors defined as the angle from startVec to endVec in the plane defined by its normal vector being either the cross product or negation of startVec x endVec. Pass 1 for 'oneCaseFlag' if you are not passing matrices of row vectors.
Usage details for the 'rotateVec' argument are as follows: rotateVec is a user-specified (normal) vector pointing from the side of the plane that startVec and endVec lie in, which points in the direction about which the angle is measured. If you do not know what this normal vector is, then pass an empty vector and the function will substitute their cross product, but in this case the angles will always be positive, so the user must employ some other test external to the function to determine when the angle should be positive or negative based on the directions of input vectors startVec and endVec, and adjust accordingly.
For derivation of the implemented formulas, see StackOverflow answer at: or search for the term "signed angle between two 3D vectors with the same origin within the same plane" in the search bar and look for the 2015 answer by Adrian Leonhard. That proof is the basis of the corresponding post in math.stackexchange:, which explains how to calculate the four-quadrant arctangent based on Adrian Leonhard's proof.

Cite As

Seth Wagenman (2024). signedAngleTwoVectors (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2019b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

Had order of arguments to atan2 backwards! Also included unit3DNonZeroVectorByRow function and updated instances of oneCaseFlat to oneVecFlag per suggestion.


Found logic error in sign check; fixed to be row-wise


Cleaned up code a bit and used unit3DNonZeroVectorByRow instead of prior version of that function by same author.


Changed name of file and function to make 3-D explicit.


Got rid of bug when computing dot products of single vectors (rather than matrices of row vectors) as input arguments.


Made code cleaner and variable names more meaningful


debugged check for coplanarity of rotateVec with startVec, endVec


changed last line to calculate beta (returned object) rather than theta