映射数组回现有特征矩阵 [英] Mapping array back to an existing Eigen matrix

查看:154
本文介绍了映射数组回现有特征矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲的双阵列映射到现有MatrixXd结构。到目前为止,我已经成功地映射特征矩阵,以一个简单的数组,但我找不到这样做回来的路上。

 无效美孚(Matr​​ixXd矩阵,INT N){ 双arrayd =新的双[N * N];
 //输入矩阵映射到一个数组
 地图< MatrixXd>(arrayd,N,N)=矩阵;  //做一些与阵列
             .......
//地图数组回现有的矩阵}


解决方案

我不知道你想要什么,但我会尽量解释。

您正在混合双人和浮在你的code(一MatrixXf是一个矩阵,其中每个条目是一个浮动)。我假设的时刻,这是您要处处双无意使用AMD;请参阅下面的,如果这真的是你的意图。

指令地图< MatrixXd>(arrayd,N,N)=矩阵复制矩阵的条目进入 arrayd 。它相当于环

 的for(int i = 0; I< N ++ I)
   对于(INT J = 0; J< N ++ j)条
      arrayd [I + J *η] =矩阵(I,J);

arrayd 的条目复制到矩阵,你可以使用逆分配:矩阵=地图< MatrixXd方式>(arrayd,N,N)

然而,一般以下技术是更加有用:

 无效美孚(Matr​​ixXd矩阵,INT N){
   双* arrayd = matrix.data();
   //做一些与阵列
}

现在arrayd点矩阵中的条目,并可以处理它作为任何C ++数组。该数据矩阵之间共享 arrayd ,所以你不必复制任何东西在末端。顺便说一句,你并不需要通过 N 给函数富(),因为它是存储在矩阵;使用matrix.rows()和matrix.cols()查询到它的价值。

如果你想一个MatrixXf复制到双打的数组,那么你需要明确地包括演员。这在本征的语法是:地图< MatrixXd>(arrayd,N,N)= matrix.cast<双>()

I want to map an array of double to an existing MatrixXd structure. So far I've managed to map the Eigen matrix to a simple array, but I can't find the way to do it back.

void foo(MatrixXd matrix, int n){

 double arrayd = new double[n*n];
 // map the input matrix to an array
 Map<MatrixXd>(arrayd, n, n) = matrix;  

  //do something with the array 
             .......
// map array back to the existing matrix

}

解决方案

I'm not sure what you want, but I'll try to explain.

You're mixing double and float in your code (a MatrixXf is a matrix where every entry is a float). I'll assume for the moment that this was unintentional amd that you want to use double everywhere; see below for if this was really your intention.

The instruction Map<MatrixXd>(arrayd, n, n) = matrix copies the entries of matrix into arrayd. It is equivalent to the loop

for (int i = 0; i < n; ++i)
   for (int j = 0; j < n; ++j)
      arrayd[i + j*n] = matrix(i, j);

To copy the entries of arrayd into matrix, you would use the inverse assignment: matrix = Map<MatrixXd>(arrayd, n, n).

However, usually the following technique is more useful:

void foo(MatrixXd matrix, int n) {
   double* arrayd = matrix.data();
   // do something with the array 
}

Now arrayd points to the entries in the matrix and you can process it as any C++ array. The data is shared between matrix and arrayd, so you do not have to copy anything back at the end. Incidentally, you do not need to pass n to the function foo(), because it is stored in the matrix; use matrix.rows() and matrix.cols() to query its value.

If you do want to copy a MatrixXf to an array of doubles, then you need to include the cast explicitly. The syntax in Eigen for this is: Map<MatrixXd>(arrayd, n, n) = matrix.cast<double>() .

这篇关于映射数组回现有特征矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆