从矩阵列中减去特征向量

Eigen subtracting vector from matrix columns

矩阵 linesP0 是 3xN。我想从 3x1 的向量 planeP0 中减去它。有没有更聪明、更快捷的方法来做到这一点?

现在我正在使用 for 循环。示例代码如下:

MatrixXf temp(linesP0.rows(), linesP0.cols());
for (int i = 0; i < linesP0.cols(); i++)
{
    temp.col(i) = planeP0 - linesP0.block<3, 1>(0, i);
}

我尝试使用 colwise() 但没有成功。

你可以使用 .colwise() 来做到这一点,你只需要有点创意。

Vector3d v      = Vector3d(1.0, 2.0, 3.0);
Matrix3d m      = Matrix3d::Random();
Matrix3d result = (-m).colwise() + v;
std::cout << result << std::endl;

示例结果:

v = [1 2 3]' (3x1)
m = [1 1 1; 2 2 2]' (3x2)
result = [0 1 2; -1 0 1]' (3x2)