I have a multidimensional array - in this case with 4 dimensions of x,y,z and time. I'd like to take the time derivative of this matrix, i.e. perform the diff operator along dimension number 4. In Matlab, there is a simple option to specify which dimension the difference is to be taken across., but I can't see this in R. I realise I can use aperm to rotate my array so that time is in the 1st dimension, then do the difference, but this seems a little messy. Surely I can't be the first person wanting to do this, but I can't find any answers after doing a (what I thought was thorough) search. Thanks in advance, Phil. [[alternative HTML version deleted]]
Vincent Zoonekynd
2012-Jan-09 03:10 UTC
[R] Difference across the Nth dimension of an array
On 9 January 2012 08:53, Phil Wiles <philip.wiles at gmail.com> wrote:> I have a multidimensional array - in this case with 4 dimensions of x,y,z > and time. ?I'd like to take the time derivative of this matrix, i.e. > perform the diff operator along dimension number 4."apply" can do that, but you may need to reorder the dimensions of the result. a <- array(1:81,dim=c(3,3,3,3)) b <- apply(a, 1:3, diff) b <- aperm(b,c(2:4,1)) dim(b) -- Vincent