這是可行的,但有點痛苦。我想爲新的構造函數或幫助函數提供一個體面的(和測試)貢獻將不勝感激。
與此同時,你可以做下面的例子。但要小心row-major和col-major等。另一個選項是RcppArmadillo,它有一個適當的'立方體'類型推廣矩陣爲3-d。
R> library(inline)
R> fx <- cxxfunction(signature(vs="numeric", ds="integer"), plugin="Rcpp", body='
+ Rcpp::NumericVector v(vs); // get the data
+ Rcpp::Dimension d(ds); // get the dim object
+ Rcpp::NumericVector r(d); // create vec. with correct dims
+ std::copy(v.begin(), v.end(), r.begin()); // and copy
+ return Rcpp::List::create(v, d, r);
+ ')
R> fx(1:8, c(2,2,2))
[[1]]
[1] 1 2 3 4 5 6 7 8
[[2]]
[1] 2 2 2
[[3]]
, , 1
[,1] [,2]
[1,] 1 3
[2,] 2 4
, , 2
[,1] [,2]
[1,] 5 7
[2,] 6 8
R>
與* RcppArmadillo立方體*示例:http://markovjumps.blogspot.be/2011/12/r-array-to-rcpparmadillo-cube.html –