简体   繁体   English

我如何使用Rcpp将向量的向量转换为R中的矩阵

[英]how can I convert vector of vector as matrix in R using Rcpp

Newbie to Rcpp here. Rcpp的新手在这里。 I have a list of vector of vectors in C++, where I am using the vector of vectors to represent a matrix, say of size Mi x N , where i is from 1 to K and K is the number of elements of the list: 我在C ++中有一个向量列表,其中我使用向量表示一个矩阵,大小为Mi x N ,其中i1 to KK是列表中元素的数量:

std::map<double, vector<vector<double> > x;

Put alternatively, for element 1 of the list, the vector of vector is of dimensions M1 x N , for element 2 of the list, the vector of vector is of dimensions M2 x N , ..., the vector of vector is of dimensions MK x N for element K of the list. 换句话说,对于列表的元素1,向量的向量的尺寸为M1 x N ,对于列表的元素2,向量的向量的尺寸为M2 x N ,...,向量的向量的尺寸为列表元素KMK x N

How can I convert this to a list of matrices in R. If I try: 如何将其转换为R中的矩阵列表。如果尝试:

return wrap(x)

I get a list of list of vectors, NOT list of matrices. 我得到向量列表的列表,而不是矩阵列表。

Here is a sample example, where x in C++ is : 这是一个示例示例,其中C ++中的x为:

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

If I try return wrap(x) I get in R (ps extra indentation added below to make R's output clearer): 如果我尝试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

What I want to be returned in R is: 我想在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 could be the most efficient way to do this in RCpp? 在RCpp中,最有效的方法是什么?

It was your design choice: I am using the vector of vectors to represent a matrix . 这是您的设计选择: 我正在使用向量的向量来表示矩阵 So maybe you have to write yourself a converter. 因此,也许您必须为自己编写一个转换器。 As you are a relatively frequent poster here, you by now surely know that we have documentation on how to write your own wrap() function in the vignette Rcpp-extending . 由于您是这里的相对频繁发布者,因此您现在肯定已经知道我们有如何在小插图Rcpp-extending中编写您自己的wrap()函数的文档

If you want to avoid that, my suggestion would be to actually use a matrix class when you need a matrix . 如果要避免这种情况,我的建议是在需要matrix时实际使用matrix类 Rcpp has one in NumericMatrix. Rcpp在NumericMatrix中有一个。 RcppArmadillo and RcppEigen bring their own with additional focus on matrix algebra. RcppArmadillo和RcppEigen着重介绍矩阵代数。

To me, RcppArmadillo offers a pretty perfect combination of features and ease of use. 对我来说,RcppArmadillo提供了功能和易用性的完美结合。 It also allows you to write code which depends only on Armadillo headers if you want to re-use the C++ code outside of R, but then very easily connect it to R thanks to Rcpp / RcppArmadillo. 如果您想在R之外重用C ++代码,还可以让您编写仅依赖Armadillo标头的代码,但是由于Rcpp / RcppArmadillo,可以非常轻松地将其连接到R。

vector<vector<>> does not carry rectangularity information. vector<vector<>>不携带矩形信息。 I'd recommend you to have your own class MyMatrixClass or whatever, which could very well hold a vector<vector<>> if you like. 我建议您使用自己的类MyMatrixClass或其他任何类,如果MyMatrixClass ,它们可以很好地容纳vector<vector<>>

But you define MyMatrixClass::operator SEXP() so that that it gets converted to the appropriate stucture. 但是您定义MyMatrixClass::operator SEXP()以便将其转换为适当的结构。 You can leverage the NumericMatrix class for example. 例如,您可以利用NumericMatrix类。

wrap does the best it can with the information it has at compile time, and it does not know all vectors will have the same size, so the best it can do is make a list of numeric vectors. wrap会利用其在编译时所拥有的信息来尽其所能,并且它不知道所有向量都具有相同的大小,因此,所能做的最好的事情就是列出一个数字向量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM