[Rcpp-devel] Armadillo reshape

baptiste auguie baptiste.auguie at googlemail.com
Wed Oct 26 02:09:33 CEST 2011


Dear list,

My question isn't directly related to RcppArmadillo, rather to
Armadillo itself, but I thought someone on this list might have come
across my problem with arma::reshape. Consider the following example,
where I want to reshape a vector into a 3x2 matrix filled by rows,

library(RcppArmadillo)
library(inline)

fx <- cxxfunction(signature(x = "matrix") , '
         arma::cx_mat y = as<arma::cx_mat>(x);
	  arma::cx_mat beta1(2,3), beta2(2,3), beta3(2,3);
	  beta1 = arma::reshape(y, 2, 3, 0);
	  beta2 = arma::reshape(y, 2, 3, 1);
	  beta3 = strans(arma::reshape(y, 3, 2, 0));
          return wrap( Rcpp::List::create( beta1, beta2, beta3) ) ;
	', plugin = "RcppArmadillo" )

m <- matrix(1:6 +0i, ncol=1)
fx( m )
[[1]]
     [,1] [,2] [,3]
[1,] 1+0i 3+0i 5+0i
[2,] 2+0i 4+0i 6+0i

[[2]]
     [,1] [,2] [,3]
[1,] 1+0i 3+0i 5+0i
[2,] 2+0i 4+0i 6+0i

[[3]]
     [,1] [,2] [,3]
[1,] 1+0i 2+0i 3+0i
[2,] 4+0i 5+0i 6+0i

The last result is what I want, but I cannot seem to obtain it with
reshape alone (the dim parameter seems to be ignored here).
http://arma.sourceforge.net/docs.html#reshape
	
Is there a more elegant way to do this than using reshape and strans?

Thanks,

baptiste


More information about the Rcpp-devel mailing list