So I'm trying to figure out what this function aperm()
does. When I do aperm(a)
, where a
is a matrix, I get its transpose; I get that. But what about this function:
aperm(a,c(3,1:2))
what does this do? when a
is a 6*7 matrix this command doesn't work.
I dont understand the R example either.
You receive the error because you are telling aperm
to permute a 3 dimensional array, but only providing it a 2 dimensional array (a matrix). You need a 3 dimensional array for your command to work:
Consider the following example:
myarray <- array( 1:24, dim=c(2,3,4),
dimnames=list(One=c('a','b'), Two=c('A','B','C'), Three=1:4) )
myarray
aperm(myarray, c(3,1,2))
This creates a 3 dimensional array with dimnames to help make it clearer, then permutes it. Notice the new order of the values.
Also this example:
> myarray[2,3,4]
[1] 24
>
> mynewarray <- aperm(myarray, c(3,1,2) )
> mynewarray[4,2,3]
[1] 24
>
To get the element with value 24 in the original array we take the 2nd row of the 3rd column of the 4th layer (or whatever you want to call the 3rd dimension).
But in the permuted array it is now the 4th row (that used to be layer) of the 2nd column (which used to be rows) of the 3rd layer (that used to be columns).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With