我想通過 numpytestarray 中的旋轉矩陣來旋轉 Vector vc,但我得到一個 ValueError。
這是我的代碼(簡化為要領)
import numpy as np
vc = np.array([0,0,1])
numpytestarray = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]])
new_vc = numpytestarray.dot(vc)
print(new_vc)
我該如何解決這個問題?
uj5u.com熱心網友回復:
您的旋轉矩陣和向量應該是相同的大小,例如:
- 大小為 2x2 的旋轉矩陣對應于 2D 向量 [x, y] 的 2D 旋轉
- 大小為 3x3 的旋轉矩陣對應于 3D 向量 [x, y, z] 的 3D 旋轉
您的矢量 vc 是 3D 的[0, 0, 1],但是您嘗試使用大小為 4x4 的旋轉矩陣將其旋轉 4 維。
您需要更改矢量大小:
import numpy as np
vector = np.array([0,0,0,1])
rotation_matrix = np.array([
[-1, 0, 0, 0],
[0, -1, 0, 0],
[0, 0, -1, 0],
[0, 0, 0, -1]])
rotated = rotation_matrix.dot(vector)
print(rotated) # [0, 0, 0, -1]
或旋轉矩陣大小:
import numpy as np
vector = np.array([0,0,1])
rotation_matrix = np.array([
[-1, 0, 0],
[0, -1, 0],
[0, 0, -1]])
rotated = rotation_matrix.dot(vector)
print(rotated) # [0, 0, -1]
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/456434.html
