math_daddy
2012-May-14 14:09 UTC
[R] How to apply a function to a multidimensional array based on its indices
Hello. I have a 4 dimensional array and I want to fill in the slots with values which are a function of the inputs. Through searching the forums here I found that the function "outer" is helpful for 2x2 matrices but cannot be applied to general multidimensional arrays. Is there anything which can achieve, more efficiently than the following code, the job I want? K <- array(0,dim=c(2,2,2,2)) #dimensions will be much larger for(x1 in 1:2) { for(y1 in 1:2) { for(x2 in 1:2) { for(y2 in 1:2) { K[x1,y1,x2,y2] <- x1*y2 - sin(x2*y1) #this is just a dummy function. } } } } Thank you in advance for any help. -- View this message in context: http://r.789695.n4.nabble.com/How-to-apply-a-function-to-a-multidimensional-array-based-on-its-indices-tp4629940.html Sent from the R help mailing list archive at Nabble.com.
Rui Barradas
2012-May-14 16:04 UTC
[R] How to apply a function to a multidimensional array based on its indices
Hello, Try K <- array(0,dim=c(2,2,2,2)) #dimensions will be much larger for(x1 in 1:2){ for(y1 in 1:2) { for(x2 in 1:2) { for(y2 in 1:2) { K[x1,y1,x2,y2] <- x1*y2 - sin(x2*y1) #this is just a dummy function. } } } } fun <- function(x){ x1 <- x[1] # these are x2 <- x[2] # not y1 <- x[3] # really y2 <- x[4] # needed x1*y2 - sin(x2*y1) # could have used x[1], etc } res <- apply(expand.grid(1:2, 1:2, 1:2, 1:2), 1, fun) dim(res) <- c(2, 2, 2, 2) res all.equal(K, res) [1] TRUE See the help pages ?expand.grid ?apply Hope this helps, Rui Barradas math_daddy wrote> > Hello. I have a 4 dimensional array and I want to fill in the slots with > values which are a function of the inputs. Through searching the forums > here I found that the function "outer" is helpful for 2x2 matrices but > cannot be applied to general multidimensional arrays. Is there anything > which can achieve, more efficiently than the following code, the job I > want? > > K <- array(0,dim=c(2,2,2,2)) #dimensions will be much larger > for(x1 in 1:2) > { > for(y1 in 1:2) > { > for(x2 in 1:2) > { > for(y2 in 1:2) > { > K[x1,y1,x2,y2] <- x1*y2 - sin(x2*y1) #this is just a dummy > function. > } > } > } > } > > Thank you in advance for any help. >-- View this message in context: http://r.789695.n4.nabble.com/How-to-apply-a-function-to-a-multidimensional-array-based-on-its-indices-tp4629940p4629955.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2012-May-14 17:22 UTC
[R] How to apply a function to a multidimensional array based on its indices
On May 14, 2012, at 10:09 AM, math_daddy wrote:> Hello. I have a 4 dimensional array and I want to fill in the slots > with > values which are a function of the inputs. Through searching the > forums here > I found that the function "outer" is helpful for 2x2 matrices but > cannot be > applied to general multidimensional arrays. Is there anything which > can > achieve, more efficiently than the following code, the job I want? > > K <- array(0,dim=c(2,2,2,2)) #dimensions will be much larger > for(x1 in 1:2) > { > for(y1 in 1:2) > { > for(x2 in 1:2) > { > for(y2 in 1:2) > { > K[x1,y1,x2,y2] <- x1*y2 - sin(x2*y1) #this is just a dummy > function. > } > } > } > } >If you can create a data.frame or matrix that has the indices x1,x2,y1,y2 and the values you can use the: K[cbind(index-vectors)] <- values construction: mtx<- data.matrix( expand.grid(x1=1:2,x2=1:2,y1=1:2,y2=1:2) ) K[mtx] <- apply(mtx, 1, function(x) x["x1"]*x["y2"] - sin(x['x2']*x['y1']) ) #---------------- > K , , 1, 1 [,1] [,2] [1,] 0.158529 0.09070257 [2,] 1.158529 1.09070257 , , 2, 1 [,1] [,2] [1,] 0.09070257 1.756802 [2,] 1.09070257 2.756802 , , 1, 2 [,1] [,2] [1,] 1.158529 1.090703 [2,] 3.158529 3.090703 , , 2, 2 [,1] [,2] [1,] 1.090703 2.756802 [2,] 3.090703 4.756802> Thank you in advance for any help. > > > -- > View this message in context: http://r.789695.n4.nabble.com/How-to-apply-a-function-to-a-multidimensional-array-based-on-its-indices-tp4629940.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT
math_daddy
2012-May-15 12:41 UTC
[R] How to apply a function to a multidimensional array based on its indices
Thanks for the help, this worked great! Much appreciated. -- View this message in context: http://r.789695.n4.nabble.com/How-to-apply-a-function-to-a-multidimensional-array-based-on-its-indices-tp4629940p4630068.html Sent from the R help mailing list archive at Nabble.com.