MedCalc  # Matrices

A matrix is a rectangular array of real and/or complex numbers arranged in rows and columns. A r × c matrix has r rows and c columns. The whole numbers r and c are the dimensions of the matrix. The individual items in the matrix are elements. The element on row r and column c is identified with er,c.

In the literature, matrices are commonly denoted by a boldface uppercase character, e.g. A.

A matrix with a single row or a single column is called a vector. A r × 1 matrix is a column vector, and a 1 × c matrix is called a row vector. A 1 × 1 matrix is called a scalar.

A matrix with the same number of rows and columns is called a square matrix.

An identity matrix or unit matrix is a square matrix in which all the elements of the principal diagonal are ones and all other elements are zeros. The effect of multiplying a given matrix by an identity matrix is to leave the given matrix unchanged.

A zero matrix is a matrix where all elements are zero.

A diagonal matrix is a square diagonal matrix with all elements that are not on the principal diagonal, equal to zero.

To make a distinction between matrices that contain real numbers and matrices that contain complex numbers, the term real matrix or complex matrix is used.

In the MedCalc spreadsheet a matrix is represented in a single cell as a list of values between curly brackets with values on the same row separated with a comma (or your local list separator symbol) and rows are separated with a vertical bar. For example, the textual representation of the matrix

 5 6 7 8

is {5,6|7,8}.

For large matrices, when not all values can be displayed in a single cell, and the values that cannot be displayed are represented with 3 dots. For example, the identity matrix with 12 rows and 12 columns will be textually represented as

{1,0,0,0,0,0,0,0,0,0,0,0| 0,1,0,0,0,0,0,0,0,0,0,0| 0,0,1,0,0,0,0,0,0,0,0,0| 0,0,0,1,0,0,0,0,0,0,0,0| 0,0,0,0,1,0,0,0,0,0,0,0| 0,0,0,0,0,1,0,0,0,0,0,0| 0,0,0,0,0,0,1,0,0,0,0,0| 0,0,0,0,0,0,0,1,0,0,0,0| 0,0,0,0,0,0,0,0,1,0,0,0| 0,0,0,0,0,0,0,...}

## Create a matrix from data

To create a matrix from existing data, use the { } operator as follows:

### Create a matrix from variables in the spreadsheet

{variables list} returns a matrix containing the data of all cases in the spreadsheet without missing data for several variables. The number of columns is the number of variables in the variables list, and the number of rows is the number of cases without missing data for any of the variables.

When var1, var2 and var3 are variables in the spreadsheet, then {var1,var2,var3} will return a n × 3 matrix where n, the number of rows in the matrix, is the total number of cases that do not have missing data for var1, var2 or var3.

#### Create a matrix from a spreadsheet range

{range} creates a matrix containing all data in the specified spreadsheet range. The data range can contain missing data.

{a1:b20} creates a 20 × 2 matrix containing the data in spreadsheet cells A1 to B20.

### Create a matrix from a data list

{v11, v12, ... | v21, v22, ...} returns a matrix containing the data listed in the argument list. The elements on the same row are separated with a comma (or your local list separator symbol), and the rows are separated with a vertical bar. The matrix may contain missing data.

{5,6|7,8} creates the following 2 × 2 matrix:

 5 6 7 8

{5,6|7,8,9} creates the following 2 × 3 matrix:

 5 6 7 8 9

The entries in the data list may be numbers or cell references.

Since the length of the text in a MedCalc cell is limited to about 250 characters, this method does not allow to create large matrices.

## Basic mathematical operations

 Sign reversal −A A minus sign placed in front of a matrix reverses the sign of each element. Multiplication or division of a matrix with a real number A * r each element of matrix A is multiplied by the real number r A / r each element of matrix A is divided by the real number r Addition or subtraction of a matrix with a real number A + r the real number r is added to each element of matrix A A − r the real number r is subtracted from each element of matrix A Addition or subtraction of matrices A + B The corresponding elements of the two matrices are added. The matrices must have the same dimensions (*) A − B The corresponding elements of the two matrices are subtracted. The matrices must have the same dimensions (*) Elementwise multiplication A &* B The corresponding elements of the two matrices are multiplied. The matrices must have the same dimensions (*) Elementwise division A &/ B All elements of matrix A are divided by the corresponding elements of matrix B. The matrices must have the same dimensions (*) Multiplication of matrices A * B If A is an n × m matrix and B is an m × p matrix, A*B is an n × p matrix in which the element in row i, column j, is equal to $$(\mathbf{A}\mathbf{B})_{ij} = \sum_{k=1}^m A_{ik}B_{kj}$$ Matrix multiplication requires that the number of columns of the first matrix equals the number of rows of the second matrix Division of matrices A / B Matrix A is multiplied with the inverse of matrix B

(*) if one of the matrices is a m × n matrix and the other matrix is a column vector m × 1, then the elements of the column vector are added (resp. subtracted, multiplied, divided) to the corresponding elements of each column of the m × n matrix. If one of the matrices is a m × n matrix and the other matrix is a row vector 1 × n, then the elements of the row vector are added (resp. subtracted, multiplied, divided) to the corresponding elements of each row of the m × n matrix.

## Accessing individual elements, rows or columns

Individual elements, rows or columns can be accessed using the INDEX function.

For example, if cell A1 contains the matrix {4,5,6|7,8,9}, then

INDEX(A1,2,2) returns 8

INDEX(A1,0,2) returns the column matrix {5|8}, the 2nd column of the matrix in A1

INDEX(A1,2,0) returns the row matrix {7,8,9}, the 2nd row of the matrix in A1

Alternatively, you can use the following syntax: A1[r,c]

A1[2,2] returns 8 (in the spreadsheet, you have to precede this formula with the = sign, because the formula starts with a cell address).

## Matrix functions

MedCalc provides a large number of functions for matrix creation, removing and adding of columns and rows, matrix properties, matrix operations and transformations, and linear equations.

See Matrix functions.

## Mathematical functions on matrices

The following functions accept a matrix or data range as argument, in which case they return a matrix as result.

When a mathematical function is applied to a matrix, then the function is applied to all its individual elements.

Log, Ln, Exp, Sqrt, Int, Rand, Sign, Odd, Abs, Trunc, Ceil, Floor,

Erf, Erfc.

For functions that require 2 arguments (such as Power, Mod, Round, RandNorm, Atan2), when both arguments are matrices, these matrices must have the same number of rows and columns.

Beta,

## Statistical functions on matrices

When one of the following statistical spreadsheet functions is applied to a matrix, a row vector is returned that contains the statistic for the data in each corresponding column of the matrix.

The following functions can be used on real matrices:

AVEDEV, AVERAGE, COEFVAR, COUNT, COUNTNEG, COUNTPOS, GEOMEAN, HARMEAN, MAX, MEDIAN, MIN, PERCENTILE, PERCRANK, SEM, STDEV, SUM, SUMNEG, SUMPOS, TrimMean, TrimMeanSEM, TrimMeanLOW, TrimMeanHIGH, VAR

The following functions can be used on matrices that contain complex numbers:

AVERAGE, COUNT, SUM