next up previous contents
Next: Array concatenation (from David Up: Appendix II: Other tips Previous: Loops and how to   Contents

Dimensional Juggling (from David Fanning)

Whilst I'm referring to David Fanning's site (there is a lot of IDL information there) have a look at http://www.dfanning.com/tips/rebin_magic.html and

In particular, quoting from the above web-site:

a=indgen(2,3)
print,a
                       ;       0       1
                       ;       2       3
                       ;       4       5
b=randomu(sd,3)
print,b                ;     0.662640     0.991186     0.479801

;We want to multiply each column of "a" by "b". 
;If we had a new array, "b2", the same size as "a", but with a copy of
;"b" in  all its columns, we could perform the multiplication
;trivially. 

;TRY:
print,rebin(b,2,3)
;ERROR: % REBIN: Result dimensions must be integer factor of original dimensions

;USE:
print, rebin(reform(b,1,3),2,3)
                      ;     0.662640     0.662640
                      ;     0.991186     0.991186
                      ;     0.479801     0.479801

;WE  WANT:
print, rebin(reform(b,1,3),2,3)*a
                      ;      0.00000     0.662640
                      ;      1.98237      2.97356
                      ;      1.91920      2.39901


;N.B. There are other ways to make "column vectors". Examples:

help,transpose(b)     ;<Expression>    FLOAT     = Array[1, 3]

help,rotate(b,1)      ;<Expression>    FLOAT     = Array[1, 3]
1#b
b##1
You'll often see these in place of reform(b,1,3), but don't be confused, they do exactly the same thing: prepend a leading shallow dimension. They are nice because they relieve you from having to know they length of b (3 here). However, they only work for creating column vectors, though: i.e. up to 2D data, and the latter two only work for numeric data.



John Marsham 2009-12-07