从矩阵列中减去特征向量

Ser*_*ABP 3 c++ matrix eigen

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

现在,我正在使用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);
}
Run Code Online (Sandbox Code Playgroud)

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

Jac*_*lam 5

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

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

样本结果:

v = [1 2 3]' (3x1)
m = [1 1 1; 2 2 2]' (3x2)
result = [0 1 2; -1 0 1]' (3x2)
Run Code Online (Sandbox Code Playgroud)

  • @gipouf 我能想到的原因: - Eigen 将可靠地矢量化,更重要的是,将以您想要的方式精确地矢量化。有些向量化模式无法被编译器识别,因为它必须知道你的数据在运行时是什么样子——类似“map”的操作让每个人内心深处都感到温暖和模糊,这样可以更容易地避免偏离——一个错误 - 你想做“const”的所有事情 (2认同)