Re: array concatenation in 2-D [message #59622] |
Fri, 04 April 2008 14:25  |
ben.bighair
Messages: 221 Registered: April 2007
|
Senior Member |
|
|
On Apr 4, 3:31 pm, Jean H <jghas...@DELTHIS.ucalgary.ANDTHIS.ca>
wrote:
> elwood wrote:
>> I have a loop which calculates two variables x[i] and y[i]
>
>> At each iteration of the loop I calculate
>> x and y
>> And I'd like to concatenate x and y into a 2 column, unknown numbers
>> of rows
>> output array.
>> I'd like to dynamically grow the output array at each interation.
>
>> For example:
>> x=1 y=5 on first iteration
>> x=2, y=6 on 2nd iteration
>> I want an output array that looks like the below:
>
>> 1 5
>> 2 6
>
>> How do i achieve this without knowing the array size??
>
>> Tx!
>> -Elisha
>
> Elisha,
> You can concatenate the arrays, as you suggest:
>
> a = [[1,2],[3,4]]
> a = [[a],[5,6]]
> help,a
> ==>A INT = Array[2, 3]
>
> However, if you have many elements, this can be very resources
> consuming. Another option is to create a "big" 2*n array, to
> progressively fill it, to keep a counter on the number of entries, and
> finally to cut what you haven't used.... and similarly, if your array is
> not big enough, add a large number of rows and keep filling them (use
> the same concatenation method as above)
>
Hi,
As a second to the method jean suggests, you might want to consider
using Mike Galloy's collections objects which are efficient resizable
buckets for holding data. Check it out at ...
http://michaelgalloy.com/2006/04/24/collection-package-mgarr aylist.html
Cheers,
Ben
|
|
|
Re: array concatenation in 2-D [message #59623 is a reply to message #59622] |
Fri, 04 April 2008 12:31   |
Jean H.
Messages: 472 Registered: July 2006
|
Senior Member |
|
|
elwood wrote:
> I have a loop which calculates two variables x[i] and y[i]
>
> At each iteration of the loop I calculate
> x and y
> And I'd like to concatenate x and y into a 2 column, unknown numbers
> of rows
> output array.
> I'd like to dynamically grow the output array at each interation.
>
> For example:
> x=1 y=5 on first iteration
> x=2, y=6 on 2nd iteration
> I want an output array that looks like the below:
>
> 1 5
> 2 6
>
> How do i achieve this without knowing the array size??
>
>
> Tx!
> -Elisha
Elisha,
You can concatenate the arrays, as you suggest:
a = [[1,2],[3,4]]
a = [[a],[5,6]]
help,a
==>A INT = Array[2, 3]
However, if you have many elements, this can be very resources
consuming. Another option is to create a "big" 2*n array, to
progressively fill it, to keep a counter on the number of entries, and
finally to cut what you haven't used.... and similarly, if your array is
not big enough, add a large number of rows and keep filling them (use
the same concatenation method as above)
Jean
|
|
|
Re: array concatenation in 2-D [message #59624 is a reply to message #59623] |
Fri, 04 April 2008 12:40   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
R.G. Stockwell writes:
> If it is always 2 columns by whatever rows, then just concatenate the
> arrays.
> (you may have to do some tranposes)
>
> If there is a variable number of columns, then as David says, pointers.
Oh, here (for those you don't have time to read
the Dimensional Juggling and Pointer tutorials):
PRO Example
arrayptr = Ptr_New()
seed = -3L
FOR j=0,9 DO BEGIN
points = Randomu(seed, 2)
Print, 'Points: ', points
IF j EQ 0 THEN $
arrayptr = Ptr_New(points) ELSE $
*arrayptr = [[Temporary(*arrayptr)],[points]]
ENDFOR
Print, 'Print Values in Pointer'
Print, *arrayPtr
Ptr_Free, arrayPtr
END
Results in:
Points: 0.897916 0.558249
Points: 0.766930 0.589101
Points: 0.0603181 0.973112
Points: 0.0378892 0.218058
Points: 0.142394 0.984703
Points: 0.894904 0.947651
Points: 0.804079 0.160385
Points: 0.208246 0.818130
Points: 0.103716 0.741117
Points: 0.0134482 0.0960160
Print Values in Pointer
0.897916 0.558249
0.766930 0.589101
0.0603181 0.973112
0.0378892 0.218058
0.142394 0.984703
0.894904 0.947651
0.804079 0.160385
0.208246 0.818130
0.103716 0.741117
0.0134482 0.0960160
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming (www.dfanning.com)
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|
|
|
Re: array concatenation in 2-D [message #59754 is a reply to message #59623] |
Tue, 08 April 2008 18:19  |
elwood
Messages: 23 Registered: February 2007
|
Junior Member |
|
|
On Apr 4, 2:31 pm, Jean H <jghas...@DELTHIS.ucalgary.ANDTHIS.ca>
wrote:
> elwood wrote:
>> I have a loop which calculates two variables x[i] and y[i]
>
>> At each iteration of the loop I calculate
>> x and y
>> And I'd like to concatenate x and y into a 2 column, unknown numbers
>> of rows
>> output array.
>> I'd like to dynamically grow the output array at each interation.
>
>> For example:
>> x=1 y=5 on first iteration
>> x=2, y=6 on 2nd iteration
>> I want an output array that looks like the below:
>
>> 1 5
>> 2 6
>
>> How do i achieve this without knowing the array size??
>
>> Tx!
>> -Elisha
>
> Elisha,
> You can concatenate the arrays, as you suggest:
>
> a = [[1,2],[3,4]]
> a = [[a],[5,6]]
> help,a
> ==>A INT = Array[2, 3]
>
> However, if you have many elements, this can be very resources
> consuming. Another option is to create a "big" 2*n array, to
> progressively fill it, to keep a counter on the number of entries, and
> finally to cut what you haven't used.... and similarly, if your array is
> not big enough, add a large number of rows and keep filling them (use
> the same concatenation method as above)
>
> Jean
Alas, this is a form of concatenation, but it does not produce
the required results.
I need to concatenate by COLUMN, not row.
If I code the concatenation you show, it produces:
column 1:
1 2
3 4
5 6
Whereas I need it to paste the columns together such that I
get
1 3 5
2 4 6
To be specific, each iteration of the loop
I calculate new values of x and y
I want to do the following, but using concatenation
outputarry[0,0]=x1
outputarry[1,0]=y1
next iteration
outputarry[0,1]=x2
outputarry[1,1]=y2
to get a final array where x values are in column 0
y values are in column 1
|
|
|