Dear everybody, I have a following problem. I have a 3D array lambda <- array( dim=c(N,M,M-1)) where I have to extract the elements as follows: lambda[ 1, state[1], 1] lambda[ 1, state[1], 2] ... lambda[ 1, state[1], M-1] lambda[ 2, state[2], 1] ... lambda[ 2, state[2], M-1] ... lambda[ N, state[N], M-1] i.e. the result should be a 2D array, where the second index follows the first one and the third is free (lambda[i,state[i],]). The process is to be repeated so I do not want to write a cycle. Any suggestions? Thanks in advance, Ott Toomet -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Is not lambda[rep(1:N,rep(3,N),state[rep(1:N,rep(3,N)],] ? (i.e., for N=5 , the first index is:> rep(1:5,rep(3,5))[1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 but make sure that state[rep(1:N,rep(3,N)] is within 1:M. Agus Dr. Agustin Lobo Instituto de Ciencias de la Tierra (CSIC) Lluis Sole Sabaris s/n 08028 Barcelona SPAIN tel 34 93409 5410 fax 34 93411 0012 alobo at ija.csic.es On Thu, 20 Sep 2001 siim at obs.ee wrote:> Dear everybody, > > I have a following problem. I have a 3D array > > lambda <- array( dim=c(N,M,M-1)) > > where I have to extract the elements as follows: > > lambda[ 1, state[1], 1] > lambda[ 1, state[1], 2] > ... > lambda[ 1, state[1], M-1] > lambda[ 2, state[2], 1] > ... > lambda[ 2, state[2], M-1] > ... > lambda[ N, state[N], M-1] > > i.e. the result should be a 2D array, where the second index follows the > first one and the third is free (lambda[i,state[i],]). The process is > to be repeated so I do not want to write a cycle. > > Any suggestions? > > Thanks in advance, > > Ott Toomet > > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Although by looping, this small function works with the kind of array you are interested in. Carlos Ortega. ####### Function ####### f<-function(the.matrix) { n<-dim(the.matrix)[1] m<-dim(the.matrix)[2] matrix.result<-matrix(0,nrow=n,ncol=m-1) for(i in 1:n) { for(j in 1:(m-1)) { matrix.result[i,j]<-the.matrix[i,i,j] } } return(matrix.result) } #### Example x<-60 y<-70 matrix.tmp<-array(round(rnorm(x*y*(y-1)),3), c(x,y,y-1)) f(matrix.tmp) ############### -----Mensaje original----- De: siim at obs.ee [mailto:siim at obs.ee] Enviado el: jueves 20 de septiembre de 2001 9:57 Para: r-help at stat.math.ethz.ch Asunto: [R] indexing an array Dear everybody, I have a following problem. I have a 3D array lambda <- array( dim=c(N,M,M-1)) where I have to extract the elements as follows: lambda[ 1, state[1], 1] lambda[ 1, state[1], 2] ... lambda[ 1, state[1], M-1] lambda[ 2, state[2], 1] ... lambda[ 2, state[2], M-1] ... lambda[ N, state[N], M-1] i.e. the result should be a 2D array, where the second index follows the first one and the third is free (lambda[i,state[i],]). The process is to be repeated so I do not want to write a cycle. Any suggestions? Thanks in advance, Ott Toomet -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
siim at obs.ee writes:> Dear everybody, > > I have a following problem. I have a 3D array > > lambda <- array( dim=c(N,M,M-1)) > > where I have to extract the elements as follows: > > lambda[ 1, state[1], 1] > lambda[ 1, state[1], 2] > ... > lambda[ 1, state[1], M-1] > lambda[ 2, state[2], 1] > ... > lambda[ 2, state[2], M-1] > ... > lambda[ N, state[N], M-1] > > i.e. the result should be a 2D array, where the second index follows the > first one and the third is free (lambda[i,state[i],]). The process is > to be repeated so I do not want to write a cycle. > > Any suggestions?How about X <- matrix(,N,M-1) rr <- as.vector(row(M)) cc <- as.vector(col(M)) X[cbind(rr,cc)] <- lambda[cbind(rr, state[rr], cc)] or (faster) rr <- rep(1:N, M - 1) cc <- rep(1:(M - 1), rep(N, M - 1)) X <- matrix(lambda[cbind(rr, state[rr], cc)], N, M - 1) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._