9.5 Matrices

  • A matrix is a 2 dimensional vector.

  • All columns in a matrix must have:
    • the same type (numeric, character or logical)
    • the same length

9.5.2 Two-dimensional object

Vectors have one index per element (1-dimension).
Matrices have two indices (2-dimensions) per element, corresponding to the row and the column:

  • Fetching elements of a matrix:

The “coordinates” of an element in a 2-dimensional object will be first the row (on the left of the comma), then the column (on the right of the comma):

rstudio logo

9.5.3 Matrix manipulation

  • Add 1 to all elements of a matrix
  • Multiply by 3 all elements of a matrix
  • Subtract 2 to each element of the first row of a matrix
  • Replace elements that comply a condition: