简体   繁体   English

是否有一个c ++矩阵库,可以像R一样使用非连续向量对矩阵进行索引?

[英]Is there a c++ matrix library where I can index matrices with non-contiguous vectors as in R?

I believe boost has a limitation on contiguous or at least step-wise consistent slicing of matrices. 我认为boost对矩阵的连续或至少逐步一致的切片有局限性。 In R, I could have a random vector c(5,2,8) and use that to index into a matrix M[c(5,2,8),] for example... 在R中,我可以有一个随机向量c(5,2,8),并使用它来索引矩阵M [c(5,2,8),]。

Armadillo supports this as of version 3.0 which was released not even two weeks ago. Armadillo从3.0版开始支持此功能,该版本甚至在两周前就已发布。

Here is a worked example via RcppArmadillo : 这是通过RcppArmadillo制作的示例:

R> library(inline)
R> 
R> code <- '
+   arma::mat  M = Rcpp::as<arma::mat>(m);   // normal matrix
+   arma::uvec V = Rcpp::as<arma::uvec>(v);  // unsigned int vec
+   arma::mat  N = M.cols(V);                // index matrix by vec
+   return Rcpp::wrap(N);
+ '
R> 
R> fun <- cxxfunction(signature(m="numeric", v="integer"),
+                    code,
+                    plugin="RcppArmadillo")
R> M <- matrix(1:25,5,5)
R> V <- c(1L, 3L, 5L) - 1     # offset by one for zero indexing
R> fun(M, V)
     [,1] [,2] [,3]
[1,]    1   11   21
[2,]    2   12   22
[3,]    3   13   23
[4,]    4   14   24
[5,]    5   15   25
R> 

There is a matching function to pick rows rather than columns. 有一个匹配功能可以选择行而不是列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM