How do I create and manipulate matrices?

In Mathematica, vectors, matrices and arrays of arbitrary dimensions are uniformly represented as lists or lists of lists. This prevents a lot of problems common in other technical computing software in which users have to work with and convert between many different representations. For example, Maple uses array, matrix, vector, table, set, randmatrix and so on.

Creating a simple matrix

The most straightforward way to create a vector, matrix or array of arbitrary dimensions in Mathematica is to use the Table command.

mat1 = Table[(Subscript[a, i, j]), {i, 1, 3}, {j, 1, 3}]

A Mathematica list can contain Mathematica objects of any kind and these objects do not have to have the same dimensions.For example, the following list contains a scalar, a vector, an array, a graphics object and two variables.

wiredlist = {1, {1, 2, 3}, {{9, 7, 2}, {9, 8, 2}}, 
          Plot[Sin[x], {x, 0, 1}], a, b^7};

Element 4 is a graphics object, so it can rendered using Show.

Show[wiredlist[[4]]]

Element 3 is a two dimensional array.

wiredlist[[3]] // TableForm

Creating special matrices

Mathematica contains some commands for quickly generating special matrices like identity or diagonal matrices.

IdentityMatrix[5] // MatrixForm

DiagonalMatrix[{a, b, c, d}] // MatrixForm

Changing how Mathematica displays arrays

Mathematica offers basically three ways to display lists: MatrixForm, TableForm, and the standard list-of-lists notation.

mat1 = Table[(Subscript[a, i, j]), {i, 1, 3}, {j, 1, 3}]

MatrixForm[mat1]

TableForm[mat1]

mat1

These formatting commands also work for higher dimensional arrays.

MatrixForm[
      Table[(Subscript[a, i, j, k]), {i, 1, 2}, {j, 1, 2}, {k, 1, 2}]]

Getting information about lists

A number of commands are available to query properties of lists. Some examples are below. More information about the functions and tensors in Mathematica is in section 3.7.11 of The Mathematica Book.

Dimensions[{{1, 2}, {3, 4}, {5, 6}}]

fourdim = 
        Table[(Subscript[a, i, j, k, l]), {i, 1, 2}, {j, 1, 2}, {k, 1, 2}, {l, 1, 
            2}];

Dimensions[fourdim]

TensorRank[fourdim]

Length[fourdim]

Selecting pieces of arrays by position

The most generic mechanism for selecting pieces of arrays is the Part command or its shortcut notation matrix[[index]]. For more information about Part and similar commands like Take and Drop, see The Mathematica Book, Sections 1.8.4, 2.1.5 and 3.8.4.

The following command selects the first row of the array mat1.

mat1 = Table[(Subscript[a, i, j]), {i, 1, 3}, {j, 1, 3}]

mat1[[1]]

Here [[All,1]]selects the first column

mat1[[All, 1]]

Subarrays can be extracted using ranges as selection criteria.

mat1[[{1, 2, 3}, {1, 2}]] // MatrixForm

For larger matrices, the Range command simplifies setting up the selection.

mat1[[Range[1, 3], {1, 2}]] // MatrixForm

The command above is equivalent to

mat1[[{1, 2, 3}, {1, 2}]] // MatrixForm

Selecting pieces of arrays by criterion

mat2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

The standard command for selecting pieces of lists based on criteria is Select. Selection criteria are usually defined as pure functions. For vectors or one dimensional-lists, Select can be called directly.

vec1 = Range[10]

Select[vec1, EvenQ]

For higher dimensions, Select has to be mapped over the structure.

(Select[#, EvenQ] &) /@ mat2

Alternatively,  Position returns the indices of the entries fitting the criterion. Extract can then be used to extract the indexed elements. Note that while Select uses a pure function, Position looks for a pattern.

evenpos = Position[mat2, x_?EvenQ]

Extract[mat2, evenpos]

For customized selection criteria, you can easily create your own patterns using the condition operator /;.

vec3  =  {1, 3, 2, 12, 1}

maxi = Position[vec3, x_ /; x == Max[vec3]]

Extract[vec3, maxi]

Changing pieces of lists

You can assign new values to list elements directly. Keep in mind that this changes the original matrix in place.

mat2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

mat2[[1, 1]] = 3;

mat2

Because elements of List do not have to have the same dimensions, you can also assign values to whole sublists.

mat2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

mat2[[1]] = 3;

mat2

mat2[[1]] = {1, 2, 3};

mat2

Some other places to look

ListOperations

Mathematica provides a large number of additional functions for list operations and structure manipulations like partitioning, To learn more about them, read Section 1.8 of The Mathematica Book.

LinearAlgebra`MatrixManipulation`

The LinearAlgebra`MatrixManipulation` package contains a number of simple utility functions for matrix manipulations, like creating block matrices.

Statistics`DataManipulation

The Statistics`DataManipulation` package contain some functions that might also be very helpful for general matrix manipulations.

Reading and writing tabular data

See import and export

Image processing

See Image Processing