## 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.

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.

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

Element 3 is a two-dimensional array.

 9 7 2 9 8 2

Creating Special Matrices

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

Changing How Mathematica Displays Arrays

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

These formatting commands also work for higher-dimensional arrays.

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.

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.

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

Subarrays can be extracted using ranges as selection criteria.

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

The command above is equivalent to the following.

Selecting Pieces of Arrays by Criterion

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.

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

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.

For customized selection criteria, one can easily create patterns for Position using the condition operator /;.

The following line returns the position of the largest element of vec3.

The value can then be found by using Extract.

Changing Pieces of Lists

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

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

Some Other Places to Look

• List Operations

Mathematica provides a large number of additional functions for list operations and structure manipulations such as 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 such as creating block matrices.

• Statistics`DataManipulation`

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

• Reading and Writing Tabular Data

• Image Processing