如何在 C++ 中定义矩阵(数组)列表
How to define a list of matrix (array) in c++
我正在尝试在 C++ 中创建大对象,使用 Rcpp lib,它是负数和正数的矩阵,我定义了一个双精度类型的辅助二维数组,因为由于对象的尺寸我无法使用 NumericMatrix .
现在我想把这个巨大的数组放到一个std::list中,比如:
std::list<NumericMatrix> listOfMatrix;
但是对于数组:
std::list<double array[nrows][ncols]> listOfMarix;
你想要 Rcpp::List
作为 return。
这是一个非常简单的例子,有两个矩阵;您也可以使用多维数组,但不太常见。另请参阅 RcppArmadillo 了解向量、矩阵、立方体和字段类型。
R> library(Rcpp)
R> cppFunction("Rcpp::List foo() { return Rcpp::List::create(Rcpp::NumericMatrix(2,2), Rcpp::NumericMatrix(3,3)); }")
R> foo()
[[1]]
[,1] [,2]
[1,] 0 0
[2,] 0 0
[[2]]
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
R>
在 R 中,一切都只是一个连续的向量。矩阵恰好具有(大小为 2)的维度属性;您可以将其概括为三个或更多维度——但此类数据结构在 R 中很少见,而且转换器也很少。
这是一个 2x2x2 数组的最小示例:
R> cppFunction('Rcpp::NumericVector bar() { Rcpp::NumericVector x(8); Rcpp::IntegerVector d = Rcpp::IntegerVector::create(2,2,2); x.attr("dim") = d; return(x); }')
R> bar()
, , 1
[,1] [,2]
[1,] 0 0
[2,] 0 0
, , 2
[,1] [,2]
[1,] 0 0
[2,] 0 0
R>
您可以像我上面那样将其中的几个组合在一个列表中。
我正在尝试在 C++ 中创建大对象,使用 Rcpp lib,它是负数和正数的矩阵,我定义了一个双精度类型的辅助二维数组,因为由于对象的尺寸我无法使用 NumericMatrix .
现在我想把这个巨大的数组放到一个std::list中,比如:
std::list<NumericMatrix> listOfMatrix;
但是对于数组:
std::list<double array[nrows][ncols]> listOfMarix;
你想要 Rcpp::List
作为 return。
这是一个非常简单的例子,有两个矩阵;您也可以使用多维数组,但不太常见。另请参阅 RcppArmadillo 了解向量、矩阵、立方体和字段类型。
R> library(Rcpp)
R> cppFunction("Rcpp::List foo() { return Rcpp::List::create(Rcpp::NumericMatrix(2,2), Rcpp::NumericMatrix(3,3)); }")
R> foo()
[[1]]
[,1] [,2]
[1,] 0 0
[2,] 0 0
[[2]]
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
R>
在 R 中,一切都只是一个连续的向量。矩阵恰好具有(大小为 2)的维度属性;您可以将其概括为三个或更多维度——但此类数据结构在 R 中很少见,而且转换器也很少。
这是一个 2x2x2 数组的最小示例:
R> cppFunction('Rcpp::NumericVector bar() { Rcpp::NumericVector x(8); Rcpp::IntegerVector d = Rcpp::IntegerVector::create(2,2,2); x.attr("dim") = d; return(x); }')
R> bar()
, , 1
[,1] [,2]
[1,] 0 0
[2,] 0 0
, , 2
[,1] [,2]
[1,] 0 0
[2,] 0 0
R>
您可以像我上面那样将其中的几个组合在一个列表中。