新手到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中做到這一點的最有效方法?