6

連続していないスライスを含むRcppのサブマトリックスを選択しようとしています。同等のRコードは

> xx = matrix(0,nrow=10,ncol=8)
> xx[,c(1,3,4)]
      [,1] [,2] [,3]
 [1,]    0    0    0
 [2,]    0    0    0
 [3,]    0    0    0
 [4,]    0    0    0
 [5,]    0    0    0
 [6,]    0    0    0
 [7,]    0    0    0
 [8,]    0    0    0
 [9,]    0    0    0
[10,]    0    0    0

Rcppでは、やろうとしています

Rcpp::NumericMatrix xx(10,8);
Rcpp::NumericMatrix aa = xx(Rcpp::Range(0,9), Rcpp::NumericVector::create(1,3,4));

ただし、これにより

error: no match for call to '(Rcpp::NumericMatrix) (Rcpp::Range, Rcpp::Vector<14>)'
/opt/local/lib/R/library/Rcpp/include/Rcpp/vector/Matrix.h:126: note: candidates are: typename Rcpp::Vector<RTYPE>::Proxy Rcpp::Matrix<RTYPE>::operator()(const size_t&, const size_t&) [with int RTYPE = 14]
/opt/local/lib/R/library/Rcpp/include/Rcpp/vector/Matrix.h:132: note:                 typename Rcpp::Vector<RTYPE>::Proxy Rcpp::Matrix<RTYPE>::operator()(const size_t&, const size_t&) const [with int RTYPE = 14]
/opt/local/lib/R/library/Rcpp/include/Rcpp/vector/Matrix.h:142: note:                 Rcpp::MatrixRow<RTYPE> Rcpp::Matrix<RTYPE>::operator()(int, Rcpp::internal::NamedPlaceHolder) [with int RTYPE = 14]
/opt/local/lib/R/library/Rcpp/include/Rcpp/vector/Matrix.h:146: note:                 Rcpp::MatrixColumn<RTYPE> Rcpp::Matrix<RTYPE>::operator()(Rcpp::internal::NamedPlaceHolder, int) [with int RTYPE = 14]
/opt/local/lib/R/library/Rcpp/include/Rcpp/vector/Matrix.h:150: note:                 Rcpp::SubMatrix<RTYPE> Rcpp::Matrix<RTYPE>::operator()(const Rcpp::Range&, const Rcpp::Range&) [with int RTYPE = 14]
/opt/local/lib/R/library/Rcpp/include/Rcpp/vector/Matrix.h:154: note:                 Rcpp::SubMatrix<RTYPE> Rcpp::Matrix<RTYPE>::operator()(Rcpp::internal::NamedPlaceHolder, const Rcpp::Range&) [with int RTYPE = 14]
/opt/local/lib/R/library/Rcpp/include/Rcpp/vector/Matrix.h:158: note:                 Rcpp::SubMatrix<RTYPE> Rcpp::Matrix<RTYPE>::operator()(const Rcpp::Range&, Rcpp::internal::NamedPlaceHolder) [with int RTYPE = 14]

これはRcppで可能ですか?

4

3 に答える 3

6

NumericMatrix のヘルプ ファイル: http://dirk.eddelbuettel.com/code/rcpp/html/classMatrix.html

Range と NumericVector を受け取るメソッドはなく、2 つの Range または "_" と Range のみを使用するメソッドがないことを示しています...したがって、連続していないスライスを選択する直接的な方法はありません...

于 2011-12-08T18:05:00.273 に答える
6

これが私がやった方法です:

library(inline)

testSlice = cxxfunction(signature(r_mat='int', r_cols='int'), body = 
'
NumericMatrix mat (r_mat);
NumericVector cols (r_cols);

NumericMatrix matslice (mat.nrow(), cols.size());

for (int i=0; i<cols.size(); i++) {
  matslice(_,i) = mat(_, cols(i)-1);
}

return(matslice);
'
, plugin='Rcpp')
> xx = matrix(sample(10, 10*10, replace=T), nrow=10)
> xx
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    6    7    9    8    2    6    1    8   10     8
 [2,]    5    6    9    5    1    5    2    7   10     3
 [3,]    9    5    1   10    8   10   10    1    2     7
 [4,]    2    7    1    4    5    1    8    2   10     9
 [5,]    6    6    9    8    3    1    2   10    2     1
 [6,]    4    6    7    9    7    5    4    7   10    10
 [7,]   10   10    8    8   10    7   10    4    6     3
 [8,]    6    3   10    6    3    2    5    4    1    10
 [9,]    8    7    2    7    9    7    7    8    3    10
[10,]    5    4    5    4    4    8   10    1    5     4

> testSlice(xx, c(2,5,6))
      [,1] [,2] [,3]
 [1,]    7    2    6
 [2,]    6    1    5
 [3,]    5    8   10
 [4,]    7    5    1
 [5,]    6    3    1
 [6,]    6    7    5
 [7,]   10   10    7
 [8,]    3    3    2
 [9,]    7    9    7
[10,]    4    4    8
于 2011-12-08T18:17:58.610 に答える
0

あなたはこれだけをすることができますね おそらく、引数と戻り値のタイプを調整する必要があります

mat submat_cols(mat X, uvec idx) {
    mat y = X.cols(idx);
    return y;
}

この質問は、ここでの議論にも関連しています。そこにベンチマークを含む3つのソリューションを投稿しました。ただし、論理ベクトルに基づいて行列をサブセット化することです。しかし、これは同様の問題であり、 xx[,c(1:8) %in% c(1,3,4)]代わりにxx[,c(1,3,4)]同じ結果を生成するはずです。いずれにせよ、これらのもののための砂糖溶液があることは本当に素晴らしいでしょう。

于 2012-12-05T20:46:07.493 に答える