Manipulation of Matrices
This section is devoted to basic manipulation with matrices.
For a given \( m \times n \) matrix A, its transpose is the
\( n \times m \) matrix, denoted either by
\( {\bf A}^T \) or
by At or just by \( {\bf A}' , \) whose entries are formed by interchanging the rows with
the columns; that is, \( \left( {\bf A}' \right)_{i,j} = \left( {\bf A}' \right)_{j,i} . \)
Example: Let us consider the 3-by-4 matrix
\[
\begin{bmatrix}
34 &22& 19& 12 \\
72& 87& 162& 122 \\
69& 69& 420& 89 \end{bmatrix} .
\]
Its transpose will be
\[
\begin{bmatrix}
34 & 72 & 69 \\
22& 87 & 69 \\
19& 162 & 420 \\
12 & 122 & 89 \end{bmatrix} .
\]
Mathematica confirms
A = {{34, 22, 19, 12}, {72, 87, 162, 122}, {69, 69, 420, 89}}
Transpose[A] // MatrixForm
Out[2]= \( \displaystyle \quad
\begin{pmatrix} 34& 72&69 \\ 22& 87& 69 \\ 19& 162&420 \\
12& 122& 89 \end{pmatrix} \)
Theorem: Let A and B denote matrices whose sizes are appropriate for the following operations.
- \( \left( {\bf A}^{\mathrm T} \right)^{\mathrm T} = {\bf A} \)
- \( \left( {\bf A} + {\bf B} \right)^{\mathrm T} = {\bf A}^{\mathrm T} + {\bf B}^{\mathrm T} \)
- For any scalar s, \( \left( s{\bf A} \right)^{\mathrm T} = s\,{\bf A}^{\mathrm T} .\)
- \( \left( {\bf A}\,{\bf B} \right)^{\mathrm T} = {\bf B}^{\mathrm T} {\bf A}^{\mathrm T} . \)
- If A is a square matrix, then \( \mbox{tr}\left( {\bf A}^{\mathrm T} \right) = \mbox{tr}\left( {\bf A} \right) . \)
- If A is a square nonsingular matrix, then \( \left( {\bf A}^{\mathrm T} \right)^{-1} = \left( {\bf A}^{-1} \right)^{\mathrm T} . \)
- If A is a square matrix, then \( \det\left( {\bf A}^{\mathrm T} \right) = \det\left( {\bf A} \right) . \)
First we generate 3-by-4 matrix:
m = Range@12~Partition~4;
m // MatrixForm
Out[2]= \( \displaystyle \quad
\begin{pmatrix} 1& 2& 3& 4 \\ 5& 6& 7& 8 \\
9& 10& 11& 12 \end{pmatrix} \)
Insert a column at position 2:
v = Range[21, 23];
Insert[m // Transpose, v, 2] // Transpose // MatrixForm
Out[4]= \( \displaystyle \quad
\begin{pmatrix} 1& 21&2& 3& 4 \\ 5& 22&6& 7& 8 \\
9& 23&10& 11& 12 \end{pmatrix} \)
Extract row 3:
m[[3]]
Out[5]= {9, 10, 11, 12}
Extract column 2
m[[All, 2]] // MatrixForm
Out[6]= \( \displaystyle \quad
\begin{pmatrix} 2 \\ 6 \\ 10 \end{pmatrix} \)
Insert a row at position 2:
v = Range[30, 33];
Insert[m, v, 2] // MatrixForm
Out[8]= \( \displaystyle \quad
\begin{pmatrix} 1&2&3&4 \\ 30&31&32&33 \\ 5&6&7&8 \\ 9&10&11&12 \end{pmatrix} \)
Adding two rows or columns. First, we add column 3 = column 3 + column 1:
m2 = m;
m2[[All, 3]] += m2[[All, 1]];
m2 // MatrixForm
Out[8]= \( \displaystyle \quad
\begin{pmatrix} 1&2&4&4 \\ 5&6&12&8 \\ 9&10&20&12 \end{pmatrix} \)
Now row 2 = row 2 + row 3:
m2 = m;
m2[[2]] += m2[[3]];
m2 // MatrixForm
Out[8]= \( \displaystyle \quad
\begin{pmatrix} 1&2&3&4 \\ 14&16&18&20 \\ 9&10&11&12 \end{pmatrix} \)
MatrixPlot[{{1, 2, 3, 4}, {14, 16, 18, 20}, {9, 10, 11, 12}}]
m2 = m;
m2[[{1, 3}]] = m2[[{3, 1}]];
m2 // MatrixForm
Out[11]= \( \displaystyle \quad
\begin{pmatrix} 9&10&11&12 \\ 5&6&7&8 \\ 1&2&3&4 \end{pmatrix} \)
Swap column 1 and 3:
m2[[All, {1, 3}]] = m2[[All, {3, 1}]];
m2 // MatrixForm
Out[13]= \( \displaystyle \quad
\begin{pmatrix} 11&10&9&12 \\ 7&6&5&8 \\ 3&2&1&4 \end{pmatrix} \)
Multiply row 2 with 3:
m*{1, 3, 1} // MatrixForm
Out[14]= \( \displaystyle \quad
\begin{pmatrix} 1&2&3&4 \\ 15&18&21&24 \\ 9&10&11&12 \end{pmatrix} \)
Multiply column 1 with 4:
((m // Transpose)*{4, 1, 1, 1}) // Transpose // MatrixForm
Out[15]= \( \displaystyle \quad
\begin{pmatrix} 4&2&3&4 \\ 20&6&7&8 \\ 36&10&11&12 \end{pmatrix} \)
Replace a block of a matrix:
mat = RandomInteger[10, {5, 5}]
mat // MatrixForm
Out[2]= \( \displaystyle \quad
\begin{pmatrix} 3&8&10&7&1 \\ 9&5&10&7&1 \\ 3&6&0&4&4 \\ 10&3&2&3&6 \\
7&8&9&3&8 \end{pmatrix} \)
Update the 3-by-4 submatrix by using the short form of Span (;;)
to specify the relevant span of rows and columns:
mat[[1 ;; 3, 1 ;; 4]] = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
mat // MatrixForm
Out[4]= \( \displaystyle \quad
\begin{pmatrix} 0&0&0&0&1 \\ 0&0&0&0&1 \\ 0&0&0&0&4 \\ 10&3&2&3&6 \\
7&8&9&3&8 \end{pmatrix} \)
To pick out a submatrix, you can use Span (;;). First, define a 4�5 matrix:
mat = RandomInteger[10, {4, 5}]
mat // MatrixForm
Out[6]= \( \displaystyle \quad
\begin{pmatrix} 3&1&8&5&0 \\ 0&0&8&6&8 \\ 8&0&0&10&0 \\ 4&4&8&9&8
\end{pmatrix} \)
Extract the top-left 3�4 submatrix by using Span (;;) to specify the relevant
span of rows and columns:
mat[[1 ;; 3, 1 ;; 4]] // MatrixForm
Out[8]= \( \displaystyle \quad
\begin{pmatrix} 3&1&8&5 \\ 0&0&8&6 \\ 8&0&0&10
\end{pmatrix} \)
You can get the same block from a matrix with another command:
Take[mat, {1, 3}, {1, 4}]
Another option is to cut off some rows or/and columns. If you want to delete, for example, the second row, type:
Drop[mat, {2}]
or
Delete[mat, 2]
If one wants to delete the first two rows, type:
Drop[mat, 2]
Now suppose you want to remove certain rows and columns from a matrix:
remove[a_?MatrixQ, row_?VectorQ, col_?VectorQ] :=
Module[{nr, nc, krow, kcol},
{nr, nc} = Dimensions[a];
krow = Complement[Range[1, nr], row];
kcol = Complement[Range[1, nc], col];
a[[krow, kcol]]
]
As an example, consider a 4×5 matrix:
b = RandomInteger[{0, 10}, {4, 5}];
MatrixForm[b]
Out[4]= \( \displaystyle \quad
\begin{pmatrix} 3&3&1&10&9 \\ 9&9&3&4&3 \\ 8&10&3&9&0 \\ 8&1&1&6&0
\end{pmatrix} \)
Here we attempt to remove rows 2 and 5 and column 4.
Note that there is no row 5, so the command disregards it.
remove[b, {2, 5}, {4}] // MatrixForm
Out[5]= \( \displaystyle \quad
\begin{pmatrix} 3&3&1&9 \\ 8&10&3&0 \\ 8&1&1&0
\end{pmatrix} \)
Extract all elements except the outermost rows and columns (negative indices count from the end):
mat // MatrixForm
mat[[2 ;; -2, 2 ;; -1]]
Out[9]= \( \displaystyle \quad
\begin{pmatrix} 3&1&8&5&0 \\ 0&0&8&6&8 \\ 8&0&0&10&0 \\ 4&4&8&9&8
\end{pmatrix} \)
Out[10]= {{0, 8, 6, 8}, {0, 0, 10, 0}}
Extract diagonal elements:
Diagonal[mat]
Out[11]= {3, 0, 0, 9}
Diagonal[mat, 2]
Out[12]= {8, 6, 0}
To find the sum of diagonal elements (which is called trace), enter:
Total[Diagonal[mat]]
Out[13]= 12