Search code examples
rrcpparmadillo

Get error when use Rcpp remove rows of matrix


#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
// [[Rcpp::export]]
arma::mat fed(arma::mat x){
arma::mat zz=x.shed_rows(0,2);
return(zz);
}

Just want remove some rows from matrix, get error as follows. conversion from 'void' to non-scalar type 'arma::Mat} requested'


Solution

  • Two points:

    • Please don't post error messages as image. Use Text instead.
    • As the error indicates, the shed_rows() method does not return anything. Instead it alters the matrix it acts on, c.f. the documentation.

    The following works:

    #include <RcppArmadillo.h>
    // [[Rcpp::depends(RcppArmadillo)]]
    using namespace Rcpp;
    // [[Rcpp::export]]
    arma::mat fed(arma::mat x){
        x.shed_rows(0,2);
        return(x);
    }
    
    /*** R
    fed(matrix(1:16, 4 ,4))
    */