Rcpp 如何将 IntegerVector 转换为 NumericVector
Rcpp How to convert IntegerVector to NumericVector
我想知道如何将 Rcpp IntegerVector 转换为 NumericVetortor 以在不替换数字 1 到 5 的情况下进行三次采样。
seq_len 输出一个 IntegerVector 和 sample sample 只需要一个 NumericVector
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadilloExtensions/sample.h>
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector follow_path(NumericMatrix X, NumericVector y) {
IntegerVector i = seq_len(5)*1.0;
NumericVector n = i; //how to convert i?
return sample(cols_int,3); //sample only takes n input
}
我从 http://adv-r.had.co.nz/Rcpp.html#rcpp-classes 那里学到了如何使用
NumericVector cols_num = as<NumericVector>(someIntegerVector)
.
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadilloExtensions/sample.h>
#include <Rcpp.h>
using namespace Rcpp;
using namespace RcppArmadillo;
// [[Rcpp::export]]
NumericVector follow_path(NumericMatrix X, IntegerVector y) {
IntegerVector cols_int = seq_len(X.ncol());
NumericVector cols_num = as<NumericVector>(cols_int);
return sample(cols_num,3,false);
}
你这里有一些问题,或者我可能严重误解了这个问题。
首先,sample()
接受整数向量,实际上它是模板化的。
其次,你根本没有使用你的论据。
这是修复后的版本:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadilloExtensions/sample.h>
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
IntegerVector sampleDemo(IntegerVector iv) { // removed unused arguments
IntegerVector is = RcppArmadillo::sample<IntegerVector>(iv, 3, false);
return is;
}
/*** R
set.seed(42)
sampleDemo(c(42L, 7L, 23L, 1007L))
*/
这是它的输出:
R> sourceCpp("/tmp/soren.cpp")
R> set.seed(42)
R> sampleDemo(c(42L, 7L, 23L, 1007L))
[1] 1007 23 42
R>
编辑: 当我写这篇文章时,你自己回答了...
我想知道如何将 Rcpp IntegerVector 转换为 NumericVetortor 以在不替换数字 1 到 5 的情况下进行三次采样。 seq_len 输出一个 IntegerVector 和 sample sample 只需要一个 NumericVector
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadilloExtensions/sample.h>
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector follow_path(NumericMatrix X, NumericVector y) {
IntegerVector i = seq_len(5)*1.0;
NumericVector n = i; //how to convert i?
return sample(cols_int,3); //sample only takes n input
}
我从 http://adv-r.had.co.nz/Rcpp.html#rcpp-classes 那里学到了如何使用
NumericVector cols_num = as<NumericVector>(someIntegerVector)
.
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadilloExtensions/sample.h>
#include <Rcpp.h>
using namespace Rcpp;
using namespace RcppArmadillo;
// [[Rcpp::export]]
NumericVector follow_path(NumericMatrix X, IntegerVector y) {
IntegerVector cols_int = seq_len(X.ncol());
NumericVector cols_num = as<NumericVector>(cols_int);
return sample(cols_num,3,false);
}
你这里有一些问题,或者我可能严重误解了这个问题。
首先,sample()
接受整数向量,实际上它是模板化的。
其次,你根本没有使用你的论据。
这是修复后的版本:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadilloExtensions/sample.h>
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
IntegerVector sampleDemo(IntegerVector iv) { // removed unused arguments
IntegerVector is = RcppArmadillo::sample<IntegerVector>(iv, 3, false);
return is;
}
/*** R
set.seed(42)
sampleDemo(c(42L, 7L, 23L, 1007L))
*/
这是它的输出:
R> sourceCpp("/tmp/soren.cpp")
R> set.seed(42)
R> sampleDemo(c(42L, 7L, 23L, 1007L))
[1] 1007 23 42
R>
编辑: 当我写这篇文章时,你自己回答了...