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

Home » Public Forums » archive » REPLICATE with arrays
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
REPLICATE with arrays [message #18899] Fri, 11 February 2000 00:00 Go to next message
Vince Hradil is currently offline  Vince Hradil
Messages: 574
Registered: December 1999
Senior Member
I often have the need to replicate an array, but IDL's replicate only
works with scalars. Does anyone have any tips on the most efficient,
simplest, clearest (you choose) way to do this?

e.g.

I have:
help, x
INT = Array[3, 3]
print, x
2 4 10
3 7 5
3 9 2

and would like to do:
x2 = replicate(x,2)
help, x2
INT = Array[3, 3, 2]
print, x2
2 4 10
3 7 5
3 9 2

2 4 10
3 7 5
3 9 2

I've figured out some trick for 1 and 2 dimensional arrays, but I'm
looking for a more general strategy to use on higher dim arrays.

Thanks,

Vince
Re: REPLICATE with arrays [message #18960 is a reply to message #18899] Tue, 15 February 2000 00:00 Go to previous message
Jeremy Faden is currently offline  Jeremy Faden
Messages: 2
Registered: February 2000
Junior Member
Vince Hradil wrote:

> I often have the need to replicate an array, but IDL's replicate only
> works with scalars. Does anyone have any tips on the most efficient,
> simplest, clearest (you choose) way to do this?
>
> e.g.
>
> I have:
> help, x
> INT = Array[3, 3]
> print, x
> 2 4 10
> 3 7 5
> 3 9 2
>
> and would like to do:
> x2 = replicate(x,2)
> help, x2
> INT = Array[3, 3, 2]
> print, x2
> 2 4 10
> 3 7 5
> 3 9 2
>
> 2 4 10
> 3 7 5
> 3 9 2
>
> I've figured out some trick for 1 and 2 dimensional arrays, but I'm
> looking for a more general strategy to use on higher dim arrays.
>
> Thanks,
>
> Vince

Vince,
Here is a somewhat simpler solution which works at least
for your example. It should also handle arrays of any dimension.
Jeremy

------ begin code -------
function replicate_array, A, n_rep
;+
; NAME: replicate_array
; PURPOSE: replicates an array into many copies.
; CALLING SEQUENCE: AAA= replicate_array( A,n_copy )
; INPUTS:
; A, an array of any dimension. Cannot be a scalar.
; n_copy, the number of copies to make.
; OUTPUTS:
; AAA, an array with the same dimensions of A, and an additional
; dimension that indexes the copy number.
; RESTRICTIONS: If input array is an int, a long is returned.
Otherwise, type
; is preserved. Input array can not be a scalar. Input type must
; be a number (i.e. int, long, float, double, complex).
; EXAMPLE:
; x= [[2,4,10],[3,7,5],[3,9,2]]
; print, x
; x2= replicate_array( x, 2 )
; help, x2
; print, x2
;
; MODIFICATION HISTORY:
; written, Jeremy Faden, University of Iowa, February 15, 2000.
;-

new_dim= [ size(A,/dimensions), n_rep ]
new_star= A(*) # make_array(value=1,n_rep)
return, reform( new_star,new_dim )
end
------- end code ---------

Jeremy Faden
University of Iowa
jbf@supras.physics.uiowa.edu
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Barcodes
Next Topic: Re: REQ: alternative plot procedure?

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

Current Time: Fri Oct 10 01:38:09 PDT 2025

Total time taken to generate the page: 2.71898 seconds