Matlab VectorA/VectorB 到 Python

Matlab VectorA/VectorB to Python

我目前正在将我的 matlab 代码翻译成 python,我在 Matlab 中有一行

X = VectorA/VectorB;

其中 x 是单个标量值,VectorA 和 VectorB 都是大小为 1x1750 的数组。 但我无法将其转换为 python 代码,我知道它是一个线性方程系统,应该通过 python 中的 numpy.linalg.solve(a, b) 工作,但它希望 b是一个方阵并且不起作用。我是否没有使用正确的函数 and/or 弄乱了语法,不知道。

你们能帮帮我吗?

我想您可能正在寻找 np.linalg.lstsq (https://numpy.org/doc/stable/reference/generated/numpy.linalg.lstsq.html#numpy.linalg.lstsq)。

a = np.ndarray((2,1), buffer=np.array([1, 2, 3]), dtype=int)
b = np.ndarray((2,1), buffer=np.array([2, 4, 6]), dtype=int)

# Solve for X = b/a, or a * X = b
X, resid, rank, s = np.linalg.lstsq(a, b, rcond=None)  # X = array([[2.]])