Re: Variable stride in array indices [message #15248 is a reply to message #15234] |
Fri, 07 May 1999 00:00  |
Liam Gumley
Messages: 473 Registered: November 1994
|
Senior Member |
|
|
Kenneth P. Bowman wrote:
> We haven't had a good argument about IDL syntax lately, so I thought I
> would ask why IDL does not allow variable stride in array indices. For
> example, if
>
> zz = z[0:5:2]
>
> then zz would contain [z[0], z[2], z[4]].
>
> I realize that this kind of thing can be done with array subscript lists
> such as z[[0, 2, 4]], but this can be awkward for multidimensional
> arrays. It is also necessary to *store* the lists of subscripts, and I
> believe that implementation through indirect subscripts must be slower
> than implementing strides through direct iteration (i.e., internal
> compiled DO loops).
In fact we had a good argument recently here at SSEC about indexing 2D
arrays with variable stride. The argument was this: If I have an array
a, and an index array x, why doesn't a[x,x] extract a 2D subsampled
array? (it returns a 1D array).
Here's what we resolved:
Say I want to extract every 2nd row and column of an array:
IDL> a=indgen(10,10)
First, I create the index array:
IDL> x=indgen(5)*2
IDL> print,x
0 2 4 6 8
I can select the columns:
IDL> print,a[x,*]
0 2 4 6 8
10 12 14 16 18
20 22 24 26 28
30 32 34 36 38
40 42 44 46 48
50 52 54 56 58
60 62 64 66 68
70 72 74 76 78
80 82 84 86 88
90 92 94 96 98
Or I can select the columns and rows at the same time:
IDL> print,(a[x,*])[*,x]
0 2 4 6 8
20 22 24 26 28
40 42 44 46 48
60 62 64 66 68
80 82 84 86 88
This is equivalent to the following
IDL> a=a[x,*]
IDL> print,a
0 2 4 6 8
10 12 14 16 18
20 22 24 26 28
30 32 34 36 38
40 42 44 46 48
50 52 54 56 58
60 62 64 66 68
70 72 74 76 78
80 82 84 86 88
90 92 94 96 98
IDL> a=a[*,x]
IDL> print,a
0 2 4 6 8
20 22 24 26 28
40 42 44 46 48
60 62 64 66 68
80 82 84 86 88
Cheers,
Liam.
---
Liam E. Gumley
Space Science and Engineering Center, UW-Madison
http://cimss.ssec.wisc.edu/~gumley
|
|
|