矩阵列的特征减法向量

时间:2017-03-15 13:22:50

标签: c++ matrix eigen

矩阵linesP0是3xN。我想从矢量planeP0中减去3x1。有没有更聪明,更快捷的方法呢?

现在我正在使用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(),但没有工作。

2 个答案:

答案 0 :(得分:4)

你可以使用.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)

答案 1 :(得分:-1)

您想对多个数据(SIMD)使用单个指令。 您可以尝试使用矢量计算,例如英特尔的AVX: https://software.intel.com/sites/landingpage/IntrinsicsGuide/

相关问题