subscripting arrays with dim > 1 [message #12707] |
Fri, 28 August 1998 00:00  |
fireman
Messages: 49 Registered: August 1991
|
Member |
|
|
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
|
|
|
Re: subscripting arrays [message #46250 is a reply to message #12707] |
Mon, 14 November 2005 01:53  |
wem
Messages: 6 Registered: October 2005
|
Junior Member |
|
|
David Fanning wrote:
> Benjamin Hornberger writes:
>
>
>
>> this is probably simple, but I can't figure it out right now:
>>
>> I want to extract an element from a 2d array, and I have the 2d
>> subscripts of that element available as a 2-element vector.
>>
>> IDL> a = dist(300)
>> IDL> b = [30, 50]
>> IDL> print, a[b]
>> 30.0000 50.0000
>>
>> Not what I want. What I want is
>>
>> IDL> print, a[b[0], b[1]]
>> 58.3095
>>
>> Is there a more elegant way than splitting b up?
>
>
> If by "elegant" you mean "correct", then the answer is no. :-)
>
> Cheers,
>
> David
>
And if you mean by "elegant", "more readable", then the answer is: yes ;-)
IDL> a = dist(300)
IDL> b = [30, 50]
IDL> x = b[0]
IDL> y = b[1]
IDL> print, a[x, y]
and using comments ;-)
|
|
|
Re: subscripting arrays [message #46256 is a reply to message #12707] |
Sun, 13 November 2005 21:07  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Benjamin Hornberger writes:
> this is probably simple, but I can't figure it out right now:
>
> I want to extract an element from a 2d array, and I have the 2d
> subscripts of that element available as a 2-element vector.
>
> IDL> a = dist(300)
> IDL> b = [30, 50]
> IDL> print, a[b]
> 30.0000 50.0000
>
> Not what I want. What I want is
>
> IDL> print, a[b[0], b[1]]
> 58.3095
>
> Is there a more elegant way than splitting b up?
If by "elegant" you mean "correct", then the answer is no. :-)
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|