Dear R, I have two small questions confused me recently. Now assume I have a matrix "a", like this,> a <- matrix(1:6, 2, 3) > a[,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 I sometimes need each row of "a" raised to a different exponent. So I do a trick like this,> a^c(2, 3)[,1] [,2] [,3] [1,] 1 9 25 [2,] 8 64 216 My first question is that if it is possible to do this trick column wise? Just out of curiosity, of course I know there are other ways of doing this. And the second question is why I get such result when I put another element in the exponent part like this,> a^c(2, 3, 4)[,1] [,2] [,3] [1,] 1 81 125 [2,] 8 16 1296 BTW, I have a 64bit R version (2.11) for Linux. Any advice would be appreciated. Feng -- Feng Li Department of Statistics Stockholm University 106 91 Stockholm, Sweden http://feng.li/ [[alternative HTML version deleted]]
"^" is vectorized operator, so a^c(2,3) is essentially the same as a^rep(c(2,3), length.out = length(a)) which is c(a)^rep(c(2,3), length.out = length(a)) but put back in a matrix format (i.e., with rows and columns). Now, if you want each column in different power, you need to explicitly use rep(), e.g., a^rep(c(2,3,4), each = nrow(a)) I hope it helps. Best, Dimitris On 9/7/2010 6:35 PM, Feng Li wrote:> Dear R, > > I have two small questions confused me recently. Now assume I have a matrix > "a", like this, > >> a<- matrix(1:6, 2, 3) >> a > [,1] [,2] [,3] > [1,] 1 3 5 > [2,] 2 4 6 > > I sometimes need each row of "a" raised to a different exponent. So I do a > trick like this, > >> a^c(2, 3) > [,1] [,2] [,3] > [1,] 1 9 25 > [2,] 8 64 216 > > My first question is that if it is possible to do this trick column wise? > Just out of curiosity, of course I know there are other ways of doing this. > > And the second question is why I get such result when I put another element > in the exponent part like this, > >> a^c(2, 3, 4) > [,1] [,2] [,3] > [1,] 1 81 125 > [2,] 8 16 1296 > > > > BTW, I have a 64bit R version (2.11) for Linux. Any advice would be > appreciated. > > > > Feng >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
Feng, Hello, all of this behavior comes down to argument recycling. Feng Li wrote:> Dear R, > > I have two small questions confused me recently. Now assume I have a matrix > "a", like this, > >> a <- matrix(1:6, 2, 3) >> a > [,1] [,2] [,3] > [1,] 1 3 5 > [2,] 2 4 6 > > I sometimes need each row of "a" raised to a different exponent. So I do a > trick like this, > >> a^c(2, 3) > [,1] [,2] [,3] > [1,] 1 9 25 > [2,] 8 64 216Right, so you have as.vector(a) [1] 1 2 3 4 5 6 and a^c(2,3) will then use argument recycling to compute 1^2 2^3 3^2 4^3 5^2 6^3 and then return the matrix in its original dimension.> > My first question is that if it is possible to do this trick column wise?Yes, just use t to transpose.> Just out of curiosity, of course I know there are other ways of doing this. > > And the second question is why I get such result when I put another element > in the exponent part like this, > >> a^c(2, 3, 4) > [,1] [,2] [,3] > [1,] 1 81 125 > [2,] 8 16 1296Same reason as it worked above, argument recycling, now you get 1^2 2^3 3^4 4^2 5^3 6^4> > > > BTW, I have a 64bit R version (2.11) for Linux. Any advice would be > appreciated. > > > > Feng >
On Sep 7, 2010, at 12:35 PM, Feng Li wrote:> Dear R, > > I have two small questions confused me recently. Now assume I have a > matrix > "a", like this, > >> a <- matrix(1:6, 2, 3) >> a > [,1] [,2] [,3] > [1,] 1 3 5 > [2,] 2 4 6 > > I sometimes need each row of "a" raised to a different exponent. So > I do a > trick like this, > >> a^c(2, 3) > [,1] [,2] [,3] > [1,] 1 9 25 > [2,] 8 64 216 > > My first question is that if it is possible to do this trick column > wise?Most questions of this sort are answerable by thinking of R matrices as folded vectors. The folding occurs columnwise (unlike Matlab), so for this problem:> > a^rep(c(2, 3, 4), each=nrow(a)) # the exponents become 2,2,3,3,4,4 > [,1] [,2] [,3] > [1,] 1 27 625 > [2,] 4 64 1296 >or: > a^matrix(c(2, 3, 4), byrow=TRUE, nrow=2, ncol=3) [,1] [,2] [,3] [1,] 1 27 625 [2,] 4 64 1296> Just out of curiosity, of course I know there are other ways of > doing this. > > And the second question is why I get such result when I put another > element > in the exponent part like this,Because argument recycling makes the exponents 2,3,4,2,3,4 and they are applied folded column wise> >> a^c(2, 3, 4) > [,1] [,2] [,3] > [1,] 1 81 125 > [2,] 8 16 1296 > > > > BTW, I have a 64bit R version (2.11) for Linux. Any advice would be > appreciated.David Winsemius, MD West Hartford, CT