Creating Matrices in Mathematica

A matrix is an array of numbers arranged in rows and columns. In Mathematica, matrices are expressed as a list of rows, each of which is a list itself. It means a matrix is a list of lists. If a matrix has n rows and m columns then we call it an n by m matrix. The value(s) in the ith row and jth column is called the i, j entry.

In Mathematica, matrices can be entered with the { } notation, constructed from a formula, or imported from a data file. There are also commands for creating diagonal matrices, constant matrices, and other special matrix types.

Creating matrices in Mathematica

  • Create a matrix using { } notation
    mat={{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
    but output will not be in matrix form, to get in matrix form use command like
  • Creating matrix using Table command
    mat1=Table[b{row, column},
    {row, 1, 4, 1}, {column, 1, 2, 1}]
  • Creating symbolic matrix such as
    mat2=Table[xi+xj , {i, 1, 4}, {j, 1, 3}]
  • Creating a diagonal matrix with nonzero entries at its diagonal
    DiagonalMatrix[{1, 2, 3, r}]//MatrixForm
  • Creating a matrix with same entries i.e. a constant matrix
    ConstantArray[3, {2, 4}]//MatrixForm
  • Creating an identity matrix of order n × n

Matrix Operations in Mathematica

In Mathematica, matrix operations can be performed on both numeric and symbolic matrices.

  • To find the determinant of a matrix
  • To find the transpose of a matrix
  • To find the inverse of a matrix for linear system
  • To find the Trace of a matrix i.e. sum of diagonal elements in a matrix
  • To find Eigenvalues of a matrix
  • To find Eigenvector of a matrix
  • To find both Eigenvalues and Eigenvectors together

Note that +, *, ^ operators all automatically work element-wise.

Displaying matrix and its elements

  • mat[[1]]         displays the first row of a matrix where mat is a matrix create above
  • mat[[1, 2]]     displays the element from first row and second column, i.e. m12 element of the matrix
  • mat[[All, 2]]  displays the 2nd column of matrix