我如何在rcpp中导入向量?

问题描述 投票:0回答:1

下午好,

我创建了这个rcpp函数,它返回一个3d矩阵 with 尺寸K * K * L

Rcpp代码:

#include <Rcpp.h>
using namespace Rcpp;   

// [[Rcpp::export]]

NumericVector Test( int K , int L ){

  // calling some R functions 

  Function f1("rnorm"); 
  Function f2("runif"); 
  Function f3("ceiling"); 
  Function f4("unique"); 
  Function f5("array"); 
  Function f6("unique.array"); 
  Function f7("print");

  // Creating a vector of normal law with length= K * K * L

  NumericVector rn = f5(f1(K*K*L, Named("sd")=1, _["mean"]=0), Named("dim")=NumericVector::create(K,K,L));

  // Reshaping the 1d vector  , the 3d matrix will be visible in R 
  rn.attr("dim") = Dimension(K, K , L ); 
      return rn ;
    }

***Example of the obtained results :***  The function is called in Rstudio

    > Test(3,2)
    , , 1

            [,1]       [,2]        [,3]
[1,] -0.57144807 -0.1650806  0.07607133
[2,]  0.61278268 -0.5049861  0.37705107
[3,]  0.09037529  1.6185765 -0.68675536

, , 2

           [,1]       [,2]       [,3]
[1,] -0.5789173 -0.6630934  0.9961613
[2,] -0.4425025 -0.9083358 -1.4860523
[3,]  0.2150056 -0.3229108 -1.6536952

我的问题:] >>

  • 假设(K,L)=(3,2)像例子一样,我怎么能以rcpp格式提取

    rn的第二个表中的第二行/列(由[,,2]索引)?
  • 是否有可能使用vector.import()

  • 方法在numericVector变量中将提取的长度为K的向量存储

    对不起,但是我没有找到可以帮助我的例子!

非常感谢!

[下午好,我创建了这个rcpp函数,该函数返回尺寸为K * K * L的3d矩阵。Rcpp代码:#include 使用命名空间Rcpp; // [[[Rcpp :: export]] NumericVector ...

r rcpp
1个回答
0
投票

可能的解决方案可能是:

© www.soinside.com 2019 - 2024. All rights reserved.