c++ - Eigen 矩阵是否支持 vector 索引?

标签 c++ c++11 eigen

例如,如果我有一个 4 x 4 矩阵。有没有办法创建另一个矩阵(或原始矩阵的 View ,甚至更好),它只是原始矩阵的第 1 行和第 3 行。

我只看到如何提取一行或一个 block ,但没有看到我上面提到的内容。这是我的代码:

#include <iostream>
#include <Eigen/Dense>

using namespace Eigen;

int main()
{
  Matrix4f m = Matrix4f::Random();
  std::cout << "Matrix : " << std::endl;
  std::cout << m << std::endl;

  std::cout << "row" << std::endl;
  std::cout << m.row(1) << std::endl;

  std::cout << "block : " << std::endl;
  std::cout << m.block(1,0,2,4) << std::endl;
  return 0;
}

一个潜在的解决方案是用一个 1 和 0 的矩阵预乘我的矩阵,

 z = ([[ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  1.]])

z * m 会给我我想要的,但有没有更好的解决方案。

编辑:

我想做的事情的可能应用:

假设我有矩阵 A(m x n)B(n x k)。我想从 A 中采样并乘以 B,假设我取 A 行的 1/5 A'(m/5 X n) * B(n x k) 是我所追求的。我不需要 A' 本身,它就是我所追求的产品。

最佳答案

Eigen 的置换矩阵可能就是您正在寻找的:

Randomly permute rows/columns of a matrix with eigen

using Eigen;
Matrix4f m = Matrix4f::Random();

PermutationMatrix<Dynamic,Dynamic> P(4);
perm.indices()[0] = 1;
perm.indices()[1] = 3;

MatrixXf B = (m * P).leftCols(2);

关于c++ - Eigen 矩阵是否支持 vector 索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25331875/

相关文章:

c++ - 什么时候对流类型使用 std::swap?

c++ - std::bind 类内部的静态成员函数

c++ - C++快速输出图片的方法

c++ - 无法使用 net stop 命令停止 Windows 服务

c++ - 打包一个 HTML5 应用并将其部署在桌面上

c++ - C++11 std::sort 在不同的 STL 实现中使用了哪些算法?

c++ - Eigen 3.3.0 与 3.2.10 的性能回归?

c++ - 使用 Eigen 库的 mingw 出现未知错误

c++ - fatal error : eigen3/Eigen/Core: No such file or directory

c++ - printf 和 scanf 中 %lli 和 %lld 之间的区别?