Skip to content

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

3 messages · Jonathan Rougier, Bill Simpson

#
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,
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?
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Thanks very much Jonathon for your help!
Yes I realize this looks strange. The function is the image intensity for
a drifting vertical sinewave grating (fuzzy bars). So the image always has
the same intensity for all y values at a given x. I can't just ignore y
because the energy is affected by it.

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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._