• basic command for creating matrix:
    m <- matrix(data=c(1:20), nrow=4, ncol=5)
    

    This command creates matrix as array with only 2 dimensions. If you print this matrix R gives you response:

         [,1] [,2] [,3] [,4] [,5]
    [1,]    1    5    9   13   17
    [2,]    2    6   10   14   18
    [3,]    3    7   11   15   19
    [4,]    4    8   12   16   20
    
  • but array can have more dimensions:
    m2 <- array(c(1:40), dim=c(4,5,2))
    

    if we print it we see this:

    , , 1
    
         [,1] [,2] [,3] [,4] [,5]
    [1,]    1    5    9   13   17
    [2,]    2    6   10   14   18
    [3,]    3    7   11   15   19
    [4,]    4    8   12   16   20
    
    , , 2
    
         [,1] [,2] [,3] [,4] [,5]
    [1,]   21   25   29   33   37
    [2,]   22   26   30   34   38
    [3,]   23   27   31   35   39
    [4,]   24   28   32   36   40
    

functions for manipulation with matrixes:

  • nrow(), ncol() – number of rows/ columns
  • dim() – return dimensions
  • t() – transposition of the matrix
  • diag – return leading diagonal
  • sum(diag()) – sum of leading diagonal of the matrix
  • %*% – matrix multiplication