Initializing a Matrix to NA in Rcpp

rcpp

Solution

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

Problem

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?

Original source