How to | Create a Matrix

Matrices are represented in Mathematica with lists. They can be entered directly with the { } notation, constructed from a formula, or imported from a data file. Mathematica also has commands for creating diagonal matrices, constant matrices, and other special matrix types.

A matrix can be entered directly with notation:

In[125]:=
Click for copyable input
Out[125]=

You can show the result in matrix notation with MatrixForm:

In[126]:=
Click for copyable input
Out[126]//MatrixForm=

is another way of entering . It can be convenient to use it when is a formatting function.

This uses Table to create a grid of values in and :

In[127]:=
Click for copyable input
In[129]:=
Click for copyable input
Out[129]//MatrixForm=

Note that matrices in Mathematica are not restricted to contain numbers; they can contain symbolic entries such as formulas:

In[130]:=
Click for copyable input
Out[131]//MatrixForm=
    

When you create a matrix and save it with an assignment, take care not to combine this with formatting using MatrixForm. Use parentheses:

In[132]:=
Click for copyable input
Out[132]//MatrixForm=

You can use in further calculations:

In[133]:=
Click for copyable input
Out[133]//MatrixForm=

Suppose you do not use parentheses:

In[134]:=
Click for copyable input
Out[134]//MatrixForm=

Then will print like a matrix but will not work in calculations like a matrix. For example, the following does not carry out matrix multiplication:

In[135]:=
Click for copyable input
Out[135]=

You can check the value of by using FullForm:

In[11]:=
Click for copyable input
Out[11]//FullForm=

This shows that also includes the formatting wrapper MatrixForm, which stops it from working as a matrix.

    

There are functions to create a variety of special types of matrices.

This creates a 4×5 matrix of real values that fall between and :

In[136]:=
Click for copyable input
Out[136]//MatrixForm=

This creates a matrix that only has nonzero entries on the diagonal:

In[137]:=
Click for copyable input
Out[137]//MatrixForm=

This creates a matrix whose entries are all the same:

In[138]:=
Click for copyable input
Out[138]//MatrixForm=

This creates a 4×4 Hilbert matrix; each entry is of the form :

In[139]:=
Click for copyable input
Out[139]//MatrixForm=
    

Many linear algebra and other functions return matrices.

Here, the QR decomposition of a random 3×3 matrix is calculated:

In[140]:=
Click for copyable input

This prints the Q matrix:

In[142]:=
Click for copyable input
Out[142]//MatrixForm=

When Mathematica functions return matrices they often use an optimized storage format called packed arrays.

    

You can apply many common operations in Mathematica to a list, and get back another list with the function mapped onto each element. This also works for matrices, which are lists of lists.

Here is a 2×2 matrix of squares:

In[143]:=
Click for copyable input
Out[144]//MatrixForm=

This applies Sqrt to each element of the matrix:

In[145]:=
Click for copyable input
Out[145]//MatrixForm=

This behavior of Sqrt is called listability, and it makes very readable and efficient code.

If a function that is not listable is used, it does not map onto each element:

In[146]:=
Click for copyable input
Out[146]=

You can make the function listable; now it will map onto each element:

In[147]:=
Click for copyable input
Out[148]//MatrixForm=
    

Another important way to create a matrix is to import a data file. This can be done with tabular formats such as Table (.dat), CSV (.csv), and TSV (.tsv). A matrix can also be read from an Excel spreadsheet (.xls).

Here, ImportString is used to import a CSV formatted string into a matrix. Importing from a file is done with Import:

In[149]:=
Click for copyable input
Out[149]=

Mathematica also supports a number of other formats including scientific and medical data formats such as HarwellBoeing, MAT, HDF, NASACDF, and FITS.

    

The way that you create a matrix can have an important impact on the efficiency of your programs. For the best efficiency, avoid appending to a matrix, avoid unnecessary creation operations, and use listable operations when you can.

This example repeatedly adds a new row to a matrix:

In[150]:=
Click for copyable input
Out[151]=

It is much faster to create the matrix in one computation. Whenever you see a For loop, try to replace it with some other construct, like Table:

In[152]:=
Click for copyable input
Out[152]=

The following example creates a matrix of zeros and then fills it in with a loop. The creation of a zero matrix here is completely unnecessary:

In[153]:=
Click for copyable input
Out[153]=

It is much faster to create data for each row of the matrix once, and then use a listable operation:

In[154]:=
Click for copyable input
Out[154]=

If your matrices are large and have many elements that are the same (for example, zero), then you should consider working with sparse matrices formed with SparseArray.

New to Mathematica? Find your learning path »
Have a question? Ask support »