repeating elements of an array [message #89347] |
Sun, 24 August 2014 09:42  |
Cleo Drakos
Messages: 1 Registered: August 2014
|
Junior Member |
|
|
Hi guys
First I want to arrange two values (-50 and 40) in the range of 10, so that I get the following array:
a = [-50 -40 -30 -20 -10 0 10 20 30 40]
Now, I want to repeat each element of array 5 times so that I can get the following result:
ans1 = [-50 -50 -50 -50 -50 -40 -40 ....40]
I also want to repeat the whole array 5 times to get the following result:
ans2 = [-50 -40 -30 -20 -10 0 10 20 30 40 -50 -40 -30 -20 -10 0 10 20 30 40 -50 -40 -30 -20 -10 0 10 20 30 40 -50 -40 -30 -20 -10 0 10 20 30 40 -50 -40 -30 -20 -10 0 10 20 30 40 .......]
How can I do it? I hope someone can help me.
cleo
|
|
|
Re: repeating elements of an array [message #89348 is a reply to message #89347] |
Sun, 24 August 2014 13:09   |
dg86
Messages: 118 Registered: September 2012
|
Senior Member |
|
|
On Sunday, August 24, 2014 12:42:53 PM UTC-4, Cleo Drakos wrote:
> Hi guys
>
>
>
> First I want to arrange two values (-50 and 40) in the range of 10, so that I get the following array:
>
>
>
> a = [-50 -40 -30 -20 -10 0 10 20 30 40]
>
> Now, I want to repeat each element of array 5 times so that I can get the following result:
>
>
>
> ans1 = [-50 -50 -50 -50 -50 -40 -40 ....40]
>
>
>
> I also want to repeat the whole array 5 times to get the following result:
>
>
>
> ans2 = [-50 -40 -30 -20 -10 0 10 20 30 40 -50 -40 -30 -20 -10 0 10 20 30 40 -50 -40 -30 -20 -10 0 10 20 30 40 -50 -40 -30 -20 -10 0 10 20 30 40 -50 -40 -30 -20 -10 0 10 20 30 40 .......]
>
>
>
> How can I do it? I hope someone can help me.
>
>
>
> cleo
It sounds like I might be doing your homework, but ...
a = [-50:40:10]
ncopies = 5
nelements = n_elements(a)
npts = ncopies*nelements
ans1 = rebin(a, npts, /sample)
; build up answer by concatenation
ans2 = []
for i = 1, ncopies do ans2 =[ans2, a]
; or else build up answer by copying
ans2 = intarr(npts)
for i = 0, npts-1, nelements do ans2[i] = a
TTFN,
David
|
|
|
Re: repeating elements of an array [message #89349 is a reply to message #89347] |
Sun, 24 August 2014 13:13  |
Phillip Bitzer
Messages: 223 Registered: June 2006
|
Senior Member |
|
|
First, the original array:
IDL> a = findgen(10)*10-50
Now, repeat each element of the array:
IDL> aRepeat= rebin(a, 50, /sample)
Replicating the array is a little more involved, but still pretty straightforward:
IDL> aReplicate = reform(a#replicate(1., 5), 50)
Fun with reform and rebin!
|
|
|