假设我有这样一个矩阵
df = pd.DataFrame(randint(2,size=(3,9)))
df.values
array([[0, 1, 0, 1, 1, 1, 0, 1, 1],
[1, 0, 1, 1, 1, 1, 0, 0, 1],
[0, 0, 0, 1, 0, 0, 1, 1, 0]])
同样,本例中的每一行表示三个需要旋转的三维坐标,例如,以下旋转矩阵:
array([[ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00],
[ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00],
[ -1.00000000e+00, 0.00000000e+00, 0.00000000e+00]])
为了尽可能有效地做到这一点(实际问题有数百万坐标),我有些困惑,我不得不这样做:
首先应用df.reshape
--本例中的每一行都包含三个3D坐标,即: so (x,y,z),(x,y,z),(x,y,z)
array([[0, 1, 0],
[1, 1, 1],
[0, 1, 1],
[1, 0, 1],
[1, 1, 1],
[0, 0, 1],
[0, 0, 0],
[1, 0, 0],
[1, 1, 0]])
然后,为了得到轮流到约定,必须取u_new = R \dot u
,这意味着上面的转置,这样我们就可以用旋转矩阵进行一列方向(即坐标)乘法。
array([[0, 1, 0, 1, 1, 0, 0, 1, 1],
[1, 1, 1, 0, 1, 0, 0, 0, 1],
[0, 1, 1, 1, 1, 1, 0, 0, 0]])
然后我们可以做乘法:
pd.DataFrame(dot(rotmat,df)).values
array([[ 0.00e+00, 2.22e-16, 0.00e+00, 1.00e+00, 2.22e-16,
2.22e-16, 1.00e+00, 1.00e+00, 2.22e-16],
[ 1.00e+00, 0.00e+00, 1.00e+00, 1.00e+00, 1.00e+00,
1.00e+00, 0.00e+00, 0.00e+00, 1.00e+00],
[ 0.00e+00, -1.00e+00, 0.00e+00, -1.00e+00, -1.00e+00,
-1.00e+00, 2.22e-16, -1.00e+00, -1.00e+00]])
然后反转整个过程,使其回到原来的形状,用于其他目的。
当然,必须有更有效的方法来做到这一点(希望不干扰旋转矩阵)?
发布于 2016-09-05 23:16:28
在您完成转换之前,这不应该触及数据文件。
a = np.array([
[0, 1, 0, 1, 1, 1, 0, 1, 1],
[1, 0, 1, 1, 1, 1, 0, 0, 1],
[0, 0, 0, 1, 0, 0, 1, 1, 0]
])
rotmat = np.array([
[ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00],
[ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00],
[ -1.00000000e+00, 0.00000000e+00, 0.00000000e+00]
])
a.reshape(3, 3, -1).dot(rotmat).reshape(-1, 9)
array([[ 0., 1., 0., -1., 1., 1., -1., 1., 0.],
[-1., 0., 1., -1., 1., 1., -1., 0., 0.],
[ 0., 0., 0., 0., 0., 1., 0., 1., 1.]])
df = pd.DataFrame(a.reshape(3, 3, -1).dot(rotmat).reshape(-1, 9))
df
https://stackoverflow.com/questions/39333750
复制相似问题