Previous Up Next

5.48.1  Creating matrices and modifying elements by assignment

You can give a matrix a name with assignment.
Input:

A := [[1,2,6], [3,4,8], [1,0,1]]

The rows are then accessed with indices.
Input:

A[0]

Output:

[1,2,6]

Individual elements are simply elements of the rows.
Input:

A[0][1]

Output:

2

This can be abbreviated by listing the row and column separated by a comma.
Input:

A[0,1]

Output:

2

The indexing begins with 0; if you want the indexing to begin with 1 by enclosing them in double brackets.
Input:

A[[1,2]]

Output:

2

You can use a range of indices to get submatrices.
Input:

A[0..2,1]

Output:

[2,4,0]

Input:

A[0..2,1..2]

Output:

[[2,6],[4,8],[0,1]]

Input:

A[0..1,1..2]

Output:

[[2,6],[4,8]]

An index of -1 returns the last element of a list, an index of -2 the second to last element, etc.
Input:

A[-1]

Output:

[1,0,1]

Input:

A[1,-1]

Output:

8

Individual elements of a matrix can be changed by assignment.
Input:

A[0,1] := 5

then:

A

Output:

[[1,5,6],[3,4,8],[1,0,1]]

Previous Up Next