【问题标题】:How to extract multiple values from a NumericVector in C++如何从 C++ 中的 NumericVector 中提取多个值
【发布时间】:2013-07-19 10:20:43
【问题描述】:

对于Rcpp 及其功能,我还是个新手,更不用说 C++ 本身了,所以对于你们当中的专家来说,这可能看起来微不足道。但是,没有愚蠢的问题,所以无论如何:

我想知道是否有一种方法可以在 C++ 中使用索引同时处理 NumericVector 的多个元素。为了让整个事情更清楚,这是我正在尝试做的 R 等价物:

# Initial vector
x <- 1:10

# Extract the 2nd, 5th and 8th element of the vector
x[c(2, 5, 8)]
[1] 2 5 8

这是我目前在 R 中使用 sourceCpp 执行的 C++ 函数中得到的结果。它有效,但对我来说似乎很不方便。有没有更简单的方法来实现我的目标?

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector subsetNumVec(NumericVector x, IntegerVector index) {  
  // Length of the index vector
  int n = index.size();
  // Initialize output vector
  NumericVector out(n);

  // Subtract 1 from index as C++ starts to count at 0
  index = index - 1; 
  // Loop through index vector and extract values of x at the given positions
  for (int i = 0; i < n; i++) {
    out[i] = x[index[i]];
  }

  // Return output
  return out;
}

/*** R
  subsetNumVec(1:10, c(2, 5, 8))
*/
>   subsetNumVec(1:10, c(2, 5, 8))
[1] 2 5 8

【问题讨论】:

    标签: c++ r indexing rcpp


    【解决方案1】:

    如果您使用 Armadillo 向量而不是 Rcpp 向量,则可以执行此操作。

    Rcpp Gallery 有一个post with a complete example:具体参见第二个示例。您的索引条目必须位于(未签名的)uvecumat

    【讨论】:

      【解决方案2】:

      我认为没有更短的方法了!

      但是你的NumericVector subsetNumVec(NumericVector x, IntegerVector index) 容易出错:

      在这一行内

      out[i] = x[index[i]];
      

      您无需范围检查即可访问向量。因此,在一般情况下,x 为空或索引超出范围,您会得到一些未定义的行为。

      此外,您的方法可以通过引用调用

      NumericVector subsetNumVec(const NumericVector& x, const IntegerVector& index)
      

      没有理由复制两个向量。您只需将减法 index = index -1; 移动到 out[i] = x.at(index[i] - 1);

      在这里,x.at(index[i] - 1) 抛出错误索引。但是你需要一些错误处理(返回空向量或在外部进行处理)。

      【讨论】:

        猜你喜欢
        • 2019-07-21
        • 1970-01-01
        • 2022-01-28
        • 2012-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多