cutting/grouping [message #85773] |
Thu, 05 September 2013 08:41  |
Seb
Messages: 15 Registered: January 2006
|
Junior Member |
|
|
Hi,
I've been unsuccessfully searching the docs for a function that would
"break" an array. For any R users in the list: something like the 'cut'
function, which does the following:
R> (a <- 1:10)
[1] 1 2 3 4 5 6 7 8 9 10
R> (b <- seq(0, 10, 2))
[1] 0 2 4 6 8 10
R> cut(a, breaks=b)
[1] (0,2] (0,2] (2,4] (2,4] (4,6] (4,6] (6,8] (6,8] (8,10]
(8,10]
Levels: (0,2] (2,4] (4,6] (6,8] (8,10]
Is there a particular module that needs to be loaded?
Thanks,
--
Seb
|
|
|
Re: cutting/grouping [message #85774 is a reply to message #85773] |
Thu, 05 September 2013 14:39   |
Heinz Stege
Messages: 189 Registered: January 2003
|
Senior Member |
|
|
Hi Seb,
I have absolutely no knowledge about R. The following may help you, or
may not. Look for the value_locate function in the IDL documentation.
It can reproduce the results of your R calculation:
IDL> a=indgen(10)+1
IDL> b=indgen(6)*2
IDL> pos=value_locate(b,a)
IDL> pos-=b[pos] eq a
IDL> print,b[pos]
0 0 2 2 4
4 6 6 8 8
IDL> print,'('+strtrim(b[pos],2)+','+strtrim(b[pos+1],2)+']'
(0,2] (0,2] (2,4] (2,4] (4,6] (4,6] (6,8] (6,8] (8,10] (8,10]
IDL> u=uniq(pos)
IDL> print,'('+strtrim(b[pos[u]],2)+','+strtrim(b[pos[u]+1],2)+'] '
(0,2] (2,4] (4,6] (6,8] (8,10]
Cheers, Heinz
|
|
|
Re: cutting/grouping [message #85777 is a reply to message #85774] |
Thu, 05 September 2013 21:15   |
Seb
Messages: 15 Registered: January 2006
|
Junior Member |
|
|
On Thu, 05 Sep 2013 23:39:47 +0200,
Heinz Stege <public.215.967@arcor.de> wrote:
> Hi Seb, I have absolutely no knowledge about R. The following may help
> you, or may not. Look for the value_locate function in the IDL
> documentation. It can reproduce the results of your R calculation:
IDL> a=indgen(10)+1 b=indgen(6)*2 pos=value_locate(b,a) pos-=b[pos] eq a
IDL> print,b[pos]
> 0 0 2 2 4 4 6 6 8 8
IDL> print,'('+strtrim(b[pos],2)+','+strtrim(b[pos+1],2)+']'
> (0,2] (0,2] (2,4] (2,4] (4,6] (4,6] (6,8] (6,8] (8,10] (8,10]
IDL> u=uniq(pos)
IDL> print,'('+strtrim(b[pos[u]],2)+','+strtrim(b[pos[u]+1],2)+'] '
> (0,2] (2,4] (4,6] (6,8] (8,10]
Thanks, this is a great tool. I see though that it's limited to cases
where 'a' increases monotonically.
Cheers,
--
Seb
|
|
|
Re: cutting/grouping [message #85778 is a reply to message #85777] |
Fri, 06 September 2013 05:48  |
Heinz Stege
Messages: 189 Registered: January 2003
|
Senior Member |
|
|
On Thu, 05 Sep 2013 23:15:54 -0500, Seb wrote:
> Thanks, this is a great tool. I see though that it's limited to cases
> where 'a' increases monotonically.
This may be due to the uniq function. Try
u=uniq(pos,sort(pos))
instead of u=uniq(pos) and it should work for random a's.
Good luck, Heinz
|
|
|