In article <gm1r60p1cdu66r7h8b1bornsh99m1aa05d@4ax.com>, "Matt Feinstein"
<nospam@here.com> wrote:
> If I set
>
> x = [[1,2,3,4,5,6],[7,8,9,10,11,12]]
> then (case A)
> print, x[[3,4],[1,0]]
> gives
> 10 5
> which is slick, and is the kind of indexing I want. However, if (Case B)
> I set
> y = [[3,4],[1,0]]
> then
> print, x[y]
> gives
>
> 4 5
> 2 1
> which, I guess, is also slick-- but is not what I want. Is there any way
> to set a variable 'y' that will give me the kind of indexing in Case A?
> And, yes, I know that I can set
> y = [9, 4]
> and get the 'right' answer. Is this the only way? Matt Feinstein
> --
> There is no virtue in believing something that can be proved to be true.
Hi Matt,
I have two answers, neither is particularly clean.
;1
print, x[y[*,0], y[*,1]]
10 5
;2 , the generic,Index the elements yourself, method.
x=[[1,2,3,4,5,6],[7,8,9,10,11]]
y=[[3,4],[1,0]]
;;;;;
dx=size(x, /dimensions)
dy=size(y,/dimensions)
q=replicate(1,n_elements(dx))
for i=1, n_elements(dx)-1 do for j=0, i-1 do q[i]=q[i]*dx[j]
;;q holds the number of elements per dimension.
indices=y#q ; 2 dimensions in y only
print, x[indices]
10 5
Also, some bounds checking might be needed.
Chris.
|