[Matrix-SIG] Creative slicing

Janko Hauser jhauser@ifm.uni-kiel.de
Thu, 9 Sep 1999 09:11:19 +0200 (CEST)


Try this code. It is not general for the number of dimensions, but
general for the length of the dimensions. Perhaps it is a start...

HTH,

__Janko

####### cut here
from Numeric import *

x=arange(10*4*5)
x.shape = (10,4,5) 
y = ones((4,5))*2
y[0,0]=3
y[0,1]=4
y[1,0]=5
y.shape = (4,5)   # filled with ints in the 0-9 range 
 
# These lines need to be adapted for more dimensions
yi,xi=indices(y.shape)
linindex = y*product(x.shape[1:])+yi*y.shape[-2]+xi

z=take(ravel(x),ravel(linindex))
z.shape=(4,5)