Re: Getting rid of loops [message #18926] |
Fri, 18 February 2000 00:00 |
Michael Asten
Messages: 53 Registered: March 1999
|
Member |
|
|
Try this.
Good luck,
Michael Asten
function fn,cycles
cycles_and=[cycles,max(cycles)]
cycles_shift=[cycles[0],cycles]
print , cycles_and
print , cycles_shift
b=where(cycles gt cycles_shift)
sub_cycles=[ cycles_shift[b[0]], cycles_and[b] ]
return,sub_cycles
end
cycles=[2,2,2,2,2,6,6,7,7,7,9,9,9,27,27,27]
print,fn(cycles)
;[2,6,7,9,27]>
end
Jacob Noel-Storr wrote:
> I am trying to create non-loopy IDL code for a project I am working on.
>
> I have a very long list of integers ('cycle numbers') and I need some way of
> creating an array that consists of just the cycle numbers. For example if I
> had:
>
>> cycles=[2,2,2,2,2,6,6,7,7,7,9,9,9,27,27,27]
>
> then I would want a function which would do this kid of thing:
>
>> print,fn(cycles)
> [2,6,7,9,27]
>
> At the moment this is done by looping through the data twice. Once to count
> how many different values there are and once to write the different values
> into an array.
>
> Any suggestions about how this could be achieved without the two 'for'
> loops?
>
> Thanks,
> Jacob
>
> --
> Jacob Noel-Storr
> Astronomy Department
> Columbia University / Biosphere 2 Center
> www.astro.bio2.edu/jacob/
> jake@astro.columbia.edu
|
|
|
Re: Getting rid of loops [message #18927 is a reply to message #18926] |
Fri, 18 February 2000 00:00  |
Mark Hadfield
Messages: 783 Registered: May 1995
|
Senior Member |
|
|
Jacob Noel-Storr <jake@astro.columbia.edu> wrote in message
news:88i60t$477$1@newsmaster.cc.columbia.edu...
> ..
> I have a very long list of integers ('cycle numbers') and I need some way
of
> creating an array that consists of just the cycle numbers. For example if
I
> had:
>
>> cycles=[2,2,2,2,2,6,6,7,7,7,9,9,9,27,27,27]
>
> then I would want a function which would do this kid of thing:
>
>> print,fn(cycles)
> [2,6,7,9,27]
The UNIQ function, along with the SORT function, will do this:
print, UNIQ(cycles, SORT(cycles))
or if you know that your array is already monotonic, just:
print, UNIQ(cycles)
---
Mark Hadfield
m.hadfield@niwa.cri.nz http://katipo.niwa.cri.nz/~hadfield/
National Institute for Water and Atmospheric Research
PO Box 14-901, Wellington, New Zealand
|
|
|
Re: Getting rid of loops [message #18931 is a reply to message #18926] |
Thu, 17 February 2000 00:00  |
davidf
Messages: 2866 Registered: September 1996
|
Senior Member |
|
|
Mark Hadfield (m.hadfield@niwa.cri.nz) writes:
> The UNIQ function, along with the SORT function, will do this:
>
> print, UNIQ(cycles, SORT(cycles))
>
> or if you know that your array is already monotonic, just:
>
> print, UNIQ(cycles)
Uh, actually:
print, cycles(UNIQ(cycles))
But we know what you mean. :-)
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438 E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|