简体   繁体   English

如何将Rcpp :: List转换为std :: vector <double>

[英]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.

相关问题 如何转换std :: vector <std::vector<double> &gt;到Rcpp :: Dataframe或Rcpp :: NumericMatrix - How to convert std::vector<std::vector<double>> to Rcpp::Dataframe or Rcpp::NumericMatrix 如何转换 'std::vector<double> ' 在初始化时'加倍'</double> - how to convert 'std::vector<double>' to 'double' in initialization 如何转换 std::vector <std::vector<double> > 到火炬::张量? </std::vector<double> - How is it possible to convert a std::vector<std::vector<double>> to a torch::Tensor? 将 mwArray 转换为 std::vector<double></double> - Convert mwArray to std::vector<double> 转换一个std :: vector <boost::optional<double> &gt;到std :: vector <double> - Convert a std::vector<boost::optional<double>> to a std::vector<double> 转换矢量 <std::string> 矢量 <double> - Convert vector<std::string> to vector<double> std::pair 的初始化列表<vector<int> , 双&gt; - Initializer list of std::pair<vector<int>, double> 将Rcpp :: CharacterVector转换为std :: string - convert Rcpp::CharacterVector to std::string 转换 std::vector <complex<double> &gt; 复杂<double> [N] 用于 FFT </double></complex<double> - Convert std::vector<complex<double>> to complex<double>[N] for FFT 如何将void *转换为std :: vector <std::vector<std::string> &gt; *? - How to convert void* to std::vector<std::vector<std::string> >*?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM