The examples in this section use the following Matrix. Note that (with one exception, indicated below) the same techniques can be used on Vectors and Arrays.
>
|
|
To extract a single entry, type its row and column coordinates in square brackets after the name of the Matrix:
The simplest way to extract a row is by using a single index.
To extract part of a row (or column) as a row Vector (or column Vector), use a range instead of a single index for the row (or column) coordinate:
>
|
|
To extract a sub-Matrix, replace both coordinates with ranges:
Notice that you can control whether the result is the entry, a 1-element row Vector, a 1-element column Vector, or a 1x1 Matrix based on whether you use single indices or ranges:
>
|
|
If you do not want a contiguous set of entries, or if you wish to reorder rows or columns, use a list (which can also include ranges):
>
|
|
Note that an index list containing a single item (for example, [3]) yields the same results as a range, with the same beginning and end points.
>
|
|
You can use negative indices to count backwards from the end of a Vector or Matrix. Use -1 to indicate the last row (or column), -2 to indicate the second last row (or column), and so on. (This technique does not apply to Arrays since they can have negative indices.)
>
|
|
You can also use implicit range endpoints. For example, means from the third row (or column) to the last row (or column), and means from the first row (or column) to the third row (or column). Thus, the following syntax is equivalent to for the 5x5 Matrix .
You can also use other commands such as select to remove columns or rows of a matrix that do not match a given criteria. For example, say we wanted to remove any row where the value in the first column is not even:
>
|
|
We can also filter out columns in a similar manner. Say we wanted to filter out any columns where the value in the second row is less than 3.
>
|
|
>
|
|