How can I determine the angle between two vectors in MATLAB? (2024)

1,324 views (last 30 days)

Show older comments

MathWorks Support Team on 22 Jun 2011

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab

Commented: Bruno Luong on 3 Dec 2022

Accepted Answer: MathWorks Support Team

How can I determine the angle between two vectors in MATLAB?

I have two vectors. Is there a MATLAB function that can determine the angle between them?

Sign in to answer this question.

Accepted Answer

MathWorks Support Team on 27 May 2020

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#answer_110938

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#answer_110938

Edited: MathWorks Support Team on 27 May 2020

Open in MATLAB Online

There is no in-built MATLAB function to find the angle between two vectors. As a workaround, you can try the following:

CosTheta = max(min(dot(u,v)/(norm(u)*norm(v)),1),-1);

ThetaInDegrees = real(acosd(CosTheta));

5 Comments

Show 3 older commentsHide 3 older comments

Roger Stafford on 9 Jul 2015

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_297320

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_297320

Edited: James Tursa on 9 Jul 2015

@Mathworks Support Team: I think you will find that Mathworks' 'atan2' function is more accurate than 'acos' for angles that are near zero or pi radians. Just look at a plot of acos(x) to see why.

James Tursa on 24 May 2018

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_571702

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_571702

@Felix: Did you expect something different?

Johannes Kalliauer on 14 Jan 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_786018

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_786018

Open in MATLAB Online

@MathWorks Support Team

u = [1 2 0];

v = [1 0 0];

CosTheta = dot(u,v)/(norm(u)*norm(v));

ThetaInDegrees = acosd(CosTheta);

Almost linear Vectors (Angle of say 8E-10rad) might leed to problems since CosTheta might be slightly bigger than 1, due to nummerical double precission (say something around 1+2E-16) and leed to imaginary angles

u = [1 2 0];

v = [1 0 0];

CosTheta = max(min(dot(u,v)/(norm(u)*norm(v)),1,-1);

ThetaInDegrees = acosd(CosTheta);

Johannes Kalliauer on 3 Feb 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_792963

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_792963

Open in MATLAB Online

@MathWorks Support Team

u=[0.272379472472602111022302462516 1.08301805439555555910790149937 -0.359366773005409555910790149937];

v=[0.2898030626583580555111512312578 1.15229663744866689137553104956 -0.382354774507524222044604925031];

CosTheta = (dot(u,v) / (norm(u)*norm(v)));

if abs(CosTheta)>1

error('MATLAB:odearguments:NumericPrecision','Matlab has numerical issues in calculated angle')

end

leads to an error (imaginär angle), since CosTheta=1+2.22044604925031e-16>1

Solution would be

CosTheta = max(min(dot(u,v)/(norm(u)*norm(v)),1,-1);

ThetaInDegrees = real(acosd(CosTheta));

.

Akihumi on 27 May 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_867093

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_867093

Open in MATLAB Online

Hi, did you miss out a bracket for the min? I got an error and only resolve it with the following code instead.

CosTheta = max(min(dot(u,v)/(norm(u)*norm(v)),1),-1);

ThetaInDegrees = real(acosd(CosTheta));

Sign in to comment.

More Answers (2)

Pierre-Pascal on 11 Jan 2016

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#answer_205759

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#answer_205759

So why doesn't matlab give us a function for that instead of having us look endlessly on forums?

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

James Tursa on 9 Jul 2015

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#answer_185622

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#answer_185622

Edited: James Tursa on 5 Jan 2019

Open in MATLAB Online

This topic has been discussed many times on the Newsgroup forum ... if I looked hard enough I'm sure I could find several Roger Stafford posts from many years ago on this. E.g., here is one of them:

https://www.mathworks.com/matlabcentral/answers/161515-how-do-i-find-the-angle-between-a-vector-and-a-line

The basic acos formula is known to be inaccurate for small angles. A more robust method is to use both the sin and cos of the angle via the cross and dot functions. E.g.,

atan2(norm(cross(u,v)),dot(u,v));

An extreme case to clearly show the difference:

>> a = 1e-10 % start with a very small angle

a =

1e-10

>> u = 4*[1 0 0] % arbitrary non-unit vector in X direction

u =

4 0 0

>> v = 5*[cos(a) sin(a) 0] % vector different from u by small angle

v =

5 5e-10 0

>> acos(dot(u,v)/(norm(u)*norm(v))) % acos formulation does not recover the small angle

ans =

>> atan2(norm(cross(u,v)),dot(u,v)) % atan2 formulation does recover the small angle

ans =

1e-10

3 Comments

Show 1 older commentHide 1 older comment

Johannes Kalliauer on 14 Jan 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_786021

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_786021

Thanks, sometimes even imaginäry Angles occur if using acos-function.

James Tursa on 3 Feb 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_793139

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_793139

To get a full circle result where "direction" of the angle is important, see this link for one possible strategy:

https://www.mathworks.com/matlabcentral/answers/501449-angle-betwen-two-3d-vectors-in-the-range-0-360-degree

Bruno Luong on 3 Dec 2022

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_2498942

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_2498942

@Felix Fischer If you want to find angles of multiple vector pairs put in matrix, use vecnorm rather than norm.

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLAB

Find more on MATLAB in Help Center and File Exchange

Tags

  • angle
  • vectors
  • dot
  • theta

Products

  • MATLAB

Release

R13SP1

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How can I determine the angle between two vectors in MATLAB? (13)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

How can I determine the angle between two vectors in MATLAB? (2024)
Top Articles
Alphega Apotheek De Schans | Alphega
Was ist ein Thumbnail? Einfach erklärt mit Anwendungsbeispielen
Kostner Wingback Bed
Nullreferenceexception 7 Days To Die
Bleak Faith: Forsaken – im Test (PS5)
Midflorida Overnight Payoff Address
Rek Funerals
Tap Tap Run Coupon Codes
Optimal Perks Rs3
Mail Healthcare Uiowa
Optum Medicare Support
Https Www E Access Att Com Myworklife
Heska Ulite
General Info for Parents
Daily Voice Tarrytown
Toy Story 3 Animation Screencaps
Ess.compass Associate Login
Where to Find Scavs in Customs in Escape from Tarkov
Lcwc 911 Live Incident List Live Status
Sadie Proposal Ideas
Epguides Strange New Worlds
The Ultimate Guide to Extras Casting: Everything You Need to Know - MyCastingFile
Myhr North Memorial
Okc Body Rub
Shadbase Get Out Of Jail
Rs3 Ushabti
Divide Fusion Stretch Hoodie Daunenjacke für Herren | oliv
Expression Home XP-452 | Grand public | Imprimantes jet d'encre | Imprimantes | Produits | Epson France
Possum Exam Fallout 76
Alternatieven - Acteamo - WebCatalog
Ewg Eucerin
Mia Malkova Bio, Net Worth, Age & More - Magzica
Bursar.okstate.edu
Opsahl Kostel Funeral Home & Crematory Yankton
RFK Jr., in Glendale, says he's under investigation for 'collecting a whale specimen'
Serenity Of Lathrop - Manteca Photos
Robot or human?
Craigslist Lakeside Az
The Complete Guide To The Infamous "imskirby Incident"
Studio 22 Nashville Review
Myql Loan Login
What Is Kik and Why Do Teenagers Love It?
Craigslist Mexicali Cars And Trucks - By Owner
Callie Gullickson Eye Patches
Www Craigslist Com Atlanta Ga
Mybiglots Net Associates
Crigslist Tucson
Wwba Baseball
Peugeot-dealer Hedin Automotive: alles onder één dak | Hedin
Anthony Weary Obituary Erie Pa
Haunted Mansion Showtimes Near The Grand 14 - Ambassador
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 5998

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.