Get error when use Rcpp remove rows of matrix

。_饼干妹妹 提交于 2021-02-05 09:37:40

问题


#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'


回答1:


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


来源:https://stackoverflow.com/questions/52325480/get-error-when-use-rcpp-remove-rows-of-matrix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!