I also received this error when attempting to use an arma::cube as an Rcpp function parameter. † After reading a couple of related examples online, it looks like the typical workaround is to read in your R array as a NumericVector, and since it retains its dims attribute, use these to set your arma::cube dimensions. Despite the fact that there is an extra step or two required to account for the †, the Armadillo version I put together seems to be quite a bit faster than my R solution:#include <RcppArmadillo.h>// [[Rcpp::depends(RcppArmadillo)]]// [[Rcpp::export]]arma::mat cube_means(Rcpp::NumericVector vx) { Rcpp::IntegerVector x_dims = vx.attr("dim"); arma::cube x(vx.begin(), x_dims[0], x_dims[1], x_dims[2], false); arma::mat result(x.n_cols, x.n_slices); for (unsigned int i = 0; i < x.n_slices; i++) { result.col(i) = arma::conv_to<arma::colvec>::from(arma::mean(x.slice(i))); } return result;}/*** Rrcube_means <- function(x) t(apply(x, 2, colMeans))xl <- array(1:10e4, c(100, 100 ,10))all.equal(rcube_means(xl), cube_means(xl))#[1] TRUER> microbenchmark::microbenchmark( "R Cube Means" = rcube_means(xl), "Arma Cube Means" = cube_means(xl), times = 200L)Unit: microseconds expr min lq mean median uq max neval R Cube Means 6856.691 8204.334 9843.7455 8886.408 9859.385 97857.999 200 Arma Cube Means 325.499 380.540 643.7565 416.863 459.800 3068.367 200*/我利用了arma::mat的arma::mean函数重载将默认计算列均值的事实(arma::mean(x.slice(i), 1)将为您提供该切片的行均值).where I am taking advantage of the fact that the arma::mean function overload for arma::mats will calculate column means by default (arma::mean(x.slice(i), 1) would give you the row means of that slice). †再三考虑,我不确定这是否与Rcpp::wrap有关-但问题似乎与缺少Exporter<>专业化有关arma::cube-Rcpp的Exporter.h的第31行: † On second thought, I'm not really sure if this has to do with Rcpp::wrap or not - but the issue seems to be related to a missing Exporter<> specialization for arma::cube - line 31 of Rcpp's Exporter.h:template <typename T>class Exporter{public: Exporter( SEXP x ) : t(x){} inline T get(){ return t ; }private: T t ;} ;无论如何,NumericVector/我现在使用的设置尺寸方法似乎是一种功能解决方案.Regardless, NumericVector / setting dimensions approach I used seems to be functional solution for now.根据您在问题中描述的输出维度,我假设您希望所得矩阵的每一列都是对应数组切片的列均值的向量(列1 =切片1的列均值,依此类推... ),即Based on the output dimensions you described in your question, I assumed you wanted each column of the resulting matrix to be a vector of column means of the corresponding array slice (column 1 = column means of slice 1, etc...), i.e.R> x <- array(1:27, c(3, 3, 3))R> rcube_means(x) [,1] [,2] [,3][1,] 2 11 20[2,] 5 14 23[3,] 8 17 26R> cube_means(x) [,1] [,2] [,3][1,] 2 11 20[2,] 5 14 23[3,] 8 17 26但是,如果需要的话,对您来说微不足道.but it would be trivial for you to alter this if needed. 这篇关于列均值3d矩阵(立方体)Rcpp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-06 14:30