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 
Switch to threaded view of this topic Create a new topic Submit Reply
Concatenating arrays across chosen dimension [message #31266] Tue, 25 June 2002 15:05 Go to next 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
Re: Concatenating arrays [message #45366 is a reply to message #31266] Mon, 05 September 2005 07:08 Go to previous message
K. Bowman is currently offline  K. Bowman
Messages: 330
Registered: May 2000
Senior Member
In article <431af10e@quokka.wn.com.au>, "RPF" <RPF@lol.com> wrote:

> Also is there an easy way to
> make an upper triangular matrix i.e. make all entries below the diagonal
> zero ?

IDL> n = 5
IDL> i = REBIN(LINDGEN(n), n, n)
IDL> j = REBIN(TRANSPOSE(LINDGEN(n)), n, n)
IDL> print, i
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
IDL> print, j
0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
IDL> a = (i GE j)
IDL> print, a
1 1 1 1 1
0 1 1 1 1
0 0 1 1 1
0 0 0 1 1
0 0 0 0 1


Cheers, Ken Bowman
Re: Concatenating arrays [message #45372 is a reply to message #31266] Sun, 04 September 2005 12:11 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
RPF writes:

> me...
> If I have a = [2 3] and i wish to repeat these two elements for say 1000
> times so my final array is 2000 elements long is there a quick way?

You want to read the Dimensional Juggling Tutorial:

http://www.dfanning.com/documents/tips.html#Tutorials

Try this:

a = [2, 3]
b = Reform(Rebin(a, 2, 1000), 2000)


> Also is there an easy way to make an upper triangular matrix i.e.
> make all entries below the diagonal zero ?

Probably. There always is. But the exactly method eludes me at
the moment. :-)

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Re: Concatenating arrays [message #45373 is a reply to message #31266] Sun, 04 September 2005 12:06 Go to previous message
Wonko[3] is currently offline  Wonko[3]
Messages: 9
Registered: February 2003
Junior Member
RPF@lol.com (RPF) wrote:

> If I have a = [2 3] and i wish to repeat these two elements for say 1000
> times so my final array is 2000 elements long is there a quick way?

Have a look at REPLICATE().

> Also is there an easy way to make an upper triangular matrix i.e.
> make all entries below the diagonal zero ?

I think CHOLDC does this.

Alex
--
Alex Schuster Wonko@wonkology.org
alex@pet.mpin-koeln.mpg.de
  Switch to threaded view of this topic Create a new topic Submit Reply
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: Fri Oct 10 12:47:48 PDT 2025

Total time taken to generate the page: 1.21855 seconds