【发布时间】:2018-07-08 21:59:50
【问题描述】:
我想知道如何动态声明 ListMatrix 的元素(NumericMatrix)。假设我有一个动态维度的 ListMatrix dp1,我想在 Rcpp 中做以下事情:
#include<Rcpp.h>
// [[Rcpp::export]]
Rcpp::ListMatrix ListMatrixType(Rcpp::ListMatrix dp1){
// Question: how to declare the type of elements of the ListMatrix
// dynamically according to the dimension of dp1?
// I want to avoid the verbose method as below:
Rcpp::NumericMatrix dp1_00 = dp1(0,0);
Rcpp::NumericMatrix dp1_01 = dp1(0,1);
Rcpp::NumericMatrix dp1_10 = dp1(1,0);
Rcpp::NumericMatrix dp1_11 = dp1(1,1);
// then doing something on dp1_00, dp1_01, ..., and others
dp1_00(0,0) = 100;
// then update dp1
dp1(0,0) = dp1_00;
dp1(0,1) = dp1_01;
dp1(1,0) = dp1_10;
dp1(1,1) = dp1_11;
return dp1;
}
例如,dp1 = matrix(rep(list(matrix(1,100,2)),2*2),2,2)。预期输出与dp1 相同。
【问题讨论】:
-
请在
dp1中提供一些示例数据以及 R 中的预期输出。 -
例如,
dp1 = matrix(rep(list(matrix(1,100,2)),2*2),2,2)。预期输出与dp1相同。