I have been using the following method to create L(x,t) x<-seq(1,66) time<-seq(1,19) v0<-.01 f0<-function(x,time) .45*cos(2*pi*3*x/66+v0*time) L0<-outer(x,time,f0) If I do image(L0) I get an x,t plot of a drifting cosine wave grating. The grating is actually a pattern of fuzzy bars; I have been ignoring the y dimension (because the grating is vertical: y has no effect on the luminance). However now I want to use it, and get L0(x,y,t). I don't really understand outer, and my experiments with it have not worked. The basic idea is that I want a 19 frame movie and each frame is a 66 x 31 image of a cosine grating. How to do it? Thanks very much for any help! Bill -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
In case I wasn't clear, I am trying to create a 3D array set up like this: 111000111000 t=1 111000111000 111000111000 111000111000 111000111000 011100011100 t=2 011100011100 011100011100 011100011100 011100011100 001110001110 t=3 001110001110 001110001110 001110001110 001110001110 How to do it? With C one would do v0=.01; for(t=1;t<=19;t++) for(x=1;x<=66;x++) for(y=1;y<=31;y++) L0[x][y][t] = .45*cos(2*pi*3*x/66+v0*time); Thanks very much for any help. Bill -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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. 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) } Bill -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._