[Rcpp-devel] For a Matrix A, is A(i, j) or A[i, j] preferred as an accessor for an element

Dirk Eddelbuettel edd at debian.org
Mon Dec 10 17:53:50 CET 2012


On 10 December 2012 at 10:45, Douglas Bates wrote:
| On Mon, Dec 10, 2012 at 10:38 AM, Romain Francois <romain at r-enthusiasts.com>
| wrote:
| 
|     Le 10/12/12 17:29, Douglas Bates a écrit :
| 
|         or does it matter?
| 
| 
|     A[i,j] is wrong, not valid C or C++ code. so there is only one choice.

No, it's worse, It is valid:

   R> cppFunction("double val(Rcpp::NumericMatrix X) { return X[1,2]; }")
   R> val(matrix(1:9,3,3))
  [1] 3
   R>

| Indeed.  Thanks.  Somehow I managed to convince myself that I had used A[i,j]
| at some point and the compiler accepted it.  Apparently my memory isn't what
| it used to be.

.. but it is possibly the worst gotcha we can have in C++ and matrices as the
comma _is_ in fact a valid operator coming from C --- here though A[i,j] simply
collapses to A[j] with i being ignored as comma sequences the statements
around it.

Dirk
-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com  


More information about the Rcpp-devel mailing list