# [R] indexing an array

Ortega Fernandez, Carlos (carlos) cof at agere.com
Thu Sep 20 14:37:59 CEST 2001

```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?

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

```