- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道如何将Rcpp IntegerVector转换为NumericVetortor以进行三次采样,而不用替换数字1到5。
seq_len输出一个IntegerVector,而样本样本仅取一个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
}
最佳答案
您在这里出错了,或者也许我严重误解了这个问题。
首先,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转换为NumericVector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30425334/
我写了下面的Rcpp代码编译了,但是速度没有想象中的快。 // [[Rcpp::export]] NumericVector combine_list_to_vec (const Rcpp::List
我正在使用C数组,因此需要在代码中的某些时候将它们转换为Rcpp::NumericVector。我尝试了以下代码,但引发了错误(无法从double [5]转换为'SEXP'类型)。从C数组转换为Num
我对使用 Rcpp 进行编程还很陌生,所以我正在尝试新事物以了解一切如何运作。我写了一个小程序来比较两个 NumericVectors 与 match() 函数。我还想打印出输入 Vectors 和
如何在 Rcpp 中调整大小 NumericVector ? 当我使用 push_back为此,程序会变慢。但是没有.resize()或 .reserve()职能。 (当我已经有一个具有所需大小的 N
我正在考虑通过用 C++ 重写并通过 Rcpp 集成来加速一些 R 代码。至少可以说,我的 Cpp 生锈了:所以我很感激任何建议。特别是,我正在寻找有关将函数映射到 Rcpp NumericVecto
我编写了以下 Rcpp 代码,它编译但没有给出预期的结果。 // [[Rcpp::export]] RObject test_1 (Rcpp::NumericVector& x) { Nu
我有两个用 Rcpp 编写的函数。 第一个函数f将 NumericVector 作为输入并返回 NumericVector 作为输出。但通常,f意味着被视为标量函数。当我从 R 调用它时,没关系,因为
我正在尝试分配一个大型(大概)2D 数组的 NumericVectors(总内存应该只有 16Mb 左右,除非有开销),但我遇到了堆栈溢出。这是我能想到的最小的可重现示例: require(Rcpp)
假设一个人使用 NumericVector其他用途vector在他们的 Rcpp 代码中。这两种用法之间是否存在显着差异,尤其是在性能方面? 最佳答案 一般来说,是的。 所有的 Rcpp(11) 类型
创建包含 20 多个元素的 NumericVector 会导致错误消息。 这与本文档(在最底部)一致:http://statr.me/rcpp-note/api/Vector_funs.html 目前
我是 Rcpp 的新手(也是 C++ 的新手)。我使用此处的示例代码编写了代码 https://gist.github.com/kevinushey/4561281 来计算矩阵的行/列最大/混合,如下
我正在编写一个 cpp 函数来用下一个非 na 值替换任何 NA 值。关于替换,代码可以正常工作,但是我想为那些没有后来的非 NA 值的值返回 NA 值。 例如: fill_backward(c(1,
我想将一个R函数翻译成Rcpp,一个简单的测试代码如下,但我不知道如何处理默认设置为NULL的参数。 test t=R_NilValue, Rcpp
我正在编写一个 cpp 函数来用下一个非 na 值替换任何 NA 值。关于替换,代码可以正常工作,但是我想为那些没有后来的非 NA 值的值返回 NA 值。 例如: fill_backward(c(1,
我想将一个R函数翻译成Rcpp,一个简单的测试代码如下,但我不知道如何处理默认设置为NULL的参数。 test t=R_NilValue, Rcpp
我有两个 NumericVectors A 和 B。预先对 vector B 进行子集化时,以下乘法工作正常: NumericVector B_sub = B[A - 1]; NumericVect
我正在尝试使用 Rcpp 编写一些代码,并且我正在尝试了解条件语句如何在逻辑 vector 之间工作,因为它们是通过使用 NumericVector 和 C++ 的 native bool 类型进行比
我正在使用 Rcpp 来包装一个用类似 C 的 C++ 编写(不是我)的算法(据我所知,没有 STL,没有提升,什么都没有)。可以看到实现的算法here (我正在包装 kmeans_w_03)。因此,
对于 Rcpp 及其功能,我是一个新手,更不用说 C++ 本身了,所以这对你们中的专家来说可能看起来微不足道。但是,没有愚蠢的问题,所以无论如何: 我想知道是否有一种方法可以使用索引一次处理 C++
以前有人问过类似的问题,在这里: Converting between NumericVector/Matrix and VectorXd/MatrixXd in Rcpp(Eigen) to per
我是一名优秀的程序员,十分优秀!