mat←[0 1 2◇3 4 5]
mat0 1 2
3 4 5
Read Nested arrays and trains first.
∊ (Epsilon)∊ (Enlist)Enlist recursively collects all the atoms in nested arrays into one vector.
∊ (Member of)Member of tests whether each item of its left argument is an item of the vector on its right, comparing whole items. Here neither "ab" nor "abcd" matches the string "abc".
When the right argument is a vector, Member of tests each item of the left argument. When the right argument is a matrix, it tests each row of the left argument:
⍷ (Epsilon Underbar)⍷ (Find)Find marks the start of each occurrence of a pattern. Occurrences can overlap, as in "ana" within "Banana".
A pattern can have more than one dimension:
[[0 1◇1 0] [0 1 0 0◇1 0 0 1◇0 0 1 0◇0 1 0 0]]
⍷ (Groups)Groups gives the positions of each distinct major cell, in order of first appearance:
The length of each group is the number of occurrences of its major cell:
∩ (Up shoe)∩ (Duplicates)Duplicates keeps one copy of each major cell that occurs more than once:
∩ (Intersection)Intersection keeps the major cells of the left argument found in the right argument. It preserves their order and repetitions.
∪ (Down shoe)∪ (Unique)Unique keeps the first occurrence of each distinct major cell:
Unique mask followed by Replicate gives the same result:
∪ (Union)Union appends the major cells of the right argument that are absent from the left. Repetitions already present in either argument stay.
⌷ (Squad)⌷ (Materialise)⌷Y returns Y unchanged.
Its inverse also leaves the argument unchanged:
⌷ (Index)Indexing reads items at chosen positions. Those items form a selection. ⌷ uses each item of its left argument to select along one axis, starting with axis 0. A vector of positions for a single axis must be one item of the left argument, hence the nested brackets in [[3 5]].
With two vectors, ⌷ takes every combination of the selected rows and columns:
A dot before the brackets makes a selection. Positions count from 0; negative positions count from the end:
The outer brackets contain one index per axis. To select several positions along one axis, put a vector inside them:
.[] pairs its index arrays, rather than taking every combination. With the same row and column positions, .[] now reads two items:
A digit subscript selects one major cell along axis 0. A negative subscript counts from the end. Touching digits form one position: ₁₂ selects position 12, and ₋₁₂ counts twelve places from the end. On a matrix, one subscript selects a row. Spaced subscripts form a coordinate: mat₁ ₂ selects row 1, column 2. Because a dot followed by a digit is a decimal point, write v₂ or v.[2] rather than v.2 for position 2:
A subscript attaches to the preceding item before surrounding functions apply. +/mat₁ selects row 1 before adding its items. In v₁², item 1 is selected and then squared:
After a space, the subscript or coordinate selects from the whole run on its left. A spaced subscript needs a space after it too:
A digit subscript needs an axis to select along. On an atom or scalar, which has none, it raises an INDEX error.
Axes omitted at the end stay whole. Selecting a single position removes its axis, while selecting an array of positions replaces that axis with the index array’s shape. Paired index arrays occupy the first index array’s place in the result, and whole axes keep their places:
: takes one whole axis. Here the rows stay whole and columns 1 and 3 are selected:
Keep : as a separate item, with a space after it, as in mat.[: 1]. In mat.[:1], : is a function applied to 1, and the selection is row 1.
∞ takes all remaining axes at that position. At most one ∞ is allowed. Here it keeps both axes, so the result is mat:
A selector after ∞ applies to the last axis. Here ∞ keeps the rows and 1 selects column 1:
An empty bracket list selects the whole array unchanged:
¯∞ inserts a new unnamed axis of length 1, without consuming an input axis. The new axis goes where ¯∞ stands in the brackets:
Index arrays agree in shape as arithmetic arguments do. This selects column 2 from row 0, column 0 from row 1 and column 3 from row 2:
Repeating the same index array selects a diagonal, with row and column positions equal:
A coordinate lists positions along successive axes, such as [0 1] for row 0, column 1. With an array of coordinates, .[] reads the item or cell at each coordinate. Where gives coordinates for a condition on a matrix:
A coordinate array takes up one axis for each position in a coordinate. Any further index arrays pair with the coordinates. In this rank-3 array, two coordinates select rows, and the last index gives a column for each row:
A path can continue into nested items, as Pick does. These paths select item 2 of item 1 and item 0 of item 0:
The cells selected by the paths must all have the same shape. After paths go into nested items, :, ∞ and ¯∞ can keep or add axes of those cells:
Use a string to select a key. .name reads the key "name", whereas .[...] can compute a key from a variable or expression. Dotted paths continue from left to right through nested records:
T.(expression) evaluates an expression with T’s keys as names. A key’s name takes precedence over an outer name with the same spelling. Other names still refer to their surrounding definitions. Assigning to a name inside T.(...) does not change T:
With keys on a matrix’s first axis, each name stands for a whole major cell:
A key’s value can also be a function. After a function, however, a dot before another function makes an inner product, as in +.×.
Put a space before the argument when calling a function stored in a field, because ops.double3 reads the key "double3".
After a function, .name and .[...] select from its result. Same ⊢ returns its argument unchanged, so ⊢.address.city reads that field. A standalone .[I] is a selector function. The suffix also works after a dyadic function:
Use Each to read a field from every record in a vector. To evaluate an expression for every record, apply a dfn with Each; T.(...) itself binds the keys on T’s first axis, rather than mapping over records:
Dotted paths and subscripts bind more tightly than functions. 2×rec.price doubles the price, and rec.address.city₁ reads character 1 of the city. A path is read left to right, with the array evaluated before its index:
An index array can have a named axis. Indices with the same axis name pair; indices with different names keep separate axes and build every combination:
If only one index has a given axis name, it selects the data axis with that name, wherever it stands in the brackets. Axis-qualified Iota gives an index with the axis’s name and keys:
Jan Feb Mar
NY 10 20 30
LA 40 50 60
You can select keys with strings on any axis, and mix strings with positions:
Selecting with a single string removes its axis. Selecting with a one-item vector of strings keeps that axis, with length 1:
When several selectors share an axis name, they pair and select the data’s axes in order instead. Both selectors below have the name "col": one gives a row for each column, and the other gives the column:
Reading at a position outside its axis, or at a position that is not a whole number, returns the fill, which is zero for these numbers. NaN, written $n, isn’t a whole number. Negative positions still count from the end:
An assignment through a selection changes the original array. Assigning to a single position stores the whole value there, while assigning to a vector selection replaces its selected items. For a subscript target, put no space before the first subscript: write v₁←5, not v ₁←5:
With +←, modified assignment adds to the existing values instead of replacing them. The function is applied once for each appearance of a repeated position. Assigning to a position that isn’t a whole number, such as NaN, changes nothing:
Plain assignment can repeat a position. Which value a repeated position keeps isn’t promised, so the two values assigned here are equal:
⌷ writes to every combination of its indices instead of pairing them. The space before ← makes the whole run on its left the target. A dotted target such as v.[[1 3]] is one run already, so it needs no space:
Plain assignment through a missing key adds it, and creates missing records along a dotted path. Modified assignment adds a missing key too, starting from the fill. Reading a missing key returns the fill, and assigning by position past the end of an axis changes nothing:
Jan Feb Mar Apr
NY 10 20 30 1
LA 40 50 60 2
SF 70 80 90 3
Adding a row and column together fills the other new cells with zero:
Jan Feb Mar Apr May
NY 10 20 30 1 0
LA 40 50 60 2 0
SF 70 80 90 3 0
TX 0 0 0 0 9
Assigning to a vector selection matches replacement keys to the keys on the axis it keeps. Here the replacement lists the keys in reverse order, but each new price goes to its matching key. Assigning to a single key instead stores the whole right argument as that item’s value:
An assignment can repeat a position or a key. Which replacement it keeps there isn’t promised. Selecting the same key more than once drops the selected axis’s keys. Assigning through that selection keeps the original array’s keys.
⍒ (Grade Down)⍒ (Grade Down)Grade down gives the positions that would put the major cells in descending order. Reading those positions sorts the array, as > does directly.
Grade is useful when we sort one array by another. Here the names go in order of decreasing score:
The inverse, ⍒⁻, takes a permutation: the positions from 0 to one less than the length, each used once. It returns one vector whose Grade down is that permutation, rather than recovering the original values:
⍒ (Dyadic Grade Down)A character left argument gives a custom alphabet. Here c, then g, then f have increasing positions in that alphabet. Unlisted characters share a position after the listed ones. Grade down puts these positions in reverse order:
⍋ (Grade Up)⍋ (Grade Up)Grade up gives the positions that would put the major cells in ascending order. Ties keep their original order.
Grade up’s inverse likewise returns a vector whose Grade up is the given permutation:
⍋ (Dyadic Grade Up)With a custom alphabet, Grade up puts c, g, then f first:
A grade along a named axis keeps the other axis names. Here grades holds row positions for each column. When grades is used as a selector, its "row" axis selects rows and its "col" axis pairs those positions with the matching columns:
Previous: Nested arrays and trains · Study guide · Next: Rearranging and transforming arrays