计算两个向量之间的夹角 [英] Calculate angle between two vectors matlab
问题描述
很抱歉,这个问题似乎很基本,但是我现在还没有找到一个好的答案.
I'm sorry if this question seems a really basic, but I cannot find a good answer online yet.
我对向量以及如何在Matlab中使用向量感到困惑.目前,我具有以下三对坐标(x和y):人1,人1和人2的未来位置.请参见:
I'm a little confused with vectors and how to use them in matlab. At the moment I have the following three pair of coordinates (x and y): Person 1, The future location of Person 1 and Person 2. See: The three points in a 2d view
现在,我想计算从人1到人2的向量"与从人1到人1的向量的未来"之间的夹角.我已经找到了一些可以使用矢量实现的matlab函数,但是我不确定我实际上是否对每个矢量都使用了正确的输入.
Now I want to calculate the angle between "the vector which goes from person 1 to person 2" and "the vector from person 1 to person 1 future". I've found some matlab functions which could do this with the vectors, but I am not sure I am actually using the right input for each vector.
所以现在我的问题是如何使用这些坐标(以及它们之间的差)来计算图像的角度?
So now my question is how can I use these coordinates (and the difference between them) to calculate the angle from the image?
推荐答案
尽管StefanM的解决方案对此非常普遍,但实际上在计算上是昂贵的,但最重要的是,当向量为微小和/或角度接近0或π-实际上可能导致角度稍微为负或稍微超过π.
Although StefanM's solution is a pretty common solution to this, it is actually computationally expensive, but most importantly, incorrect when the vectors are tiny and/or the angle is near 0 or π -- it can actually result in angles that are slightly negative, or slightly exceed π.
因此,它给出了错误的鲁棒性. 我反而建议
Thus, it gives a false sense of robustness. I'd instead suggest
theta = acos(min(1,max(-1, a(:).' * b(:) / norm(a) / norm(b) )));
更健壮,更正确,循环运行时快10倍以上,并且无需书面文件即可被外行理解,因为它仍然与经典"公式相符.
More robust, more correct, over 10× faster when run in a loop, and understandable by laymen without documentation, because it still agrees mostly with the "classical" formula.
这篇关于计算两个向量之间的夹角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!