 The Selection Operation - Maple Programming Help

Home : Support : Online Help : Programming : Operations : Sets and lists : selection

The Selection Operation

 Calling Sequence A[expr]

Parameters

 A - name, table, list, set, string, or expression sequence expr - expression

Description

 • The selection operation $\left[\right]$ can be used to select components from an aggregate object.
 • If A evaluates to an unassigned name then the selection operation yields an indexed name.
 • If A evaluates to a table then the selection operation is the standard table indexing operation.
 • If A evaluates to an hfarray the selection operation yields either a Maple float (if the number of indices matches the number of dimensions of the hfarray), or a sub-hfarray (if the number of indices is less than the number of dimensions).
 • If A evaluates to a list, set, or expression sequence, then the argument expr must evaluate to an integer, a range, a sequence of integers or NULL.
 • If A evaluates to a string, then A[expr] is equivalent to substring(A,expr).
 • If expr evaluates to an integer i, then the i^th element is returned.
 • If A evaluates to a list, set, or expression sequence, and expr evaluates to a range, then a list, set, or expression sequence of the elements specified by the range is returned.  If the list, set or sequence contains sublists or subsets, then expr can be an expression sequence, in which case the appropriate subentry will be returned.
 • If A evaluates to a table, and expr evaluates to a range, then the selection remains unevaluated.
 • Negative integers can appear in expr. In this case, elements are counted from the end of the list, set, or expression sequence. The -1^th element is the last element, the -2^th element is the second last, and so on.
 • If expr evaluates to a range of positive integers, the lower bound must be less than or equal to one more than the upper bound. Specifically, ${A}_{3..3}$ selects a single element, ${A}_{3..2}$ produces an empty selection, and ${A}_{3..1}$ is not permitted. Negative integers in a range are first converted to equivalent positive integers, then this requirement is imposed.
 • If expr evaluates to NULL, then an expression sequence of all elements is returned unless $A$ is a string, in which case $A\left[\right]$ is returned.

Accessing Stored Values in Arrays, Matrices and Vectors

 • To select the first $n$ elements of an Array (or Matrix, or Vector), A, you can use A[1..n]. Alternatively, you can enter A[..n].
 • To select trailing elements, use A[..n].
 • You can select all elements of an Array, Vector, or Matrix using A[...].
 • You can select specific rows or columns (or multiple rows or columns). To select a specific row, use A[a[i],...]. to select the ith row. Use A[...,a[i]] to select the ith column. To select multiple rows or columns, use A[[a[i], a[i+k]],...] or A[...,[a[i],a[i+k]]]
 • For more information, including examples, on using the selection operation, see chapter 4 of the Programming Guide.

Programmer Indexing and Mathematical Indexing

 • Arrays, Matrices, and Vectors have different indexing mechanisms:mathematical and programmer indexing. Mathematical indexing uses the basic selection operator, [] while programmer indexing uses round brackets, ().
 • For details on programmer and mathematical indexing, see rtable_indexing.

Examples

 > $L≔\left[a,b,c\right]:$
 > $L\left[1\right]$
 ${a}$ (1)
 > $L\left[1..2\right]$
 $\left[{a}{,}{b}\right]$ (2)
 > $L\left[-2..-1\right]$
 $\left[{b}{,}{c}\right]$ (3)
 > $L\left[3..3\right]$
 $\left[{c}\right]$ (4)
 > $L\left[3..2\right]$
 $\left[\right]$ (5)

The following would lead to a selection of length -1, and so is not legal:

 > $L\left[3..1\right]$
 > $X≔L\left[\right]$
 ${X}{≔}{a}{,}{b}{,}{c}$ (6)
 > $X\left[1\right]$
 ${a}$ (7)
 > $X\left[1..2\right]$
 ${a}{,}{b}$ (8)
 > $X\left[-2..-1\right]$
 ${b}{,}{c}$ (9)
 > $L≔\left[1,\left[2,3\right],\left[4,\left[5,6\right],7\right],8,9\right]$
 ${L}{≔}\left[{1}{,}\left[{2}{,}{3}\right]{,}\left[{4}{,}\left[{5}{,}{6}\right]{,}{7}\right]{,}{8}{,}{9}\right]$ (10)
 > $L\left[3,2,1\right]$
 ${5}$ (11)
 > $S≔\left\{a,b,c\right\}$
 ${S}{≔}\left\{{a}{,}{b}{,}{c}\right\}$ (12)
 > $S\left[1\right]$
 ${a}$ (13)
 > $S\left[1..2\right]$
 $\left\{{a}{,}{b}\right\}$ (14)
 > $S\left[-2..-1\right]$
 $\left\{{b}{,}{c}\right\}$ (15)
 > $T\left[1\right]≔a:$$T\left[2\right]≔b:$$T\left[3\right]≔c:$
 > $T\left[1\right]$
 ${a}$ (16)
 > $T\left[1..2\right]$
 ${{T}}_{{1}{..}{2}}$ (17)
 > $T\left[-2..-1\right]$
 ${{T}}_{{-2}{..}{-1}}$ (18)
 > $S≔"a string"$
 ${S}{≔}{"a string"}$ (19)
 > $S\left[1\right]$
 ${"a"}$ (20)
 > $S\left[1..2\right]$
 ${"a"}$ (21)
 > $S\left[-2..-1\right]$
 ${"ng"}$ (22)

Use the selection operation to select a specific row or column.

 > $M≔\mathrm{Matrix}\left(3,3,\mathrm{symbol}=m\right)$
 ${M}{≔}\left[\begin{array}{ccc}{{m}}_{{1}{,}{1}}& {{m}}_{{1}{,}{2}}& {{m}}_{{1}{,}{3}}\\ {{m}}_{{2}{,}{1}}& {{m}}_{{2}{,}{2}}& {{m}}_{{2}{,}{3}}\\ {{m}}_{{3}{,}{1}}& {{m}}_{{3}{,}{2}}& {{m}}_{{3}{,}{3}}\end{array}\right]$ (23)

Select the first row of matrix, M.

 > $M\left[1,..\right]$
 $\left[\begin{array}{ccc}{{m}}_{{1}{,}{1}}& {{m}}_{{1}{,}{2}}& {{m}}_{{1}{,}{3}}\end{array}\right]$ (24)

Select the third column of M.

 > $M\left[..,3\right]$
 $\left[\begin{array}{c}{{m}}_{{1}{,}{3}}\\ {{m}}_{{2}{,}{3}}\\ {{m}}_{{3}{,}{3}}\end{array}\right]$ (25)

Select first and second row of M.

 > $M\left[\left[1,2\right],..\right]$
 $\left[\begin{array}{ccc}{{m}}_{{1}{,}{1}}& {{m}}_{{1}{,}{2}}& {{m}}_{{1}{,}{3}}\\ {{m}}_{{2}{,}{1}}& {{m}}_{{2}{,}{2}}& {{m}}_{{2}{,}{3}}\end{array}\right]$ (26)

Select all of the elements of M.

 > $M\left[..\right]$
 $\left[\begin{array}{ccc}{{m}}_{{1}{,}{1}}& {{m}}_{{1}{,}{2}}& {{m}}_{{1}{,}{3}}\\ {{m}}_{{2}{,}{1}}& {{m}}_{{2}{,}{2}}& {{m}}_{{2}{,}{3}}\\ {{m}}_{{3}{,}{1}}& {{m}}_{{3}{,}{2}}& {{m}}_{{3}{,}{3}}\end{array}\right]$ (27)