Re: How to append a multi dimensional array? [message #74246] |
Wed, 05 January 2011 05:47 |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Balt writes:
> a seemingly trivial problem and I can't get my head around it. Does
> anyone see the solution?
>
> Given:
> IDL> x = [1,2,3]
> IDL> y = [4,5,6]
> IDL> bigarray = [[x], [y]]
> IDL> help,bigarray
> BIGARRAY INT = Array[3, 2]
>
> Except, I can't do it like that, because I need to create BIGARRAY
> step by step, i.e. there are steps that require bigarray to already
> exist when y is calculated. And y then needs to be dynamically added
> to bigarray.
>
> How can I add a vector to an array generically?
You might want to have a look at the Dimensional Juggling
and Array Concatenation tutorials:
http://www.dfanning.com/tips/rebin_magic.html
http://www.dfanning.com/tips/array_concatenation.html
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|
|
Re: How to append a multi dimensional array? [message #74248 is a reply to message #74247] |
Wed, 05 January 2011 05:36  |
Balt
Messages: 16 Registered: May 2010
|
Junior Member |
|
|
Thanks Brian. This actually does work when I execute it manually on a
small array:
IDL> array=[1,2,3]
IDL> vector=[4,5,6]
IDL> array = [[array],[reform(vector,3,1)]]
IDL> help,array
ARRAY INT = Array[3, 2]
IDL> print,array
1 2 3
4 5 6
That's exactly what I wanted. However.... when I build it into my
code, it works for the first 2 concatenations, but on the third it
fails, even though the array dimensions are all the same. I'm at a
loss... here's the code, am I missing something?
; now read the baselines
FOR bl = 0, N_ELEMENTS(baselines) - 1 DO BEGIN
READCOL,baselines[bl], ti, pi, FORMAT='D,D', /SILENT
; add a new column to the array for each baseline.
; each baseline data can then be accessed as:
; Phi[*,bl] <= this would return all phase information for baseline
bl
IF bl EQ 0 THEN BEGIN
Phasetime = ti
Phi = pi
ENDIF ELSE BEGIN
Phasetime = [[Phasetime],[reform(ti,N_ELEMENTS(ti),bl)]]
Phi = [[Phi],[reform(pi,N_ELEMENTS(pi),bl)]]
ENDELSE
ENDFOR
It dies with this error when bl = 2:
% REFORM: New subscripts must not change the number elements in TI.
But all the dimensions look identical:
IDL> help,ti
TI DOUBLE = Array[718]
IDL> help,pi
PI DOUBLE = Array[718]
IDL> help,phasetime
PHASETIME DOUBLE = Array[718, 2]
IDL> help,phi
PHI DOUBLE = Array[718, 2]
Confused...
|
|
|
Re: How to append a multi dimensional array? [message #74251 is a reply to message #74248] |
Wed, 05 January 2011 04:04  |
Brian Daniel
Messages: 80 Registered: July 2009
|
Member |
|
|
On Jan 5, 6:01 am, Balt <bindermue...@gmail.com> wrote:
> Hi all,
>
> a seemingly trivial problem and I can't get my head around it. Does
> anyone see the solution?
>
> Given:
> IDL> x = [1,2,3]
> IDL> y = [4,5,6]
> IDL> bigarray = [[x], [y]]
> IDL> help,bigarray
> BIGARRAY INT = Array[3, 2]
>
> Except, I can't do it like that, because I need to create BIGARRAY
> step by step, i.e. there are steps that require bigarray to already
> exist when y is calculated. And y then needs to be dynamically added
> to bigarray.
>
> How can I add a vector to an array generically? In "pseudo code", I
> need to be able to say bigarray[0] = x, then later bigarray[1] = y and
> so forth... all with dynamic (and automatic) resizing of bigarray, of
> course... :-)
>
> I suspect this is really simple and I've done similar things like
> adding a single element to an array dynamically ( array = [[array],
> newval] ) but this doesn't work when newval is a vector!
>
> Any hints greatly appreciated!
>
> - Balt
Part of the problem is how to concatenate arrays. Say your array is N
by M and you wanted to concatenate it with a vector such that it'll be
an N by M+1 array, then:
array = [[array],[reform(vector,N,1)]
Or, if the vector is of length M (column vector), then concatenating
to N+1 by M is:
array = [array,transpose(vector)]
Cheers,
Brian
|
|
|