Skip to main content

Matrix

A matrix represents a 2-dimensional array of numeric values - each row and column is labelled by default as R1, C1 etc. but these can be whatever you like.

Constructor​

To create a matrix, you can use the following constructor.

// Create a matrix with 4 rows and 5 columns
m1 = Matrix(4, 5)

// Create a matrix with 3 rows and 3 columns with the labels A, B and C
m2 = Matrix(["A","B","C"])

Constructor functions​

FunctionDescription
IdentityMatrix(size)Creates a new identity matrix of the given size
Matrix(labels)Creates a new matrix from a list of labels
Matrix(labels, v)Creates a new matrix from a list of labels
Matrix(x, y)Creates a new matrix
Matrix(y, x, v)Creates a new matrix, initialising all values to the provided value

Properties​

A matrix has the following properties:

NameDescriptionType
nameThe name of the matrixString
descriptionA description of the matrixString
ondateThe date this matrix representsDate
xLabelsThe list of x or column labelsString Array
yLabelsThe list of y or row labelsString Array
xSizeThe number of columnsInteger
ySizeThe number of rowsInteger
currencyThe currency of the values in this matrixString
unitsThe units of the values in this matrixString
timezoneThe timezone for the values in this matrixString
precisionThe data value precision configurationString
identityThe configured identity for this curveObject

Methods​

A matrix has the following methods:

NameDescriptionReturn Type
setLabels(String Array)Sets the labels for the rows and columns of a symmetrical matrixThe current matrix
setXLabels(String Array)Sets the labels for the columns of this matrixThe current matrix
setYLabels(String Array)Sets the labels for the rows of this matrixThe current matrix
setColumnData(column, list)Sets the values for the specified column name or indexThe current matrix
setRowData(row, list)Sets the values for the specified row name or indexThe current matrix
setValue(column, row, value)Sets the value for the specified column and row indexThe current matrix
setValueAt(column, row, value)Sets the value for the specified column and row labelThe current matrix
getValue(column, row)Gets the value for the specified column and row indexDouble value
getColumnForLabel(column)Gets the column index for a specified column labelInteger
getRowForLabel(row)Gets the row index for a specified row labelInteger
isSameDimensions(matrix)Tests to see if the passed in matrix is the same dimensions as this matrixBoolean
transpose()Returns a 90 degree transposed version of this matrixMatrix
forwardFill()Fills null values with the previous value, operates row by rowVoid
getColumn(col)Gets the data for a columnList
getGroupForColumn(col)Gets the group name for the column number of this matrix (0-based)String
getGroupForRow(row)Gets the group name for the row number of this matrix (0-based)String
getLabelForColumn(col)Gets the column label for the column number of this matrix (0-based)String
getLabelForRow(row)Gets the row label for the row number of this matrix (0-based)String
getRow(col)Gets the data for a rowList
getValueAt(y, x)Gets the value at the specified column and rowNumber
subMatrix(rc1, rc2)Creates a new matrix as a subset of this matrixMatrix
subMatrix(y1, x1, y2, x2)Creates a new matrix as a subset of this matrixMatrix

Using Matrices​

Create a matrix and set some values​

m2 = Matrix(["A","B","C","D","E"])
m2.setValueAt("A","A",12.2)
m2.setValue(0,1,12.5)
print m2

An example of some matrix operations​

set precision 1

m1 = Matrix(["A","B","C"])
m1.setRowData("A", [1,2,3])
m1.setRowData("B", [4,5,6])
m1.setRowData("C", [7,8,9])
print m1

m2 = Matrix(["A","B","C"])
m2.setColumnData("A", [1,2,3])
m2.setColumnData("B", [4,5,6])
m2.setColumnData("C", [7,8,9])
print m2

print m1 + m2
print m1 * 2
print 1/m1
print m1 ^ 2

m3 = Matrix(2, 3)
m3.setColumnData("C1", [1,2,3])
m3.setColumnData("C2", [4,5,6])

print m3
print m3.transpose()

Further Reading​