[英]How to convert Rcpp::List to std::vector<double>
I'm a newcomer in the field of R langauge, but I need to compute singular value decomposition via irlba in my C++ code. 我是R langauge领域的新手,但我需要在我的C ++代码中通过irlba计算奇异值分解。 For this purpose I use RInside lib.
为此,我使用了RInside lib。
RInside R(argc, argv);
std::string cmd = "S<-diag(3)";
R.parseEval(cmd);
R.parseEval("B<-svd(S,nu=dim(S)[1],nv=dim(S)[2])");
Rcpp::List V ((SEXP)R.parseEval("B$v"));
Now I need to convert my results from Rcpp::List with singular vectors to std::vector 现在我需要将我的结果从带有奇异向量的Rcpp :: List转换为std :: vector
Question: What is the best way to convert results of performing svd to the std::vector? 问题:将执行svd的结果转换为std :: vector的最佳方法是什么?
How can I transform my input matrix written as std::vector to the format appropriate for using it as an input parameter for svd function in irlba? 如何将我作为std :: vector编写的输入矩阵转换为适合将其用作irlba中svd函数的输入参数的格式?
To go from C++ types to R objects, you can use wrap
. 要从C ++类型转换为R对象,可以使用
wrap
。 The way I usually construct NumericMatrix
s from a std::vector<double>
is like so: 我通常从
std::vector<double>
构造NumericMatrix
的方式如下:
// with x as a std::vector<double>
using namespace Rcpp;
NumericVector m = wrap(x); // wrap x into an R object
m.attr("dim") = Dimension(<num_rows>, <num_cols>); // set the dimensions
with <num_rows>
and <num_cols>
chosen based on your desired dimensions. 根据您所需的尺寸选择
<num_rows>
和<num_cols>
。 m
should then be usable as an R matrix. 那么
m
应该可以用作R矩阵。
In general, you can use as<T>
to go from R types to C++ types, and wrap
to go from C++ types to R types. 通常,您可以使用
as<T>
从R类型转换为C ++类型,并将wrap
从C ++类型转换为R类型。 See section 3 of the Rcpp-Introduction vignette for some more information. 有关更多信息,请参阅Rcpp-Introduction插图的第3部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.