[R] 3D array was L(x,y,t)?

Jonathan Rougier J.C.Rougier at durham.ac.uk
Thu May 4 10:56:18 CEST 2000


Hi Bill,

> This is what I wound up doing to create a 3D array: I used outer to create
> a 2D array, and used a loop around outer to handle the third index.
> 
> I guess R/S only has constructs for fast 2D array handling (e.g. outer).
> If you have higher dimensional arrays you apply outer() to 2D portions of
> them.
> 
> I would appreciate any improvements over my solution.

Well your function does not appear to need outer at all.  If you want to
create a n-D array of function values over a grid in n variables and your
function is simple, as yours appears to be, you can just use (for your
example)

grid <- expand.grid(x=seq(1, 66), y=seq(1, 31), time=seq(1, 19))
grid <- as.matrix(grid)
z0 <- apply(grid, 1, function(v) f0(v[1], v[2], v[3]))

(although this is not optimal!).  Then just dim(z0) appropriately.

However, your functions do appear to be a bit strange.  Can it be right
that y appears nowhere in the body of either f0 or f1?

> corr.contrast3<-function(v0=.01, v1=-.01, phase=0)
> {
> #uses 3D rep of signals: s0(x,y,t)
> #note corr=0 if v1=v0+pi/2 (orthogonal)
> x<-seq(1,66)
> y<-seq(1,31)
> z0<-rep(0,66*31*19)
> dim(z0)<-c(66,31,19)   
> z1<-z0
> 
> f0<-function(x,y,time) .45*cos(2*pi*3*x/66+v0*time)
> f1<-function(x,y,time) .45*cos(2*pi*3*x/66+v1*time+phase)
> 
> for(time in seq(1,19))
>         {
>         z0[,,time]<-outer(x,y,f0,time)
>         z1[,,time]<-outer(x,y,f1,time)
>         }
>     
> energy<-sum(z0^2+z1^2)/2   
> corr<-sum(z0*z1)/energy
>         
> list(corr=corr,energy=energy)
> }

Cheers, Jonathan.

Jonathan Rougier                       Science Laboratories
Department of Mathematical Sciences    South Road
University of Durham                   Durham DH1 3LE
http://www.maths.dur.ac.uk/stats/people/jcr/jcr.html

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list