Matrices
 
 
 

A matrix is a two-dimensional table of floating point values.

To declare a matrix, use the matrix keyword and the variable name, followed by the number of rows and columns in the matrix:

matrix $a3[2][3];

This creates a matrix with 2 rows of 3 columns, and assigns it to variable $a3. Each element of the matrix is initially filled with zeros.

Unlike arrays, you must specify the size of a matrix when you create it:

matrix $a2[][]; // ERROR: Size not specified

Literal representation

The literal representation of a matrix is a series of values separated by commas representing rows, with rows separated by semicolons. The values are surrounded by << and >>:

matrix $a3[3][4] = <<2.5, 4.5, 3.25, 8.05;
 1.12, 1.3, 9.5, 5.2;
 7.23, 6.006, 2.34, 4.67>>

Any difference between the size you specify and the literal matrix you assign is filled with zeros.

Even when you assign a literal matrix to a variable, you must still specify the size of the matrix:

matrix $a1[][] = <<1; 4>>; // ERROR: Size not specified

Getting and setting matrix values

Setting an element of a matrix is similar to setting an element in an array. Remember that the first index specifies the row and the second index specifies the column.

matrix $a3[4][2];
$a3[1][0] = 9;

Unlike arrays, you cannot expand the size of a matrix. If you try to set a value outside the range of the matrix MEL will signal an error:

$a3[0][1] = 7; // ERROR: Element doesn’t exist