Creating a Matrix

A matrix is a rectangular array of numbers (or functions!) arranged in horizontal rows and vertical columns. An mxn matrix has m rows and n columns. A square matrix is one where n=m. The entries of a matrix A are written aij, where i denotes the row and j the column.

The diagonal of a matrix is the collection of entries aii.

You create a matrix in Mathematica by specifying its rows, also known as partitioning by rows. The rows are put in brackets with entries separated by commas. The rows are also separated by commas and the entire matrix is placed in brackets.

In[1]:=

  a= {{1, -1, 1}, {2, 0, 1}, {3, -1, 2}}

Out[1]=

  {{1, -1, 1}, {2, 0, 1}, {3, -1, 2}}

In order to see your matrix in the usual form, use the command MatrixForm. Note that Mathematica is CASE SENSITIVE, that is, the upper and lower case letters must be entered as shown.

In[2]:=

  MatrixForm[a]

Out[2]=

  1    -1   1
  
  2    0    1
  
  3    -1   2

In[3]:=

  Simplify[%]

Out[3]=

  {{1, -1, 1}, {2, 0, 1}, {3, -1, 2}}

In[4]:=

  b={{b11, b12, b13, b14},{b21, b22, b23, b24},{b31, b32, b33, b34},
    {b41, b42, b43, b44}}

Out[4]=

  {{b11, b12, b13, b14}, {b21, b22, b23, b24}, 
   
    {b31, b32, b33, b34}, {b41, b42, b43, b44}}

In[5]:=

  MatrixForm[b]

Out[5]=

  b11   b12   b13   b14
  
  b21   b22   b23   b24
  
  b31   b32   b33   b34
  
  b41   b42   b43   b44

Special matrices can also be entered. A diagonal matrix is a matrix where only the diagonal is non-zero. An identity matrix is the diagonal matrix with diagonal consisting of all 1's. An upper triangular matrix is a matrix whose nonzero entries lie above the diagonal. A lower triangular matrix is a matrix whose non-zero entries lie below the diagonal. The superdiagonal is the collection ai,i+1, the diagonal "directly above" the diagonal. A row vector is a 1xn matrix. A column vector is an mx1 matrix. Note the following examples.

In[6]:=

  DiagonalMatrix[{2, -3, 5}]

Out[6]=

  {{2, 0, 0}, {0, -3, 0}, {0, 0, 5}}

In[7]:=

  MatrixForm[%]

Out[7]=

  2    0    0
  
  0    -3   0
  
  0    0    5

The [%] applies the MatrixForm function to the previous calculation.

In[8]:=

  Table[If[i>=j, 1, 0], {i,5}, {j, 5}]

Out[8]=

  {{1, 0, 0, 0, 0}, {1, 1, 0, 0, 0}, {1, 1, 1, 0, 0}, 
   
    {1, 1, 1, 1, 0}, {1, 1, 1, 1, 1}}

In[9]:=

  MatrixForm[%]

Out[9]=

  1   0   0   0   0
  
  1   1   0   0   0
  
  1   1   1   0   0
  
  1   1   1   1   0
  
  1   1   1   1   1

In[10]:=

  Table[If[i<=j, 1, 0], {i,5}, {j, 5}]

Out[10]=

  {{1, 1, 1, 1, 1}, {0, 1, 1, 1, 1}, {0, 0, 1, 1, 1}, 
   
    {0, 0, 0, 1, 1}, {0, 0, 0, 0, 1}}

In[11]:=

  MatrixForm[%]

Out[11]=

  1   1   1   1   1
  
  0   1   1   1   1
  
  0   0   1   1   1
  
  0   0   0   1   1
  
  0   0   0   0   1

In[12]:=

  MatrixForm[IdentityMatrix[3]]

Out[12]=

  1   0   0
  
  0   1   0
  
  0   0   1

In[13]:=

  MatrixForm[IdentityMatrix[2]]

Out[13]=

  1   0
  
  0   1

Up to Mathematica and Linear Algebra