c++ - Armadillo 和 C++ - 使用二维数组进行矩阵初始化

标签 c++ arrays matrix armadillo

我需要用 double 组初始化 Armadillo 矩阵。我在原始文档中找到了这个构造函数:

mat(*aux_mem, n_rows, n_cols, copy_aux_mem = true, strict = true) 

我在 SO 上找到了关于它的问题:

armadillo C++: matrix initialization from array

问题是,构造函数似乎只适用于一维数组的初始化,而不适用于二维数组。如果我尝试以这种方式使用它:

double **matrix = new double *[block_size];
for(int i = 0; i < block_size; i++) {
    matrix[i] = new double[block_size];
}

arma::mat arma_matrix( &matrix[0][0], matrix_size, matrix_size, true, true );

cout << "am: " << arma_matrix[1][0] << endl;

我得到错误:

fined_grain:103/init_function: In function ‘void place_user_fn_103(ca::Context&, ca::TokenList<double>&)’:
fined_grain:103/init_function:61:42: error: invalid types ‘double[int]’ for array subscript

那么,用二维数组初始化 Arma 矩阵的理想方法是什么?我更喜欢最快的解决方案,因为我需要使用大矩阵。

最佳答案

我快速浏览了 armadillo library documentation我看到了以下问题。

  1. 您传递给 arma_matrix 的参数在句法上是正确的,但不正确。您需要使用:

    double *matrix = new double [block_size*block_size];
    arma::mat arma_matrix( matrix, block_size, block_size, true, true );
    
  2. 访问元素的语法是:

    cout << "am: " << arma_matrix(1, 0) << endl;
    

    您还可以使用:

    int row = 1;
    int col = 0;
    cout << "am: " << arma_matrix[row*block_size+col] << endl;
    

关于c++ - Armadillo 和 C++ - 使用二维数组进行矩阵初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28838380/

相关文章:

C++ dll 与其他 dll 作为依赖项 (SDL2)

python - 将数组列转换为 PySpark 数据框中的结构数组

math - Julia (Julia):是否可以使用isposdef()确定Cholesky分解是否可以分解矩阵?

python - 将元素设置为零的有效方法,其中掩码在 scipy 稀疏矩阵上为 True

matlab - 将单元格划分为单列矩阵

c++ - opengl斜投影

c++ - C++ 中的 Postfix 不像我期望的那样表现

javascript - 如何使用带有 angular.js 和 Angular Material 的 ngif 从 DOM 中隐藏元素?

c++ - 正确发送 Uniform 数据到 GPU

python - 如何使用随机数打印设置新记录(在数组内)的时间