新手到Rcpp在这里。我有C++,其中我使用向量的向量来表示矩阵向量的向量的列表,说大小Mi x N
,其中i
是从1 to K
和K
的是列表中元素的数目:如何在R中使用Rcpp转换向量的向量作为矩阵
std::map<double, vector<vector<double> > x;
或者,对于列表的元素1,矢量的矢量的尺寸为M1 x N
,对于列表的元素2,矢量的矢量的尺寸为M2 x N
,...,矢量的矢量的尺寸为MK x N
元素K
的列表。
我怎样才能将它转换为在R矩阵的列表。如果我尝试:
return wrap(x)
我得到向量的列表,而不是矩阵的名单列表。
下面是一个简单例子,其中x
在C++是:
1st element of map:
within that, element of the 1st vector: 1 1 1 1
element of the 2nd vector: 1 1 1 1
element of the 3rd vector: 2 2 2 2
element of the 4th vector: 3 3 3 3
2nd element of map:
within that, element of the 1st vector: 4 4 4 4
element of the 2nd vector: 3 3 3 3
element of the 3rd vector: 2 2 2 2
element of the 4th vector: 5 5 5 5
element of the 5th vector: 1 1 1 1
如果我尝试return wrap(x)
我得到了R(PS额外的缩进下面做的r输出更清晰):
$`1`
$`1`[[1]]
[1] 1 1 1 1
$`1`[[2]]
[1] 1 1 1 1
$`1`[[3]]
[1] 2 2 2 2
$`1`[[4]]
[1] 3 3 3 3
$`2`
$`2`[[1]]
[1] 4 4 4 4
$`2`[[2]]
[1] 3 3 3 3
$`2`[[3]]
[1] 2 2 2 2
$`2`[[4]]
[1] 5 5 5 5
$`2`[[5]]
[1] 1 1 1 1
我想在R中返回的是:
$`1`
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 1 1 1 1
[3,] 2 2 2 2
[4,] 3 3 3 3
$`2`
[,1] [,2] [,3] [,4]
[1,] 4 4 4 4
[2,] 3 3 3 3
[3,] 2 2 2 2
[4,] 5 5 5 5
[5,] 1 1 1 1
What co是否是在RCpp中做到这一点的最有效方法?