This is kind of a fun-wth-math problem. What you are experiencing is the inability to have two meanings for what this syntax a[[0,2,3],[1,2,4]], so it had to choose just one, and it chose "the union of a[0,1], a[2,3] and a[3,4]". That is what you are observing in the result:
; 6 14 27
The same rules that make 'a[[0,2,3],[1,2,4]]' be interpreted unambiguously as the above allow the following subscripting syntax to be interpreted the way you want:
b = a[ [[0,2,3],[0,2,3],[0,2,3]], [[1,1,1],[2,2,2],[4,4,4]] ]
print, b
; 6 8 9
; 12 14 15
; 24 26 27
The above subscript definition could get very unmanageable, if you had a large array of non-contiguous subscripts that you wanted to subset, but matrix math offers a quick way to dynamically build subscripts like the above. Here is a demonstration with your example:
a = indgen(6,5)
print, a
; 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 25 26 27 28 29
; Now the subscript variables
s1 = [0,2,3] ; column subscripts
s2 = [1,2,4] ; row subscripts
columnSubscripts = s1 # replicate(1, n_elements(s2))
print, columnSubscripts
; 0 2 3
; 0 2 3
; 0 2 3
rowSubscripts = replicate(1, n_elements(s1)) # s2
print, rowSubscripts
; 1 1 1
; 2 2 2
; 4 4 4
b = a[columnSubscripts, rowSubscripts]
print, b
; 6 8 9
; 12 14 15
; 24 26 27
Of course, you do not need all these lines; you could compress this all to:
b = a[s1#replicate(1, n_elements(s2)), replicate(1, n_elements(s1))#s2]
James Jones
|