JPhrases/LocateSelect

From J Wiki
Jump to: navigation, search

4B. Locating & Selecting

In dealing with large amounts of data it is frequently necessary to find the location or locations of a particular datum, sometimes a particular value, sometimes one satisfying certain criteria. The verbs below give examples of techniques that can be used. In the first set, indices are the result. The result of each of these can be used to select the item indexed, using an expression such as (f y){y or (x f y){y .

m1=: i.>./ Index of (IO) first occurrence of maximum of y
m2=: i:>./ IO last occurrence of maximum of y
m3=: i.<./ IO first occurrence of minimum of y
m4=: i:<./ IO last occurrence of minimum of y
m5=: {.@\: IO first occurrence of maximum of y
m6=: {.@/: IO first occurrence of minimum of y
d7=: ~:i.1: IO first item where x and y differ
d8=: e.i.1: IO first occurrence in x of any item of y
m9=: ' '&(=i:0:) IO last nonblank in y
d10=: [ + i.&1@}. IO first 1 in y after first x items
m11=: ] i. 1: IO first 1 in y
m12=: ] i: 1: IO last 1 in y
.@] i. [ IO last item of x in y
.@[ i. ])"1 IO last occurrence of y in x
.@[ i. ] IO last occurrence of y in x, from rear
d16=: I.@:E. Indices of (ISO) beginnings of x in y
d17=: e.~&, # i.@#@] ISO all occurrences of items of y in x
d18=: =#[:i.[:#[ ISO all occurrences of items of y in x
m19=: +/{.\: ISO 1s in Boolean list y
m20=: [: m38 m39 ISO 1s in Boolean table y
m21=: ] i."1 ' '"_ ISO first blank in rows of table y
.i.1:)' '"_ ~: ])"1 ISO last nonblank in rows of table y
m23=: I. ISO 1s in Boolean list y
m24=: # i.@# ISO 1s in Boolean list y
d25=: ([: $ ]) #."1 ([: > [) Indices from (boxed) indices x to table y
d26=: +/i. ISO infixes of length y, starting at x
m27=: [: i. # All indices of list y
m28=: [: { [: i.&.> $ Catalog of indices of y, in shape of y
m29=: +/\@}:@(0:,]) Indices of start positions from counts y
m30=: $ #: [: i. [: # , Table of all indices of array y (odometer)
d31=: {@(;/&i.) Catalog of all pairs from i.x and i.y
d32=: [: m20 [ e. [: , ] Indices in x of atoms of list y
m33=: a."_ i. ] Index in a. of character y
d34=: i."_1 Itemwise index in table x of rows of y
d35=: E. i. 1: IO first occurrence of array x in y
d36=: <:@(+/@(</)) Index in classes x of y
d37=: i. Index in rows of table x of list y
m38=: <"1 (Boxed) indices from rows of open table y
m39=: $ #: (# i.@$)@, Open ISO 1s in array y (inverse of m85)
m40=: m20@m43 ISO free zeros in formatted y
m41=: m20@m82 IO saddle point in y
m42=: <:@(+/\) i. i.@(+/) ISO 1s ub Boolean list y

Location of an item in an array y can also be given by means of a Boolean list x having the same number of items as y. Typically such a Boolean list can be formed using a relational or membership expression. Such a list characterizes the items of an array in a certain way. There can be 2^#y ways of forming such a Boolean list, each way characterizing the array in a different and unique way. The importance of such a Boolean list is that it can be used to select items from by an expression such as (f y) # y or (x f y) # y .  The following expressions give some of the ways such Boolean lists can be formed.

m43=: ' 0 '"_ E."1 ' '"_ ,. ] ,. ' '"_ Locate free zeros in formatted y
m44=: =&' Locate quotes in y
m45=: = >./ Locate all instances of maximum of y
m46=: '.'&~: Exclude periods in y
m47=: e.&' 0123456789' Locate digits and blanks in y
d48=: E. Locate beginning points of pattern x in y
/~@i.))) Locate primes less than y
d50=: i.@(#@])e.[ Locate indices x in y
d51=: ] e.~ [: i. [: # [ Locate indices y in x
m52=: ] e.~ [: i. [: >: >./ Locate indices y
m53=: *./ .=&' ' Locate blank rows in table y
m54=: ] -:"1 [: {. ' '"_ , ] Locate blank rows in table y
m55=: ~: Locate first instance of each item
m56=: [: *./\ ' '"_ = ] Locate leading blanks
m57=: [: *./\. ' '"_ = ] Locate trailing blanks
+/\@(=&') Locate text between and including quotes
+/\@(=&') Locate text between quotes
m60=: 2: </\ 0: , ] Locate first 1 in each group of 1s in y
m61=: 2: >/\ 0: , ] Locate first 0 after each group of 1s in y
d62=: * $ -@] {. 1: Locate ends of x fields of length y
d63=: * $ ] {. 1: Locate starts of x fields of length y
m64=: 1: ,~ 2: ~:/\ ] Loc ends of fields of identical atoms in y
m65=: 1: , 2: ~:/\ ] Loc starts of fields of identical atoms in y
m66=: 1: ,~ 2: ~:/\ ] Loc where atom differs from nxt rgt in y
m67=: 1: , 2: ~:/\ ] Loc where atom differs next left in y
m68=: ] > [: }. 0: ,~ ] Loc last 1 in each group of 1s in y
m69=: ] > [: }: 0: , ] Loc first 1 in each group of 1s
[: i. [: # ] Loc every xth item of y
[: >: [: i. # Loc every 2d item of y
Loc atoms of y divisible by x
d73=: [: -. [ e.~ [: i. ] Loc atoms of i. y not in x
d74=: [ e.~ [: i. ] Loc atoms of i.y in x
d75=: i. = [: # [ Loc items of y not in x
m76=: 0: *. ] Loc nothing
d77=: <: Loc where x implies y
d78=: > Loc where x but not y
d79=: ~: Loc exclusive or of x and y
d80=: = Loc Kronecker delta of x and y
d81=: #@[ > /:@(] ,~ i.@#@[) Loc fills formed by expanding y by x
m82=: (= <./"1) *. (="1 >./) Loc saddle points of table y
m83=: [: #: [: i. 2: ^ # Loc all subsets of order #n (truth table)
m84=: [: #: [: i. 2: ^ ] Loc all subsets of order n (truth table)
m85=: 1: [`(m38@])`(m93@])} ] Loc indices y in large enough table (connection matrix from table of indices) (inverse of m39)
m86=: 1: [`(<"1@])`((>:@(>./@])$0:)@]) } ] Loc indices y in large enough table
m87=: ' '&= Loc blanks in y
m88=: = Loc each item of set of y (position or distribution matrix)
m89=: (#~ (1: ~: +/"1))@= Table in which each row Locs an infix in y of 2 or more consecutive equal items
d90=: [: , [ {.&> ] (<@#"0) 1: Expansion mask for fields of length y to uniform field of length |x
m91=: _8&d90 Expansion mask for fields of length y to uniform right justified fields of length 8
m92=: [: -. [: *./\. m53 Loc rows preceding trailing blank rows
m93=: 0: $~ [: >: >./ Loc nothing in table large enough for y
d94=: [ -:"1 ([: # [) {."1 ] Loc rows of y beginning with x
m95=: ' '&(+./ .~:) Loc nonblank columns
m96=: [: -. [: *./\. ' '"_ = ] Exclude trailing blanks
m97=: m105@m95 Exclude leading blank columns
m98=: +. 1&,@}: Loc 1s and 1st 0 in each group of 0s
m99=: m98@m95 Exclude all but first blank columns
m100=: *./ .=&' ' Loc blank rows
m101=: 1: , 2: +./\ ] Exclude all 0s but the first in a group
m102=: m101@m100 Exclude all but first blank rows
m103=: +./\. Loc items left of leftmost trailing 0
m104=: m103@m95 Exclude trailing blank columns
m105=: +./\ Loc items right of rightmost leading 0
m106=: *./\ Exclude all 1s right of first 0
m107=: *./\. Exclude all 1s left of last 0
m108=: +./ .~:&' ' Loc nonblank rows
m109=: m103@m108 Exclude trailing blank rows
m110=: m105@m108 Exclude leading blank rows
m111=: m98@m95 Include only first of group of blank cols
m112=: m98@m108 Include only first of group of blank rows
d113=: +./ .~:"1 Loc rows of x having atoms not y
d114=: -:"1 Loc rows of x matching y
d115=: ,@,. # $&1 0@#@, Alternate i{x 1s and i{y 0s
m116=: # $&1 0@# Alternate i{x 1s, (1+i){x 0s, etc
m117=: +./\ *. +./\. Loc atoms between leading & trailing 0s
m118=: *. *./\@(= +./\)"1 Loc first infix of 1s
m119=: ' '&~: Loc nonblanks
d120=: ]*.*.(]e.#)+/\@(2:</\0:,])@] Loc groups of ones in y indicated by x
m121=: +.~:/\ Loc 1s in y and 0s between pairs of 1s
m122=: -. *. ~:/\ Loc 0s between pairs of 1s
m123=: </\ Loc leftmost 1
m124=: <:/\ Loc 1s and all 0s following leftmost 0
m125=: ~:/\ Loc where odd number of 1s are at left

If L is a function that determines locations (that is, indices) in an argument according to some criteria, then the function L { ] selects the indicated portions of the argument. For example:

d126=: #"1 1s in x select from each row (list) of y
d127=: #.@[ { ] Boolean list x as integer selecting from y
d128=: <"1@[ { ] Scattered index table x select from y
d129=: { Selecting x from y
d130=: >./ . # Select maximum of x located by y
m131=: m41 { ] Select saddle point(s) of y
m132=: ([:-.(1:,''"_=])#.1:)}.] Delete trailing blanks
m133=: m104 #"1 ] Delete trailing blank columns
m134=: m97 #"1 ] Delete leading blank columns
m135=: m110 # ] Delete leading blank rows
m136=: m109 # ] Delete trailing blank rows
m137=: m98@m108 # ] Delete repeated blank rows
m138=: m98@m95 #"1 ] Delete repeated blank columns
m139=: ] -. [: {. ' '"_ , ] Delete blank rows
m140=: m108 # ] Delete blank rows
m141=: m95 #"1 ] Delete blank columns
m142=: ([: m117 ] ~: ' '"_) # ] Delete leading & trailing blanks
m143=: +/@(*./\@(' '&=)) }. ] Delete leading blanks
m144=: #~ ([: +./\. ' '&~:) Delete trailing blanks
d145=: , Append atom or list to table
d146=: ,. List or atom as new column of table
m147=: ~. Delete repeated items
m148=: */@}:@$ Number of rows in array y
.@(></\))@(' '&~:) Delete extra blanks
d150=: ,. Join along last dimension
d151=: ,.&.>/ Join items of boxed array