Re: subscripting arrays with dim > 1 [message #12705] |
Fri, 28 August 1998 00:00 |
thompson
Messages: 584 Registered: August 1991
|
Senior Member |
|
|
Gwyn:
I would take the following approach:
IDL> index1 = 0
IDL> index2 = 4
IDL> print,array[index1,index2]
That doesn't seem very elegant, until you start making arrays. For example,
IDL> print,array[0,4],array[3,3]
20 18
IDL> index1=[0,3]
IDL> index2=[4,3]
IDL> print,array[index1,index2]
20 18
You can even make index1 and index2 multidimensional arrays, so long as they
have the same dimensions. Your result should have the same dimensionality as
your index arrays.
Bill Thompson
fireman@aerialist.gsfc.nasa.gov (Gwyn Fireman) writes:
> I'd like to reference an array with a variable containing a subscript for
> each dimension, passing the subscripts as an array. For instance:
> IDL> array = indgen(5,5)
> IDL> print,array
> 0 1 2 3 4
> 5 6 7 8 9
> 10 11 12 13 14
> 15 16 17 18 19
> 20 21 22 23 24
> IDL> print,array[0,4]
> 20
> But I can't seem to reference each dimension. Instead, the subscript array
> is passed as two instances of a single subscript:
> IDL> index=[0,4]
> IDL> print,array[index]
> 0 4
> I can of course calculate a single index, but it seems a bit clunky:
> IDL> s=size(array)
> IDL> print,s
> 2 5 5 2 25
> IDL> print,array[index(0)*s(1)+index(1)*s(2)]
> 20
> - or -
> IDL> print,array[long(total(s(1:s(0))*index))]
> 20
> Is there a better way?
> Gwyn
> --
> __.
> / | , , , , , ,_ Gwyn F. Fireman
> (__/|/(_(_)(_/|/| |_/ General Sciences Corporation
> /| /| Gwyn.Fireman@gsfc.nasa.gov MODIS Characterization Supp. Team
> (_/ (_/ 301-352-2118
|
|
|