是否有一个 c++ 矩阵库,我可以在其中索引具有非连续向量的矩阵,如 R 中的矩阵

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

本文关键字:向量 连续 索引 c++ 有一个 我可以 是否 在其中      更新时间:2023-10-16

我相信boost对矩阵的连续或至少逐步一致的切片有局限性。在 R 中,我可以有一个随机向量 c(5,2,8),并使用它来索引到矩阵 M[c(5,2,8),] 例如......

犰狳从两周前发布的3.0版本开始支持此功能。

这是通过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> 

有一个匹配函数来选择行而不是列。