Devon,
The session excerpt below suggest an approach using
I. and histogram. This approach works best if the expected
frequency of each integer is the same. This may be an
unacceptable assumption for you.
]rnn=. ?.12$10
6 5 9 2 4 9 0 7 0 4 6 8
~.rnn
6 5 9 2 4 0 7 8
range =: >./ >:@- <./
range rnn
10
tiles =: >:@:[EMAIL PROTECTED] }:@:% [
tiles 4
0.25 0.5 0.75
uppers =: <./@] + [EMAIL PROTECTED]
4 uppers rnn
2.5 5 7.5
histogram=: <: @ (#/.~) @ ([EMAIL PROTECTED]@[ , I.)
rnn histogram~4 uppers rnn
3 3 3 2
On Sun, 27 May 2007, Devon McCormick wrote:
+ Members of the Forum -
+
+ I have this function:
+
+ NB.* ncile: break vector into x equally-sized pieces based on ascending
+ values.
+ ncile=: 4 : 0"(0 1)
+ grd=. /:y
+ brkptixs=. roundNums (>:i.<:x)*x%~#y NB. Internal breakpoints only
+ brkptixs=. (grd{y) i. brkptixs{grd{y NB. Adjust for breaks across
+ =values.
+ ptn=. (1) (0,brkptixs)}0$~#y
+ ptn<;.1 grd{y
+ )
+
+ This is supposed to break up a vector into (roughly) equal-sized pieces,
+ for example:
+
+ ]rnn=. ?12$10
+ 7 4 0 2 7 6 8 4 6 9 8 4
+ 4 ncile rnn
+ +---+-----+-------+-----+
+ |0 2|4 4 4|6 6 7 7|8 8 9|
+ +---+-----+-------+-----+
+
+ The pieces are not equal-sized because it's more important that the
+ divisions
+ be non-overlapping, so all three "4"s have to be in the same group.
+
+ The companion to this returns a list of partition indexes:
+
+ NB.* ncileix: index vector by ncile into which it falls.
+ ncileix=: 4 : 0"(0 1)
+ grd=. /:y
+ brkptixs=. roundNums (>:i.<:x)*x%~#y NB. Internal breakpoints only
+ brkptixs=. (grd{y) i. brkptixs{grd{y NB. Adjust for breaks across
+ =values.
+ ptn=. (1) (0,brkptixs)}0$~#y
+ ptnix=. (#&>ptn<;.1]1$~#ptn)#i.+/ptn NB. Partition indexes/graded values
+ ptnix{~/:grd NB. Back in original input order
+ )
+
+ So, for example (matching the divisions above):
+
+ rnn,:4 ncileix rnn
+ 7 4 0 2 7 6 8 4 6 9 8 4
+ 2 1 0 0 2 2 3 1 2 3 3 1
+
+ However, my feeling is this code is a bit clumsy. Can anyone think of
+ a neater pair of solutions?
+
+ P.S. I don't really care about this behavior that fails to return "x"
+ values as my left arguments will probably be hundreds of nearly distinct
+ values and I'll usually want no more than 10 partitions:
+
+ 4 ncile 20$1
+ +---------------------------------------+
+ |1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1|
+ +---------------------------------------+
+ 4 ncileix 20$1
+ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
+
+
+ Thanks,
+
+ Devon McCormick, CFA
+ ^me^ at acm.
+ org is my
+ preferred e-mail
+ ----------------------------------------------------------------------
+ For information about J forums see http://www.jsoftware.com/forums.htm
+
(B=) <----------my "sig"
Brian Schott
Atlanta, GA, USA
schott DOT bee are eye eh en AT gee em ae eye el DOT com
http://schott.selfip.net/~brian/
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm