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.
![[Graphics:Images/index_gr_1.gif]](Images/index_gr_1.gif)
![[Graphics:Images/index_gr_2.gif]](Images/index_gr_2.gif)
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.
![[Graphics:Images/index_gr_3.gif]](Images/index_gr_3.gif)
![[Graphics:Images/index_gr_4.gif]](Images/index_gr_4.gif)
Element 4 is a graphics object, so it can be rendered using Show.
![[Graphics:Images/index_gr_5.gif]](Images/index_gr_5.gif)
![[Graphics:Images/index_gr_6.gif]](Images/index_gr_6.gif)
![[Graphics:Images/index_gr_7.gif]](Images/index_gr_7.gif)
Element 3 is a two-dimensional array.
![[Graphics:Images/index_gr_8.gif]](Images/index_gr_8.gif)
Return to Top
Creating Special Matrices
Mathematica contains some commands for quickly generating special matrices
such as identity or diagonal matrices.
![[Graphics:Images/index_gr_9.gif]](Images/index_gr_9.gif)
![[Graphics:Images/index_gr_10.gif]](Images/index_gr_10.gif)
![[Graphics:Images/index_gr_11.gif]](Images/index_gr_11.gif)
![[Graphics:Images/index_gr_12.gif]](Images/index_gr_12.gif)
Return to Top
Changing How Mathematica Displays
Arrays
Mathematica offers basically three ways to display lists:
MatrixForm,
TableForm, and the standard "list of lists" notation.
![[Graphics:Images/index_gr_13.gif]](Images/index_gr_13.gif)
![[Graphics:Images/index_gr_14.gif]](Images/index_gr_14.gif)
![[Graphics:Images/index_gr_15.gif]](Images/index_gr_15.gif)
![[Graphics:Images/index_gr_16.gif]](Images/index_gr_16.gif)
![[Graphics:Images/index_gr_17.gif]](Images/index_gr_17.gif)
![[Graphics:Images/index_gr_27.gif]](Images/index_gr_27.gif)
![[Graphics:Images/index_gr_28.gif]](Images/index_gr_28.gif)
These formatting commands also work for higher-dimensional arrays.
![[Graphics:Images/index_gr_29.gif]](Images/index_gr_29.gif)
![[Graphics:Images/index_gr_30.gif]](Images/index_gr_30.gif)
Return to Top
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.
![[Graphics:Images/index_gr_31.gif]](Images/index_gr_31.gif)
![[Graphics:Images/index_gr_32.gif]](Images/index_gr_32.gif)
![[Graphics:Images/index_gr_33.gif]](Images/index_gr_33.gif)
![[Graphics:Images/index_gr_34.gif]](Images/index_gr_34.gif)
![[Graphics:Images/index_gr_35.gif]](Images/index_gr_35.gif)
![[Graphics:Images/index_gr_36.gif]](Images/index_gr_36.gif)
![[Graphics:Images/index_gr_37.gif]](Images/index_gr_37.gif)
![[Graphics:Images/index_gr_38.gif]](Images/index_gr_38.gif)
![[Graphics:Images/index_gr_39.gif]](Images/index_gr_39.gif)
Return to Top
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.
![[Graphics:Images/index_gr_40.gif]](Images/index_gr_40.gif)
![[Graphics:Images/index_gr_41.gif]](Images/index_gr_41.gif)
![[Graphics:Images/index_gr_42.gif]](Images/index_gr_42.gif)
![[Graphics:Images/index_gr_43.gif]](Images/index_gr_43.gif)
Here [[All,1]]selects the first column.
![[Graphics:Images/index_gr_44.gif]](Images/index_gr_44.gif)
![[Graphics:Images/index_gr_45.gif]](Images/index_gr_45.gif)
Subarrays can be extracted using ranges as selection criteria.
![[Graphics:Images/index_gr_46.gif]](Images/index_gr_46.gif)
![[Graphics:Images/index_gr_47.gif]](Images/index_gr_47.gif)
For larger matrices, the Range command simplifies setting up the selection.
![[Graphics:Images/index_gr_48.gif]](Images/index_gr_48.gif)
![[Graphics:Images/index_gr_49.gif]](Images/index_gr_49.gif)
The command above is equivalent to the following.
![[Graphics:Images/index_gr_50.gif]](Images/index_gr_50.gif)
![[Graphics:Images/index_gr_51.gif]](Images/index_gr_51.gif)
Return to Top
Selecting Pieces of Arrays by Criterion
![[Graphics:Images/index_gr_52.gif]](Images/index_gr_52.gif)
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.
![[Graphics:Images/index_gr_53.gif]](Images/index_gr_53.gif)
![[Graphics:Images/index_gr_54.gif]](Images/index_gr_54.gif)
![[Graphics:Images/index_gr_55.gif]](Images/index_gr_55.gif)
![[Graphics:Images/index_gr_56.gif]](Images/index_gr_56.gif)
For higher dimensions, Select has to be mapped over the structure.
![[Graphics:Images/index_gr_57.gif]](Images/index_gr_57.gif)
![[Graphics:Images/index_gr_58.gif]](Images/index_gr_58.gif)
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.
![[Graphics:Images/index_gr_59.gif]](Images/index_gr_59.gif)
![[Graphics:Images/index_gr_60.gif]](Images/index_gr_60.gif)
![[Graphics:Images/index_gr_61.gif]](Images/index_gr_61.gif)
![[Graphics:Images/index_gr_62.gif]](Images/index_gr_62.gif)
For customized selection criteria, one can easily create patterns for
Position using the condition operator /;.
![[Graphics:Images/matrix_gr_1.gif]](Images/matrix_gr_1.gif)
![[Graphics:Images/matrix_gr_2.gif]](Images/matrix_gr_2.gif)
The following line returns the position of the largest element of
vec3.
![[Graphics:Images/matrix_gr_3.gif]](Images/matrix_gr_3.gif)
![[Graphics:Images/matrix_gr_4.gif]](Images/matrix_gr_4.gif)
The value can then be found by using Extract.
![[Graphics:Images/matrix_gr_5.gif]](Images/matrix_gr_5.gif)
![[Graphics:Images/matrix_gr_6.gif]](Images/matrix_gr_6.gif)
Return to Top
Changing Pieces of Lists
You can assign new values to list elements directly. Keep in mind that this changes the
original matrix in place.
![[Graphics:Images/index_gr_63.gif]](Images/index_gr_63.gif)
![[Graphics:Images/index_gr_64.gif]](Images/index_gr_64.gif)
![[Graphics:Images/index_gr_65.gif]](Images/index_gr_65.gif)
![[Graphics:Images/index_gr_66.gif]](Images/index_gr_66.gif)
Because elements of List do not have to have the same
dimensions, you can also assign values to whole sublists.
![[Graphics:Images/index_gr_67.gif]](Images/index_gr_67.gif)
![[Graphics:Images/index_gr_68.gif]](Images/index_gr_68.gif)
![[Graphics:Images/index_gr_69.gif]](Images/index_gr_69.gif)
![[Graphics:Images/index_gr_70.gif]](Images/index_gr_70.gif)
![[Graphics:Images/index_gr_71.gif]](Images/index_gr_71.gif)
![[Graphics:Images/index_gr_72.gif]](Images/index_gr_72.gif)
![[Graphics:Images/index_gr_73.gif]](Images/index_gr_73.gif)
Return to Top
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
Return to Top
|