Duplicating an array help [message #90834] |
Fri, 24 April 2015 16:02  |
joellama
Messages: 8 Registered: July 2013
|
Junior Member |
|
|
I'm not entirely sure how best to phrase what I am trying to do but essentially I am trying to find a function that does something like this (but without the loop):
x = findgen(5)
y = x
for i = 0, 100 do y = [y, x]
So I then have an array [0,1,2,3,4,0,1,2,3,4...] 100 times. Is there a way of doing this without the loop?
Thanks!
|
|
|
Re: Duplicating an array help [message #90837 is a reply to message #90834] |
Fri, 24 April 2015 16:20   |
don.woodraska
Messages: 13 Registered: October 2005
|
Junior Member |
|
|
On Friday, April 24, 2015 at 5:02:42 PM UTC-6, Joe Llama wrote:
> I'm not entirely sure how best to phrase what I am trying to do but essentially I am trying to find a function that does something like this (but without the loop):
>
> x = findgen(5)
> y = x
> for i = 0, 100 do y = [y, x]
>
> So I then have an array [0,1,2,3,4,0,1,2,3,4...] 100 times. Is there a way of doing this without the loop?
>
> Thanks!
This would probably work. It's not very readable, but it's only one line. Your code produces a 510 element array, so I used rebin with 102 as the second dimension then flattend it. I don't know how memory efficient this is.
z=(rebin(x,5,102))[*]
You can check if it's the same. I ran your example and calculated the sum of squared differences to be 0.
print,total((z-y)^2)
0.00000
Don
|
|
|
Re: Duplicating an array help [message #90839 is a reply to message #90837] |
Fri, 24 April 2015 16:30   |
wlandsman
Messages: 743 Registered: June 2000
|
Senior Member |
|
|
On Friday, April 24, 2015 at 7:20:51 PM UTC-4, don.wo...@gmail.com wrote:
> On Friday, April 24, 2015 at 5:02:42 PM UTC-6, Joe Llama wrote:
> This would probably work. It's not very readable, but it's only one line. Your code produces a 510 element array, so I used rebin with 102 as the second dimension then flattend it. I don't know how memory efficient this is.
>
> z=(rebin(x,5,102))[*]
Perhaps slightly more readable (or perhaps not) is
z = reform(rebin(x,5,102),510)
It is worth mentioning here one of the classics of the IDL literature, JD Smith's "Dimension Juggling Tutorial":
http://www.idlcoyote.com/tips/rebin_magic.html
|
|
|
|
Re: Duplicating an array help [message #90842 is a reply to message #90834] |
Mon, 27 April 2015 06:41   |
rryan%stsci.edu
Messages: 16 Registered: October 2014
|
Junior Member |
|
|
On Friday, April 24, 2015 at 7:02:42 PM UTC-4, Joe Llama wrote:
> I'm not entirely sure how best to phrase what I am trying to do but essentially I am trying to find a function that does something like this (but without the loop):
>
> x = findgen(5)
> y = x
> for i = 0, 100 do y = [y, x]
>
> So I then have an array [0,1,2,3,4,0,1,2,3,4...] 100 times. Is there a way of doing this without the loop?
>
> Thanks!
I fully endorse the other answers as better than this. But given what yo wrote, you might also consider the "mod" operator:
x= findgen(500) mod 5
-Russell
|
|
|
Re: Duplicating an array help [message #90843 is a reply to message #90842] |
Mon, 27 April 2015 08:10   |
Jeremy Bailin
Messages: 618 Registered: April 2008
|
Senior Member |
|
|
On Monday, April 27, 2015 at 8:41:15 AM UTC-5, rryan%s...@gtempaccount.com wrote:
> On Friday, April 24, 2015 at 7:02:42 PM UTC-4, Joe Llama wrote:
>> I'm not entirely sure how best to phrase what I am trying to do but essentially I am trying to find a function that does something like this (but without the loop):
>>
>> x = findgen(5)
>> y = x
>> for i = 0, 100 do y = [y, x]
>>
>> So I then have an array [0,1,2,3,4,0,1,2,3,4...] 100 times. Is there a way of doing this without the loop?
>>
>> Thanks!
>
> I fully endorse the other answers as better than this. But given what yo wrote, you might also consider the "mod" operator:
>
> x= findgen(500) mod 5
>
> -Russell
You probably really want indgen or lindgen -- taking the modulus of a float isn't a good idea and can cause unwanted results!
-Jeremy.
|
|
|
Re: Duplicating an array help [message #90844 is a reply to message #90843] |
Mon, 27 April 2015 10:04  |
Russell[1]
Messages: 101 Registered: August 2011
|
Senior Member |
|
|
On Monday, April 27, 2015 at 11:10:54 AM UTC-4, Jeremy Bailin wrote:
> On Monday, April 27, 2015 at 8:41:15 AM UTC-5, rryan%s...@gtempaccount.com wrote:
>> On Friday, April 24, 2015 at 7:02:42 PM UTC-4, Joe Llama wrote:
>>> I'm not entirely sure how best to phrase what I am trying to do but essentially I am trying to find a function that does something like this (but without the loop):
>>>
>>> x = findgen(5)
>>> y = x
>>> for i = 0, 100 do y = [y, x]
>>>
>>> So I then have an array [0,1,2,3,4,0,1,2,3,4...] 100 times. Is there a way of doing this without the loop?
>>>
>>> Thanks!
>>
>> I fully endorse the other answers as better than this. But given what yo wrote, you might also consider the "mod" operator:
>>
>> x= findgen(500) mod 5
>>
>> -Russell
>
> You probably really want indgen or lindgen -- taking the modulus of a float isn't a good idea and can cause unwanted results!
>
> -Jeremy.
No, you're right. I was being a bit cavalier and working without actually testing anything.
But one should probably use rebin() for this purpose. I just thought mod was worth mentioning because the problem may need to be generalized to another environment where rebin() isn't an option.
|
|
|