[Rcpp-devel] Problems indexing matrics

Hadley Wickham hadley at rice.edu
Wed Jan 4 22:03:41 CET 2012


> From http://dirk.eddelbuettel.com/code/rcpp/html/Matrix_8h_source.html#l00058
> The constructor for matrix is Matrix(nrow, ncol, iterator), there is no
> constructor it seems for initializing the default value. So your problem is
> Rcpp::NumericMatrix output(x_s + x_k - 1, y_s + y_k - 1, 0.0);
> should be
> Rcpp::NumericMatrix output(x_s + x_k - 1, y_s + y_k - 1);
>
> and then a separate loop for initialization. I'd like to know if someone
> knows of another way of initialization.

Got it - thanks.  Is there a way to diagnose the problem from the
error message?

I guess looking at Matrix.h:60 is helpful:

    Matrix( const int& nrows_, const int& ncols, Iterator start ) :
        VECTOR( start, start + (nrows_*ncols) ),
        nrows(nrows_)
    {
        VECTOR::attr( "dim" ) = Dimension( nrows, ncols ) ;
    }

given that the 3rd argument is an iterator.

Hadley


-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/


More information about the Rcpp-devel mailing list