### Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

## How to calculate a rotation matrix between two 3d points

さんによって質問されました 2019 年 2 月 20 日

さんによって コメントされました 2019 年 2 月 20 日
Jos (10584)

### Jos (10584) (view profile)

さんの 回答が採用されました
Hi everyone!
I have two vectors that represent one point with respect to two different reference systems, eg, p0=[x0, y0, z0] and p1=[x1, y1, z1]; I need to know wich is the rotation matrix that transform the vector p1 to the vector p0. I absolutely don't know the angle rotation, neither the axis around wich the rotation is carried out.
I've tried to use 'vrrotvec' function and then 'vrrotvec2mat' to convert rotation from axis-angle to matrix representation; in theory, if I use this two functions to calculate the rotation matrix R between p1 and p0, when I compute R*p1 I should obtain p0, but the outcome is a vector different from p0.
I hope I've been clear!
Do you have any suggestion?
Thank you!!

#### 0 件のコメント

サインイン to comment.

## 1 件の回答

2019 年 2 月 20 日

### Jos (10584) (view profile)

2019 年 2 月 20 日
採用された回答

You can used dot and cross products to get the rotation matrix:
% two random 3D vectors
p0 = randi(10,3,1)
p1 = randi(10,3,1)
% calculate cross and dot products
C = cross(p0, p1) ;
D = dot(p0, p1) ;
NP0 = norm(p0) ; % used for scaling
if ~all(C==0) % check for colinearity
Z = [0 -C(3) C(2); C(3) 0 -C(1); -C(2) C(1) 0] ;
R = (eye(3) + Z + Z^2 * (1-D)/(norm(C)^2)) / NP0^2 ; % rotation matrix
else
R = sign(D) * (norm(p1) / NP0) ; % orientation and scaling
end
% R is the rotation matrix from p0 to p1, so that (except for round-off errors) ...
R * p0 % ... equals p1
inv(R) * p1 % ... equals p0