在 Rcpp 中提取矩阵行

Extracting rows of matrix in Rcpp

我正在按照 Rcpp Quick Reference Guide 将矩阵的一行提取为向量。该指南的示例是:

// Copy the second column into new object (xx is a NumericMatrix)
NumericVector zz1 = xx( _, 1);

当我在 Rstudio 中获取以下代码时,我收到以下错误:

#include <Rcpp.h>
using namespace Rcpp;


// [[Rcpp::export]]
int foo1(Rcpp::IntegerVector res)
{
  int output = res[0];
  return output;
}


// [[Rcpp::export]]
int foo(Rcpp::IntegerMatrix res)
{
  int n = res.nrow();
  int output;
  IntegerVector temp_res = res( 1, _);
  // IntegerVector temp_res = res.row(1);

  for (int r = 0; r < n; r++) {
    output = foo1(res = temp_res); ////////   Line 22
    // output = foo1(res = as<IntegerVector>(temp_res));
  }
  return output;
}

为什么会出现此错误?如何从矩阵中提取所需的行并将其用于另一个函数,如上所示?

子集不是错误的来源。事实上,该操作员错误是由于第一个错误而触发的代码的后续部分。

话虽如此,运行 您的代码产生:

fileb9516da5f592.cpp:22:23: error: no viable overloaded '='
    output = foo1(res = temp_res); ////////   Line 22
                  ~~~ ^ ~~~~~~~~
/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include/Rcpp/vector/Matrix.h:83:13: note: candidate function not viable: no known conversion from 'Rcpp::IntegerVector' (aka 'Vector<13>') to 'const Rcpp::Matrix<13, PreserveStorage>' for 1st argument
    Matrix& operator=(const Matrix& other) {
            ^
/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include/Rcpp/vector/Matrix.h:90:13: note: candidate function not viable: no known conversion from 'Rcpp::IntegerVector' (aka 'Vector<13>') to 'const SubMatrix<13>' for 1st argument
    Matrix& operator=( const SubMatrix<RTYPE>& ) ;
            ^
1 error generated.
make: *** [fileb9516da5f592.o] Error 1
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include" -I"/private/var/folders/b0/vt_1hj2d6yd8myx9lwh81pww0000gn/T/RtmpRe7iKX/sourceCpp-x86_64-apple-darwin15.6.0-1.0.3" -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include  -fPIC  -Wall -g -O2  -c fileb9516da5f592.cpp -o fileb9516da5f592.o

注意,第一个错误是:

fileb9516da5f592.cpp:22:23: error: no viable overloaded '='
    output = foo1(res = temp_res); ////////   Line 22
                  ~~~ ^ ~~~~~~~~

由于命名参数传递,代码抛出错误。与 R 不同,C++ 不支持命名参数。解决方案是使用位置参数。

即变化:

    output = foo1(res = temp_res); ////////   Line 22

至:

    output = foo1(temp_res); ////////   Line 22

瞧!

foo(matrix(1:4))
#[1] 2

同意@coatless 的观点,并且在写他的时候正在看这个。您的代码本质上是混乱的,您需要弄清楚您要解决的问题。

我这里解决你标题的问题:提取行。正如你想要 return 一个 标量 int 我只是总结了第一个元素。

代码

#include <Rcpp.h>

// [[Rcpp::export]]
int foo1(Rcpp::IntegerVector res) {
  int output = res[0];
  return output;
}


// [[Rcpp::export]]
int foo(Rcpp::IntegerMatrix res) {
  int n = res.nrow();
  int output = 0;               // need to init

  for (int r = 0; r < n; r++) {
    Rcpp::IntegerVector temp_res = res( r, Rcpp::_);
    output = output + temp_res[0];
  }
  return output;
}

/*** R
M <- matrix(seq(1,9), 3, 3)
foo(M)
*/

输出

R> Rcpp::sourceCpp("~/git/Whosebug/60161951/question.cpp")

R> M <- matrix(seq(1,9), 3, 3)

R> foo(M)
[1] 6
R>