|
Re: 'Replicat' a string array [message #86972 is a reply to message #86971] |
Wed, 18 December 2013 06:02   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
zhangj.sdu@gmail.com writes:
> I have a string array representing date/time, for example, ['2008-
07-25T20:56', '2008-07-25T21:32']. Any idea to 'replicate' it into a
2x100 array in a way similar to that described in the 'Dimensional
Juggling Tutorial' (http://www.idlcoyote.com/tips/rebin_magic.html)?
Maybe something like this:
IDL> a = '2008-07-25T20:56' & b = '2008-07-25T21:32'
IDL> c = [Reform(Replicate(a, 20), 1, 20), $
Reform(Replicate(b,20), 1, 20)]
IDL> help, c
C STRING = Array[2, 20]
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
Re: 'Replicat' a string array [message #86974 is a reply to message #86972] |
Wed, 18 December 2013 07:24   |
Heinz Stege
Messages: 189 Registered: January 2003
|
Senior Member |
|
|
On Wed, 18 Dec 2013 07:02:27 -0700, David Fanning wrote:
> Maybe something like this:
>
> IDL> a = '2008-07-25T20:56' & b = '2008-07-25T21:32'
> IDL> c = [Reform(Replicate(a, 20), 1, 20), $
> Reform(Replicate(b,20), 1, 20)]
> IDL> help, c
> C STRING = Array[2, 20]
>
or this:
IDL> a=['2008-07-25T20:56', '2008-07-25T21:32']
IDL> c=a[*,intarr(100)]
IDL> help,c
C STRING = Array[2, 100]
Cheers, Heinz
|
|
|
Re: 'Replicat' a string array [message #86977 is a reply to message #86974] |
Wed, 18 December 2013 10:08   |
Dick Jackson
Messages: 347 Registered: August 1998
|
Senior Member |
|
|
On Wednesday, December 18, 2013 7:24:57 AM UTC-8, Heinz Stege wrote:
> On Wed, 18 Dec 2013 07:02:27 -0700, David Fanning wrote:
>
>
>
>> Maybe something like this:
>
>>
>
>> IDL> a = '2008-07-25T20:56' & b = '2008-07-25T21:32'
>
>> IDL> c = [Reform(Replicate(a, 20), 1, 20), $
>
>> Reform(Replicate(b,20), 1, 20)]
>
>> IDL> help, c
>
>> C STRING = Array[2, 20]
>
>>
>
> or this:
>
> IDL> a=['2008-07-25T20:56', '2008-07-25T21:32']
>
> IDL> c=a[*,intarr(100)]
>
> IDL> help,c
>
> C STRING = Array[2, 100]
>
>
>
> Cheers, Heinz
or to add a simplifying footnote to David's solution:
IDL> a = '2008-07-25T20:56' & b = '2008-07-25T21:32'
IDL> c = [Replicate(a, [1, 20]), Replicate(b, [1, 20])]
IDL> help, c
C STRING = Array[2, 20]
Cheers,
-Dick
Dick Jackson Software Consulting
Victoria, BC, Canada --- www.d-jackson.com
|
|
|
Re: 'Replicat' a string array [message #86984 is a reply to message #86974] |
Wed, 18 December 2013 17:26   |
zhangj.sdu
Messages: 6 Registered: December 2013
|
Junior Member |
|
|
On Wednesday, December 18, 2013 11:24:57 PM UTC+8, Heinz Stege wrote:
> On Wed, 18 Dec 2013 07:02:27 -0700, David Fanning wrote:
>
>
>
>> Maybe something like this:
>
>>
>
>> IDL> a = '2008-07-25T20:56' & b = '2008-07-25T21:32'
>
>> IDL> c = [Reform(Replicate(a, 20), 1, 20), $
>
>> Reform(Replicate(b,20), 1, 20)]
>
>> IDL> help, c
>
>> C STRING = Array[2, 20]
>
>>
>
> or this:
>
> IDL> a=['2008-07-25T20:56', '2008-07-25T21:32']
>
> IDL> c=a[*,intarr(100)]
>
> IDL> help,c
>
> C STRING = Array[2, 100]
>
>
>
> Cheers, Heinz
Perfect solution! Thanks, Heinz.
Clark
|
|
|
Re: 'Replicat' a string array [message #86989 is a reply to message #86974] |
Thu, 19 December 2013 05:25   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Heinz Stege writes:
>
> On Wed, 18 Dec 2013 07:02:27 -0700, David Fanning wrote:
>
>> Maybe something like this:
>>
>> IDL> a = '2008-07-25T20:56' & b = '2008-07-25T21:32'
>> IDL> c = [Reform(Replicate(a, 20), 1, 20), $
>> Reform(Replicate(b,20), 1, 20)]
>> IDL> help, c
>> C STRING = Array[2, 20]
>>
> or this:
> IDL> a=['2008-07-25T20:56', '2008-07-25T21:32']
> IDL> c=a[*,intarr(100)]
> IDL> help,c
> C STRING = Array[2, 100]
OK, I just got back from cutting trees. How in God's name does *this*
work, and how did you ever stumble onto it? Strangest thing I have ever
seen in IDL, and I've seen a LOT of strange things!
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
Re: 'Replicat' a string array [message #87001 is a reply to message #86989] |
Thu, 19 December 2013 07:40   |
Heinz Stege
Messages: 189 Registered: January 2003
|
Senior Member |
|
|
On Thu, 19 Dec 2013 06:25:20 -0700, David Fanning wrote:
> Heinz Stege writes:
>
>>
>> On Wed, 18 Dec 2013 07:02:27 -0700, David Fanning wrote:
>>
>>> Maybe something like this:
>>>
>>> IDL> a = '2008-07-25T20:56' & b = '2008-07-25T21:32'
>>> IDL> c = [Reform(Replicate(a, 20), 1, 20), $
>>> Reform(Replicate(b,20), 1, 20)]
>>> IDL> help, c
>>> C STRING = Array[2, 20]
>>>
>> or this:
>> IDL> a=['2008-07-25T20:56', '2008-07-25T21:32']
>> IDL> c=a[*,intarr(100)]
>> IDL> help,c
>> C STRING = Array[2, 100]
>
> OK, I just got back from cutting trees. How in God's name does *this*
> work,
I think the explanation can be found in the combination of two
features from the IDL language. The first is documented in the chapter
"Using Arrays as Array Subscripts" of the IDL Manual. You can write
y=x[[i,j]]
to get the i-th and the j-th element of the array x. No one can stop
you to write
y=x[[i,i]]
to get the i-th element doubly. And, because [i,i] is nothing else
than an array, you can write
y=x[intarr(n)+i]
to get the i-th element n times. For i=0 it is
y=x[intarr(n)]
The second feature is the use of "Extra dimensions". (See
"Understanding Array Subscripts" in the manual.) In my words every IDL
array has 8 dimensions. All trailing dimensions of size 1 are
automatically removed by IDL, but can be referenced by "0" or "*". You
may write
c=a[*,0] or c=a[*,0,0,0,0,0,0,0]
to reproduce the string array of the OP.
Combining both features results in
c=a[*,intarr(n)]
That's it.
By the way, it saves some memory using
c=a[*,bytarr(n)]
instead of the expression above. I was careless when quick replying to
the posting.
Another note: The above approach is reasonable for string arrays only.
For integer and floating point arrays it is more useful to use the
rebin function. See message ID b073e6$5p5$1@newsreader.wustl.edu (or
https://groups.google.com/forum/?hl=en#!topic/comp.lang.idl- pvwave/VWBB8kSs0HU
for google users) for an earlier discussion within this group. The
rebin function saves memory and CPU time for very large integer or
float arrays. However, rebin does not work for string arrays.
> and how did you ever stumble onto it?
Good question. I think, when I started working with PV-Wave more than
20 years ago, I didn't realize the benefit of the rebin function and
found the solution above...
> Strangest thing I have ever
> seen in IDL, and I've seen a LOT of strange things!
Thank you.
Cheers, Heinz
|
|
|
Re: 'Replicat' a string array [message #87004 is a reply to message #87001] |
Thu, 19 December 2013 08:23  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Heinz Stege writes:
> Good question. I think, when I started working with PV-Wave more than
> 20 years ago, I didn't realize the benefit of the rebin function and
> found the solution above...
Well past time you were invited to join the IDL Expert Programmer's
Association, Heinz. I'll see to it your invitation is in the mail today!
http://www.idlcoyote.com/misc_tips/iepa.php
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|