RcppAramadillo Cube::operator() : 索引越界

复合生物剂

我一直在摆弄以下用于与我编写的 R 代码集成的 C++ 代码(此处包含的代码太多),但不断收到错误,提示 Cube::operator() 索引超出范围,我不确定为什么会发生这种情况。我的怀疑是 3D 数组没有按照中所述正确填充

在 Rcpp 中使用 arma::cube 制作 3d 数组显示多维数据集错误

但我不确定如何正确解决这个问题。

下面是我的完整 C++ 代码:

// [[Rcpp::depends(RcppArmadillo)]]
#define ARMA_DONT_PRINT_OPENMP_WARNING
#include <RcppArmadillo.h>
#include <RcppArmadilloExtensions/sample.h>
#include <set>
using namespace Rcpp;


int sample_one(int n) {
  return n * unif_rand();
} 

int sample_n_distinct(const IntegerVector& x, 
                  int k,
                  const int * pop_ptr) {

  IntegerVector ind_index = RcppArmadillo::sample(x, k, false); 
  std::set<int> distinct_container;

  for (int i = 0; i < k; i++) {
     distinct_container.insert(pop_ptr[ind_index[i]]);
  }

  return distinct_container.size();
}

// [[Rcpp::export]]
arma::Cube<int> fillCube(const arma::Cube<int>& pop,
                     const IntegerVector& specs,
                     int perms,
                     int K) {

int num_specs = specs.size();
arma::Cube<int> res(perms, num_specs, K);

IntegerVector specs_C = specs - 1;
const int * pop_ptr;
int i, j, k;

for (i = 0; i < K; i++) {
    for (k = 0; k < num_specs; k++) { 
        for (j = 0; j < perms; j++) {
            pop_ptr = &(pop(0, sample_one(perms), sample_one(K)));
            res(j, k, i) = sample_n_distinct(specs_C, k + 1, pop_ptr);
        }
    }
}

return res;
}

有人知道可能导致上述错误的原因吗?

下面是调用 C++ 函数的 R 代码(包括 C++ 代码重现的注释掉的三重嵌套“for”循环)。

## Set up container(s) to hold the identity of each individual from each permutation ##

num.specs <- ceiling(N / K)

## Create an ID for each haplotype ##

haps <- 1:Hstar

## Assign individuals (N) to each subpopulation (K) ##

specs <- 1:num.specs

## Generate permutations, assume each permutation has N individuals, and sample those individuals' haplotypes from the probabilities ##

gen.perms <- function() {
    sample(haps, size = num.specs, replace = TRUE, prob = probs)
}

pop <- array(dim = c(perms, num.specs, K))

for (i in 1:K) {
    pop[,, i] <- replicate(perms, gen.perms())
}

## Make a matrix to hold individuals from each permutation ##

# HAC.mat <- array(dim = c(perms, num.specs, K))

## Perform haplotype accumulation ##

# for (k in specs) {
    # for (j in 1:perms) {
        # for (i in 1:K) {
            # select.perm <- sample(1:nrow(pop), size = 1, replace = TRUE) # randomly sample a permutation
            # ind.index <- sample(specs, size = k, replace = FALSE) # randomly sample individuals
            # select.subpop <- sample(i, size = 1, replace = TRUE) # randomly sample a subpopulation
            # hap.plot <- pop[select.perm, ind.index, select.subpop] # extract data
            # HAC.mat[j, k, i] <- length(unique(hap.plot)) # how many haplotypes are recovered
        # }
    # }
# }

HAC.mat <- fillCube(pop, specs, perms, K)
无外套

这是一个越界错误。问题的要点是电话

pop_ptr = &(pop(0, sample_one(perms), sample_one(K)));

自从

sample_one(perms)

被放置为最大长度为 的访问索引num_specs这可以通过res定义方式看出

arma::Cube<int> res(perms, num_specs, K);

因此,搬出perms了的num_specs地方应该解决这个问题。

// [[Rcpp::export]]
arma::Cube<int> fillCube(const arma::Cube<int>& pop,
                         const IntegerVector& specs,
                         int perms,
                         int K) {

  int num_specs = specs.size();
  arma::Cube<int> res(perms, num_specs, K);

  IntegerVector specs_C = specs - 1;
  const int * pop_ptr;
  int i, j, k;

  for (i = 0; i < K; i++) {
    for (k = 0; k < num_specs; k++) { 
      for (j = 0; j < perms; j++) {

        // swapped location
        pop_ptr = &(pop(sample_one(perms), 0, sample_one(K)));
        // should the middle index be 0?
        res(j, k, i) = sample_n_distinct(specs_C, k + 1, pop_ptr);
      }
    }
  }

  return res;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

索引越界?

来自分类常见问题

数组索引越界Java

来自分类Dev

回文递归;索引越界

来自分类Dev

R中的索引越界

来自分类Dev

列出索引越界

来自分类Dev

if 语句中的索引越界

来自分类Dev

数组索引越界帮助(Java)

来自分类Dev

android numberpicker索引越界旋转

来自分类Dev

数组索引越界异常如何克服

来自分类Dev

数组索引越界异常1> = 1

来自分类Dev

图像传递期间索引越界错误

来自分类Dev

JTable 中的数组索引越界异常

来自分类Dev

java对象复制,数组索引越界

来自分类Dev

使用 charAt 的字符串索引越界

来自分类Dev

我在 SQLite 中得到索引越界异常

来自分类Dev

C# WCF 服务越界数组索引

来自分类Dev

字符串索引越界异常 (charAt)

来自分类Dev

在 Python 中获取索引越界错误

来自分类Dev

Android 中的数组索引越界异常

来自分类Dev

初始化数组的索引越界错误

来自分类Dev

Java-索引越界异常:索引:1,大小:2

来自分类Dev

对单个静态整数索引的多次读写导致索引越界

来自分类Dev

-[NSManagedObjectContext保存:]中的数组索引越界问题(NSRangeException)

来自分类Dev

如何解决Jsoup数组索引越界异常?

来自分类Dev

Java的字符串索引越界异常中

来自分类Dev

索引越界多个子图与除法rest python

来自分类Dev

Octave 索引越界错误。不知道为什么

来自分类Dev

数组索引越界,但 gdb 报告错误的行 - 为什么?

来自分类Dev

从 TObjectList 中删除元素时列表索引越界