2016-08-17 2 views
1

行列内のインデックス付けのための整数のシーケンスを作成したいと思います。 Rペンダントは、次のようになります。Rcppでの整数のシーケンス

no matching function for call to 'seq_along(const int&, const int&)' 

方法:私はエラーメッセージを取得しかし

#include <Rcpp.h> 
#include <algorithm> 
#include <vector> 
#include <numeric> 
using namespace Rcpp; 
using namespace std; 


// [[Rcpp::export]] 
NumericVector test(NumericVector x) { 
    IntegerVector indexRow = Rcpp::seq_along(max(0, 1), min(1, 12)); 
} 

indexRow <- max(0,1):min(2,12) 
matrix1[indexRow, ] 

これは私が整数のシーケンスを作成するためにRcppにしようとしたものですRcppに整数のシーケンスを作成できますか?ここ

+0

使用R

#include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] IntegerVector test(IntegerVector a, IntegerVector b) { IntegerVector vec = seq(max(a), min(b)); return vec; } 

、[この](http://stackoverflow.com/questions/30063951/using-colon-to-access-elements-in-an-array -in-c-in-rcpp)は役に立つかもしれません –

答えて

4

が可能Rcpp実装で第1及び整数系列の最後の数:

library(Rcpp) 
cppFunction(plugins='cpp11','NumericVector myseq(int &first, int &last) { 
NumericVector y(abs(last - first) + 1); 
if (first < last) 
    std::iota(y.begin(), y.end(), first); 
else { 
    std::iota(y.begin(), y.end(), last); 
    std::reverse(y.begin(), y.end()); 
} 
return y; 
}') 
#> myseq(max(0,1), min(13,17)) 
#[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 

このコードは2つの引数を取る関数myseqを生成します。これは、2つの整数引数seq(first, last)で呼び出されたRのseq関数に似ています。

C++ 11関数std::iotaのドキュメントはhereです。

+2

RcppにはFWIWと同様にseq機能があります。 – nrussell

+0

ああ!知っておいてよかった。ありがとう@nrussell。だから、言語は徐々にRcpp内で「マージ」されています...私が知っているC++標準にはseq関数はありません。 – RHertel

+3

はいRcppの[sugar functions](https://github.com/RcppCore/Rcpp/tree/master/inst/include/Rcpp/sugar/functions)は、Rの対応するものと多かれ少なかれ一致していると思います。それを置くための良い方法。 – nrussell

1

seq_alongは、ベクトルを取ります。使用するベクターはseqで、minmaxの両方がベクターになります。 seqIntegerVectorを返します。ここに例があります。あなたはまた、

> test(c(0,1), c(2,12)) 
[1] 1 2 
関連する問題