Thanks for answering.

By
"It does change the default rownames and colnames of the NumericMatrix"
understand
"It does not change the default rownames and colnames of the returned
NumericMatrix."

Actually, I don't want to create a copy of the matrix.
I want to create a new matrix which as the same size as the original
one, the same rownames and the same colnames but not the same content.

I may clone the original matrix and reset his content after. Is clone as
fast as creating a new matrix?

Le 11/08/2015 13:19, Dirk Eddelbuettel a écrit :

On 11 August 2015 at 12:30, Florian Plaza Oñate wrote:
| Hi everyone,
|
| I am trying to copy the rownames and the colnames from a NumericMatrix
| to another NumericMatrix by using Rcpp
|
| Here is my code:
|
| Rcpp::NumericMatrix copy(const Rcpp::NumericMatrix& m)
| {
|    Rcpp::NumericMatrix m2(num_genes, num_samples);
|    Rcpp::rownames(m2) = Rcpp::rownames(m);
|    Rcpp::colnames(m2) = Rcpp::colnames(m);

If you want a full copy, try

    Rcpp::NumericMatrix m2 = Rcpp::clone(m);

as in

R> cppFunction("NumericMatrix foo(NumericMatrix m) { return Rcpp::clone(m); }");
R> m <- matrix(1:4,2,2,TRUE,list(c("a","b"), c("c", "d")))
R> foo(m)
   c d
a 1 2
b 3 4
R>

|    return m2;
| }
|
| It does change the default rownames and colnames of the NumericMatrix.
| Do you know how to do it?

Now I am confused. You do want to change them or not?

If you do _not_ use clone(), then m2 and m share the same underlying
pointer.  This has been explained before: we use proxy objects which are more
lightweight.  Distinct copies require clone().

Dirk



_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to