X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 02 Nov 2007 05:23 PM by  anon
Non-contiguous 2-D array subsetting
 1 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
02 Nov 2007 05:23 PM
    I have an n x m array that I would like to subset using non-contiguous vectors, but IDL seems to only output a 1-D vector. Here is an example to better explain my question. A = indgen(6,5) print, A ; results ; 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 s1 = [0,2,3] s2 = [1,2,4] B = A[s1,s2] print, B ;results in ; 6 14 27 ;when I'm expecting ; 6 8 9 ; 12 14 15 ; 24 26 27 ; If my bands were contiguous I could use C=A[[0,2,3], 1:3] print, C ;results in ; 6 8 9 ; 12 14 15 ; 18 20 21 ; but I unfortunately need to use non-contiguous subscripts Thanks. Chuck

    Deleted User



    New Member


    Posts:
    New Member


    --
    02 Nov 2007 05:23 PM
    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
    You are not authorized to post a reply.