Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing a Matrix to NA in Rcpp

Tags:

rcpp

There is a way to initialize Numeric vector with NA values like.

NumericVector x(10,NumericVector::get_na())

is there any similar way to initialize a matrix to NA values?

like image 755
gman Avatar asked May 19 '14 23:05

gman


1 Answers

Here is a version that does not waste memory.

#include <Rcpp.h>
using namespace Rcpp ;

// [[Rcpp::export]]
NumericMatrix na_matrix(int n){
  NumericMatrix m(n,n) ;
  std::fill( m.begin(), m.end(), NumericVector::get_na() ) ;
  return m ;
}

FWIW, in Rcpp11, you can use some more expressive syntax:

NumericMatrix m(n,n, NA) ;

Thanks to this constructor

like image 90
Romain Francois Avatar answered Oct 28 '22 03:10

Romain Francois