comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Concatenating arrays across chosen dimension
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Concatenating arrays across chosen dimension [message #31266] Tue, 25 June 2002 15:05 Go to previous message
Dick Jackson is currently offline  Dick Jackson
Messages: 347
Registered: August 1998
Senior Member
"Randall Skelton" <rhskelto@atm.ox.ac.uk> wrote in message
news:Pine.LNX.4.33.0206251749570.28170-100000@mulligan.atm.o x.ac.uk...

> Ok... I have to ask. Is there actually a nice, clean way to concatenate
> multidimensional arrays in IDL?
>
> a = make_array(2,2,2,2)
> b = make_array(2,2,2,5)
>
> data1 = [ [[[a]]] , [[[b]]] ]
>
> Obviously the above fails, but what is the solution? Surely some
> combination of rebin/reform...

Well, I have to say I don't know *why* that one fails, since this works
fine:

IDL> a = make_array(2,2,2)
IDL> b = make_array(2,2,5)
IDL> help, [ [[a]], [[b]] ]
<Expression> FLOAT = Array[2, 2, 7]

... and we're a long way from the 8-dimension limit on arrays.

In any case, to concatenate on the *last* dimension in this case:

c = Reform([ a[*], b[*] ], [2,2,2,7])

To do this in general takes a little more hacking, dimension juggling in
particular. I couldn't resist the challenge, so I whipped up the attached
Concat.pro.

Examples:
IDL> Help, Concat(LIndGen(2,3,4), -LIndGen(5,3,4), Dim=0)
<Expression> LONG = Array[7, 3, 4]
IDL> Help, Concat(LIndGen(2,3,4), -LIndGen(2,5,4), Dim=1)
<Expression> LONG = Array[2, 8, 4]
IDL> Help, Concat(LIndGen(2,3,4), -LIndGen(2,3,5)) ; assumes last dimension
<Expression> LONG = Array[2, 3, 9]


Code is copied here for convenience. Any comments or corrections are
welcome!

=====

FUNCTION Concat, $ ; Return concatenation of two arrays
a, $ ; First array
b, $ ; Second array
Dimension=dim ; Dimension along which to
concatenate
; (counting from 0, defaults to
*last*
; dimension of arrays)
;; Examples:
;; IDL> Help, Concat(LIndGen(2,3,4), -LIndGen(5,3,4), Dim=0)
;; <Expression> LONG = Array[7, 3, 4]
;; IDL> Help, Concat(LIndGen(2,3,4), -LIndGen(2,5,4), Dim=1)
;; <Expression> LONG = Array[2, 8, 4]
;; IDL> Help, Concat(LIndGen(2,3,4), -LIndGen(2,3,5))
;; <Expression> LONG = Array[2, 3, 9]
;; (use Print with these to see actual results)

;; Assuming here that a and b are of same dimensions except perhaps for
;; dimension 'dim', which is assumed to be within range.
;; Testing and error handling for this is left as an exercise for the
;; reader. :-)

nDims = Size(a, /N_Dimensions)
IF N_Elements(dim) EQ 0 THEN dim = nDims-1

aDims = Size(a, /Dimensions)
bDims = Size(b, /Dimensions)

;; Figure out desired dimensions of result

resultDims = aDims
resultDims[dim] = aDims[dim]+bDims[dim]

;; Make a vector of dimension indices with concatenation dimension *last*

transposeDimOrder = [Where(IndGen(nDims) NE dim), dim]

;; Juggle dimensions by transposing a and b to put desired concatenation
;; dimension last, then take all elements together into one vector

joinedVector = [(Transpose(a, transposeDimOrder))[*], $
(Transpose(b, transposeDimOrder))[*]]

;; Reform the vector to an array of the right size with juggled
dimensions

juggledResult = Reform(Temporary(joinedVector),
resultDims[transposeDimOrder])

;; Un-juggle the dimensions to give final result

Return, Transpose(Temporary(juggledResult), Sort(transposeDimOrder))

END

=====

Cheers,
--
-Dick

Dick Jackson / dick@d-jackson.com
D-Jackson Software Consulting / http://www.d-jackson.com
Calgary, Alberta, Canada / +1-403-242-7398 / Fax: 241-7392
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: constraining parameters in multi-Gaussian 1D fitting
Next Topic: Re: constraining parameters in multi-Gaussian 1D fitting

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Sun Oct 12 16:32:49 PDT 2025

Total time taken to generate the page: 0.00217 seconds