HBat
HBat

Reputation: 5682

Extracting rows of matrix in Rcpp

I'm following Rcpp Quick Reference Guide to extract a row of a matrix as a vector. The guide's example is:

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

When I source the following code in Rstudio, I'm receiving the following error:

enter image description here

#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;
}

Why I'm getting this error? How can I extract a desired row from a matrix and use it in another function as shown above?

Upvotes: 3

Views: 1233

Answers (2)

Dirk is no longer here
Dirk is no longer here

Reputation: 368181

Agree with @coatless and was looking at this while wrote his. Your code is essentially muddled and you need to figure out what problem you are trying to solve.

Here I solve the issue of your title: extracting rows. And as you want to return a scalar int I just sum up the first element.

Code

#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)
*/

Output

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

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

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

Upvotes: 2

coatless
coatless

Reputation: 20746

Subsetting is not where the error is coming from. In fact, that operator error is from a later portion of the code being triggered due to the first error.

Having said this, running your code yields:

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

Note, the first error is:

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

The code is throwing an error because of a named parameter pass. Unlike R, C++ does not support named parameters. The resolution is to use positional parameters.

That is, change:

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

to:

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

Voila!

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

Upvotes: 5

Related Questions