0

I'm trying to use Rcpp to multiply part of a row of a NumericMatrix by a NumericVector. A simple version of my situation is follows:

int temp() {

NumericMatrix matr(3,3);
NumericVector vec(2, 5);

int matsize = matr.nrow() * matr.ncol();

for(int i = 0; i < matsize; i++)
  matr[i] = i;

int value = sum(matr(1, Range(1,2)) * vec );

return(value);
}

The part that is giving me difficulty is int value = sum(matr(1, Range(1,2)) * vec );.

This line gives the error

no matching function for call to object of type 'NumericMatrix' (aka 'Matrix<14>')

when I hover over the red x on the code line in RStudio.

Does anyone have a solution?

Edit: I found a solution using a temp variable.

int temp() {

  NumericMatrix matr(3,3);
  NumericVector vec = NumericVector::create(2, 5);

  int matsize = matr.nrow() * matr.ncol();

  for(int i = 0; i < matsize; i++)
    matr[i] = i;

  NumericVector temp = matr(1, _);

  NumericVector value = temp[Range(1,2)] * vec ;

  return(sum(value));
}
Scott White
  • 190
  • 2
  • 14
  • Also: http://stackoverflow.com/questions/37191673/matrix-multiplication-in-rcpp – coatless May 11 '17 at 21:47
  • Thanks for the links @coatless but here I'm only trying to multiply part of a matrix row, and not the whole row which is what is demonstrated in the links you have provided. If I were to try and use the material in those links, is there a way to select a part of a row of an arma::mat? and can I pass a NumericVector to a function that is expecting an arma::mat? I tried working with RcppArmadillo before and couldn't get it to work last time, so I'm hesitant to use it. – Scott White May 11 '17 at 22:53
  • As said before here and in the Rcpp FAQ: Consider RcppArmadillo for this. – Dirk Eddelbuettel May 11 '17 at 23:12

0 Answers0