Home page for accesible maths LAB100

Style control - access keys in brackets

Font (2 3) - + Letter spacing (4 5) - + Word spacing (6 7) - + Line spacing (8 9) - +

1 Constructing a Matrix

Matrices can be constructed in RStudio using the matrix command:

matrix( data, nrow, ncol, byrow )

The input arguments for this command are:

Argument Description
data A vector containing all of the values to be entered into the matrix.
nrow The number of rows that the matrix should have.
ncol The number of columns that the matrix should have.
byrow A logical argument denoting whether the contents of data should be inserted by row (TRUE) or by column (FALSE - default)

As an illustration, lets create the three matrices below.

A=[147258369]  B=[19-13205-6]  C=[222222]

A <- matrix( 1:9, nrow=3, ncol=3, byrow=FALSE )

B <- matrix( c(1, 9, -13, 20, 5, -6), nrow=2, ncol=3, byrow=TRUE )

C <- matrix( rep(2,6), nrow=3, ncol=2, byrow=FALSE )

Note that the length of the data vector must be equivalent to the number of elements in the matrix, i.e. for a nrow×ncol matrix, the length of the data vector must be nrow*ncol. If this is not the case, then RStudio would will cycle through the data vector until a value has been entered into each element of the matrix. Furthermore, if the number of matrix elements is not a multiple of the data vector’s length, then a warning message is produced in the console. Try the following example to understand how the data is entered into matrices

a <- 1:5

matrix(a, ncol=5, nrow=2, byrow=FALSE)

matrix(a, ncol=2, nrow=2, byrow=FALSE)

matrix(a, ncol=3, nrow=4, byrow=FALSE)