gpt4 book ai didi

c++ - 是否有一个 c++ 矩阵库,我可以在其中像 R 中那样使用非连续 vector 索引矩阵?

转载 作者:行者123 更新时间:2023-11-27 23:24:44 26 4
gpt4 key购买 nike

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

最佳答案

Armadillo从不到两周前发布的 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>

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

关于c++ - 是否有一个 c++ 矩阵库,我可以在其中像 R 中那样使用非连续 vector 索引矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10216704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com