Rcpp NumericMatrix - 如何擦除行/列?

Rcpp NumericMatrix - how to erase a row / column?

新手学习Rcpp classes/数据结构的一个问题:classRcpp::NumericMatrix的对象有没有擦除行/列的成员函数? (或其他类型的 type **Matrix -- 我假设它是一个模板 class)?

library(Rcpp)
cppFunction('
  NumericMatrix sub1 {NumericMatrix x, int& rowID, int& colID) {
    // let's assume separate functions for rowID or colID
    // but for the example case here
    x.row(rowID).erase(); // ??? does this type of member function exist?
    x.col(colID).erase(); // ???
    return x;
}')

如果不存在这种类型的成员函数,那么这个呢?

cppFunction('NumericMatrix row_erase (NumericMatrix& x, int& rowID) {
  // a similar function would exist for removing a column.
  NumericMatrix x2(Dimension(x.nrow()-1, x.ncol());
  int iter = 0; // possibly make this a pointer?
  for (int i = 0; i < x.nrow(); i++) {
    if (i != rowID) {
      x2.row(iter) = x.row(i);
      iter++;
    }
  }
  return x2;
}')

或者我们希望删除一组 rows/columns:

cppFunction('NumericMatrix row_erase (NumericMatrix& x, IntegerVector& rowID) {
  // a similar function would exist for removing a column.
  rowID = rowID.sort();

  NumericMatrix x2(Dimension(x.nrow()- rowID.size(), x.ncol());
  int iter = 0; // possibly make this a pointer?
  int del = 1; // to count deleted elements
  for (int i = 0; i < x.nrow(); i++) {
    if (i != rowID[del - 1])
      x2.row(iter) = x.row(i);
      iter++;
    } else {
      del++;
    }
  }
  return x2;
}')

是的,这两个都有效(修正了我上面的拼写错误)。我在尝试用 Rcpp::NumericMatrix::iterator iter 替换 int iter 时遇到转换错误。有什么解决办法吗?

请注意,我们不需要 row_erase(NumericMatrix& x, int& ref),因为这是 row_erase(NumericMatrix& x, IntegerVector& ref) 的特例。

NumericMatrix row_erase (NumericMatrix& x, IntegerVector& rowID) {
  rowID = rowID.sort();

  NumericMatrix x2(Dimension(x.nrow()- rowID.size(), x.ncol()));
  int iter = 0; 
  int del = 1; // to count deleted elements
  for (int i = 0; i < x.nrow(); i++) {
    if (i != rowID[del - 1]) {
      x2.row(iter) = x.row(i);
      iter++;
    } else {
      del++;
    }
  }
  return x2;
}

NumericMatrix col_erase (NumericMatrix& x, IntegerVector& colID) {
  colID = colID.sort();

  NumericMatrix x2(Dimension(x.nrow(), x.ncol()- colID.size()));
  int iter = 0; 
  int del = 1; 
  for (int i = 0; i < x.ncol(); i++) {
    if (i != colID[del - 1]) {
      x2.col(iter) = x.column(i);
      iter++;
    } else {
      del++;
    }
  }
  return x2;
}

使用 RcppArmadillo 怎么样?我认为代码的意图会更清楚...

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]

using namespace arma;

// [[Rcpp::export]]
mat sub1( mat x, uword e) {
  x.shed_col(e-1);
  x.shed_row(e-1);
  return x;
}

/*** R
sub1( matrix(1:9,3), 2 )
*/

> sub1( matrix(1:9,3), 2 )
     [,1] [,2]
[1,]    1    7
[2,]    3    9