问题描述
我是Rcpp的新手,正在尝试使用RcppArmadillo在for()
循环中基于负索引进行计算。
我已经发现RcppArmadillo中的负索引并不是那么简单,但是可以通过应保留(as I found here)的元素向量来完成。当要删除的元素是循环索引时,这对我来说似乎有点困难。我尝试在this answer中实现最后一种方法,但是没有成功。是否有一种简单的方法来指定除具有循环索引的元素以外的元素向量?
因此,我试图在以下MWE中为y[-i]
在RcppArmadillo中找到等效项:
# In R:
# Input
n <- 10
y <- 1:20
# Computation
x <- rep(NA,n)
for(i in 1:n){
x[i] <- sum(y[-i])
}
到目前为止,我的代码是Rcpp代码:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
// [[Rcpp::export]]
arma::vec rcpp_sum (arma::vec y,int n){
arma::vec x(n);
arma::vec ind(n);
for (i=0; i<n; i++){
ind[i] = /*no idea...*/
x[i] = sum(y[ind]);
}
return x;
}
非常感谢您的帮助!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)