part
Returns a part of an object.
Syntax
-
part(X, R)-
Xis any container -
Ris a range
-
-
part(X, Rx, Ry)-
Xis any planar container -
RxandRyare ranges
-
Description
If X is a linear container and R a range, then part(X, R) returns the part of X that is specified by R. R can be an index, a range(a, b), or a list of indices and ranges of any length and composition.
In this case X can be a vector, a matrix (row-major order), a list, a structure, a pixmap (row-major order), or a binary data object.
If X contains n elements, then indices 1..n counts from the start of X, while indices −n..−1 counts from the end of X. Positive and negative indices can be freely mixed; for instance, range(2, −2) means the range from the second element to the next to last element. If b (being either positive or negative) refers to an element preceding a in X, then range(a, b) means all indices from b to a in reverse order.
range(a, b, k) produces the sequence from a to b (possibly in reverse) but taking only every kth index.
If X is a planar container (such as a matrix or a pixmap), then part(X, R1, R2) returns the part of X with horizontal indices described by R1 and vertical indices described by R2.
Examples
v ≔ ❨4, 1, 0, 2, 5, 7, −3, 4, 7, 8❩
(4, 1, 0, 2, 5, 7, −3, 4, 7, 8)
part(v, 4)
(2)
part(v, '(4, 5, 1, 2))
⎛2⎞ ⎜5⎟ e⎜4⎟ ⎝1⎠
part(v, '(1, range(5, 8)))
⎛4 ⎞ ⎜5 ⎟ e⎜7 ⎟ ⎜−3⎟ ⎝4 ⎠
part(v, '(1, range(5, −1)))
⎛4 ⎞ ⎜5 ⎟ ⎜7 ⎟ e⎜−3⎟ ⎜4 ⎟ ⎜7 ⎟ ⎝8 ⎠
part(v, '(1, range(−1, −5)))
⎛4 ⎞ ⎜8 ⎟ ⎜7 ⎟ e⎜4 ⎟ ⎜−3⎟ ⎝7 ⎠
part(v, range(1, −1, 2))
⎛4 ⎞ ⎜0 ⎟ e⎜5 ⎟ ⎜−3⎟ ⎝7 ⎠
A ≔ ❨❨5, 1, 2, 3, 6❩, ❨1, 2, 3, 6, 7❩, ❨4, 1, 2, 3, 8❩, ❨0, 1, 2, 6, 1❩❩
⎛5 1 2 3 6⎞ ⎜1 2 3 6 7⎟ ⎜4 1 2 3 8⎟ ⎝0 1 2 6 1⎠
part(A, range(3, 6))
2 3 6 1
part(A, range(−1, 1, 2))
1 2 0 3 1 7 3 1 3 1
part(A, '(1, 4, 5), range(−1, −3))
⎛0 6 1⎞ ⎜4 3 8⎟ ⎝1 6 7⎠