[R] Index matrix to pick elements from 3-dimensional matrix

juhana vartiainen j.vartiainen at fief.se
Tue Apr 26 06:16:38 CEST 2005


Hi all

Suppose I have a dim=c(2,2,3) matrix A, say:

A[,,1]=
a b
c d

A[,,2]=
e f
g h

A[,,3]=
i j
k l

Suppose that I want to create a 2x2 matrix X, which picks elements from 
the above-mentioned submatrices according to an index matrix J referring 
to the "depth" dimension:
J=
1 3
2 3

In other words, I want X to be
X=
a j
g l

since the matrix J says that the (1,1)-element should be picked from 
A[,,1], the (1,2)-element should be picked from A[,,3], etc.

I have A and I have J. Is there an expression in A and J that creates X?

Thanks

Juhana

juhana at fief.se

-- 
Juhana Vartiainen

docent in economics
Director, FIEF (Trade Union Foundation for Economic Research, Stockholm), http://www.fief.se
gsm +46 70 360 9915
office +46 8 696 9915
email juhana at fief.se
homepage http://www.fief.se/staff/Juhana/index.html




More information about the R-help mailing list