Matrix 如何在不使用映射函数的情况下改变特征矩阵中的外矩阵

Matrix 如何在不使用映射函数的情况下改变特征矩阵中的外矩阵,matrix,eigen,Matrix,Eigen,我想在编译时更改特征矩阵的外部矩阵,而不使用map函数 我尝试使用OuterStriedAtCompileTime变量更改此设置,但它不起作用。有什么方法可以做到这一点吗 还有一件事,每次打印mat.Outerstride()都会给出输入矩阵的行数。如何打印特征矩阵的Outerstride 提前谢谢 我用映射函数定义了一个特征矩阵,比如 MatrixXf mat; float arr[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; mat =

我想在编译时更改特征矩阵的外部矩阵,而不使用map函数

我尝试使用OuterStriedAtCompileTime变量更改此设置,但它不起作用。有什么方法可以做到这一点吗

还有一件事,每次打印mat.Outerstride()都会给出输入矩阵的行数。如何打印特征矩阵的Outerstride

提前谢谢


我用映射函数定义了一个特征矩阵,比如

MatrixXf mat;
float arr[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
mat =  Map<Matrix<float,Dynamic,Dynamic,Eigen::RowMajor>, 0, OuterStride<Dynamic> > (arr,4,4,OuterStride<Dynamic>(5));

它不工作。

外部步幅是一个与数据存储相关的参数。更常用的名称是前导维度。你可以在这里找到一些解释

基本上,对于现有的矩阵,它是不能更改的。在不更改矩阵元素的情况下更改矩阵的唯一方法是使用不同的外部步长设置将矩阵复制到新的内存空间。当您将一个矩阵作为子矩阵复制到另一个矩阵时,通常会发生这种情况

对于列主矩阵,最小可能的外部跨距等于已打印的行数

使用Eigen时,您不必担心它,因为Eigen通常会为您处理
Eigen::Map


你的代码实际上不起作用。由于
arr
中存储的现有矩阵(4x4)为第4步,且
5 x 4列=20>16,因此将跨距设置为5已超出范围

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

int main(void) {
  using namespace Eigen;
  MatrixXf mat;
  float arr[16] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };

  mat = Map<Matrix<float, Dynamic, Dynamic, Eigen::RowMajor>, 0,
      OuterStride<Dynamic> >(arr, 4, 4, OuterStride<Dynamic>(5));
  std::cout << "mat with stride 5:\n" << mat << std::endl;

  mat = Map<Matrix<float, Dynamic, Dynamic, Eigen::RowMajor>, 0,
      OuterStride<Dynamic> >(arr, 4, 4, OuterStride<Dynamic>(4));
  std::cout << "mat with stride 4:\n" << mat << std::endl;

  return 0;
}
如果将数组扩展到20个元素

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

int main(void) {
  using namespace Eigen;
  MatrixXf mat;
  float arr[20] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };

  Map<Matrix<float, Dynamic, Dynamic, Eigen::RowMajor>, 0, OuterStride<Dynamic> > map1(arr, 4, 4, OuterStride<Dynamic>(5));
  mat = map1;
  std::cout << "map1 outer stride: " << map1.outerStride() << std::endl << map1 << std::endl;
  std::cout << "mat outer stride: " << mat.outerStride() << std::endl << mat << std::endl;

  Map<Matrix<float, Dynamic, Dynamic, Eigen::RowMajor>, 0, OuterStride<Dynamic> > map2(arr, 4, 4, OuterStride<Dynamic>(4));
  mat = map2;
  std::cout << "map2 outer stride: " << map2.outerStride() << std::endl << map2 << std::endl;
  std::cout << "mat outer stride: " << mat.outerStride() << std::endl << mat << std::endl;

  return 0;
}
map1
复制到
mat
时,您还可以看到外部步幅的变化。 希望这能让你更好地了解“走出去”是什么

事实上,在原始代码中,您以错误的方式使用了
Map
——您不应该将
Map()
复制到
Matrix
。 这就是为什么打印
mat
的步幅时,它始终为4。
您需要做的是消除不必要的数据复制并打印
map1
/
map2

外部步长是与数据存储相关的参数。更常用的名称是前导维度。你可以在这里找到一些解释

基本上,对于现有的矩阵,它是不能更改的。在不更改矩阵元素的情况下更改矩阵的唯一方法是使用不同的外部步长设置将矩阵复制到新的内存空间。当您将一个矩阵作为子矩阵复制到另一个矩阵时,通常会发生这种情况

对于列主矩阵,最小可能的外部跨距等于已打印的行数

使用Eigen时,您不必担心它,因为Eigen通常会为您处理
Eigen::Map


你的代码实际上不起作用。由于
arr
中存储的现有矩阵(4x4)为第4步,且
5 x 4列=20>16,因此将跨距设置为5已超出范围

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

int main(void) {
  using namespace Eigen;
  MatrixXf mat;
  float arr[16] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };

  mat = Map<Matrix<float, Dynamic, Dynamic, Eigen::RowMajor>, 0,
      OuterStride<Dynamic> >(arr, 4, 4, OuterStride<Dynamic>(5));
  std::cout << "mat with stride 5:\n" << mat << std::endl;

  mat = Map<Matrix<float, Dynamic, Dynamic, Eigen::RowMajor>, 0,
      OuterStride<Dynamic> >(arr, 4, 4, OuterStride<Dynamic>(4));
  std::cout << "mat with stride 4:\n" << mat << std::endl;

  return 0;
}
如果将数组扩展到20个元素

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

int main(void) {
  using namespace Eigen;
  MatrixXf mat;
  float arr[20] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };

  Map<Matrix<float, Dynamic, Dynamic, Eigen::RowMajor>, 0, OuterStride<Dynamic> > map1(arr, 4, 4, OuterStride<Dynamic>(5));
  mat = map1;
  std::cout << "map1 outer stride: " << map1.outerStride() << std::endl << map1 << std::endl;
  std::cout << "mat outer stride: " << mat.outerStride() << std::endl << mat << std::endl;

  Map<Matrix<float, Dynamic, Dynamic, Eigen::RowMajor>, 0, OuterStride<Dynamic> > map2(arr, 4, 4, OuterStride<Dynamic>(4));
  mat = map2;
  std::cout << "map2 outer stride: " << map2.outerStride() << std::endl << map2 << std::endl;
  std::cout << "mat outer stride: " << mat.outerStride() << std::endl << mat << std::endl;

  return 0;
}
map1
复制到
mat
时,您还可以看到外部步幅的变化。 希望这能让你更好地了解“走出去”是什么

事实上,在原始代码中,您以错误的方式使用了
Map
——您不应该将
Map()
复制到
Matrix
。 这就是为什么打印
mat
的步幅时,它始终为4。
您需要做的是消除不必要的数据拷贝,并打印
map1
/
map2

的跨距,为什么要更改它?你能展示一些你如何改变它的代码吗?我想使用不同步幅的矩阵。你能更具体一点吗?我无法想象有任何一种常见的矩阵运算需要改变它。为什么不使用地图呢?这正是它的目的。他们不必复制数据…你为什么要更改它?你能展示一些你如何改变它的代码吗?我想使用不同步幅的矩阵。你能更具体一点吗?我无法想象有任何一种常见的矩阵运算需要改变它。为什么不使用地图呢?这正是它的目的。他们不必复制数据……如何打印矩阵的外层网格?@Madhu,正如你的问题
mat.outerstride()
printing mat.outerStride()每次都给出输入矩阵的行数,与存储顺序无关。@Madhu如果显示重现问题的代码,就会更容易。看到我更新的代码,你就会知道原因。我想要mat1的外层网格,用于创建与mat1具有相同外层网格的目标矩阵。是否有任何函数可以使用mat1打印。如何打印矩阵的外层网格?@Madhu,如你的问题
mat.outerstride()
printing mat.outerStride()每次都给出输入矩阵的行数,与存储顺序无关。@Madhu如果显示重现问题的代码,就会更容易。看到我更新的代码,你就会知道原因。我想要mat1的outerstride,用于创建与mat1具有相同outerstride的目标矩阵。是否有任何函数可以使用mat1打印。
map1 outer stride: 5
 1  2  3  4
 6  7  8  9
11 12 13 14
16 17 18 19
mat outer stride: 4
 1  2  3  4
 6  7  8  9
11 12 13 14
16 17 18 19
map2 outer stride: 4
 1  2  3  4
 5  6  7  8
 9 10 11 12
13 14 15 16
mat outer stride: 4
 1  2  3  4
 5  6  7  8
 9 10 11 12
13 14 15 16