Re: Matrix indexing question [message #38939] |
Sat, 03 April 2004 02:26 |
Chris Lee
Messages: 101 Registered: August 2003
|
Senior Member |
|
|
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.
|
|
|
Re: Matrix indexing question [message #38941 is a reply to message #38939] |
Fri, 02 April 2004 16:47  |
R.Bauer
Messages: 1424 Registered: November 1998
|
Senior Member |
|
|
Dear Matt
what do you think on this solution
a=[3,4]
b=[1,0]
x = [[1,2,3,4,5,6],[7,8,9,10,11,12]]
print,x[[a],[b]]
10 5
My explanation:
The problem here is that there are the same signs used to create an array
and the same signs are used to assign an index array.
May be David has a better idea. I hink what happens is by using an operation
you did by {{{ print, x[[3,4],[1,0]] }}} the outer [] are used to adress
the values because then they are not interpreted to create a new array.
If you do create a temporary array like this example shows. It is not
necessary to create the {{{y}}} variable. You will get the results you see
in your example too.
print, (x)([[3,4],[1,0]])
4 5
2 1
Both could be wanted and both could give terrible results if they are not
clear described or coded.
Thanks for the advice!
Reimar
Matt Feinstein 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.
--
Forschungszentrum Juelich
email: R.Bauer@fz-juelich.de
http://www.fz-juelich.de/icg/icg-i/
============================================================ ======
a IDL library at ForschungsZentrum Juelich
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_lib_intro. html
|
|
|