This section is devoted to very important practical techniques how to extract submatrices and how to build matrices from smaller matrices. Although all presented examples and codes are applied to matrices from space 𝔽m×n with entries from field 𝔽 (which is either ℤ or ℚ or ℝ or ℂ), all these matrix operations can be extended for abstract matrices where entries could be arbitrary algebraic objects including lists.

(a*b)/c+13*d
\[ {\frac {ab}{c}}+13\,d \]

Manipulations with vectors

Usually, an n-tuple (x₁, x₂, … , xn), written in parenthesis and a row vector [x₁, x₂, … , xn], written in brackets, look as the same object to human eyes. One of the pedagogical virtues of any software package is its requirement to pay close attention to the types of objects used in specific contexts. In particular, the computer algebra system Mathematica treats these two versions of a vector differently because

\[ \left( x_1 , x_2 , \ldots , x_n \right) \in \mathbb{F}^n , \]
but
\[ \left[ x_1 , x_2 , \ldots , x_n \right] \in \mathbb{F}^{n\times 1} , \]
where x [bold] is a matrix and 𝔽m×n is read “m by n” indicating dimensions of a (rows by columns) matrix, not m multiplied by n. This denotes the space of m × n matrices with coefficients from field 𝔽.

In Maple, vectors are not just lists of elements. Maple separates the idea of the mathematical object Vector from the data object Array. Row vector can be defined as follows:

Vector[row]([1, 2, 3])
\[ \begin{bmatrix} 1 & 2 & \end{bmatrix} \]
or
x := <4| 5| 6>
\[ x := \begin{bmatrix} 3 & 4 & 6 \end{bmatrix} \]
Column vector is entered in as
Vector[column]([1, 2, 3])
\[ \begin{bmatrix} 1 \\ 2 \\ 3 \end{bmatrix} \]
or
x := <4, 5, 6>
\[ x := \begin{bmatrix} 4 \\ 5 \\ 6 \end{bmatrix} \]
Mathematica considers single curly bracket notation as a matrix with one column and a double curly bracket notation as a matrix with one row.
x={1,2};(*one column matrix*)​​
y={{1,2}};(*one row matrix*)​​
z={{1},{2}};(*one column matrix*)​​
TrueQ[x==y]
False
TrueQ[x==z]
False
MatrixForm[x]
\( \displaystyle \quad \begin{pmatrix} 1 \\ 2 \end{pmatrix} \)
MatrixForm[y]
\( \displaystyle \quad (1, \ 2) \)
MatrixForm[z]
\( \displaystyle \quad \begin{pmatrix} 1 \\ 2 \end{pmatrix} \)
Extracting entries from vectors:
x[[1]] (* extracting the first component *)
1
y[[1]]
{1, 2}
y[[1, 2]] (* extracting the first component *)
2
z[[1]]
{1}
z[[2, 1]]
2

converting n-tuples into row vectors, into columns into diagonal matrix and vice versa    

Example 1:    ■
End of Example 1

extracting subvector

2) reverse order of entries

3) building subvector with even or odd entries and vector with third values as [1, 3, 6, 9, ..]

4) random entries

Roger: Making another exanle ?

We can multiply vectors, but the output depends on the irder of the vectors and their dimensions. There known three types of products. We start with the outer product of two vectors of arbitrary dimensions. Since the outer product of two vectors is a rectangular matrix, it does not matter to what space these vectors belong. However, it is convenient to consider one vector as a row, but another as a column.

The outer product (also known as tensor product) of two vectors x ∈ ℝm×1 and y ∈ ℝ1×n is their matrix product whose entries are all products of an element in the first vector with an element from the second vector:

\[ {\bf x} \otimes {\bf y} = \left[ \begin{array}{c} x_1 \\ x_2 \\ \vdots \\ x_m \end{array} \right] \,\begin{bmatrix} y_1 & y_2 & \cdots & y_n \end{bmatrix} = \begin{bmatrix} x_1 y_1 & x_1 y_2 & \cdots & x_1 y_n \\ x_2 y_1 & x_2 y_2 & \cdots & x_2 y_n \\ \vdots & \vdots & \ddots & \vdots \\ x_m y_1 & x_m y_2 & \cdots & x_m y_n \end{bmatrix} \in \mathbb{F}^{m\times n} . \]
The dot product of two vectors of the same size is the number
\[ \begin{bmatrix} x_1 & x_2 & \cdots & x_n \end{bmatrix} \bullet \begin{bmatrix} y_1 & y_2 & \cdots & y_n \end{bmatrix} = x_1 y_1 + \cdots + x_n y_n . \]
DotProduct[x,y]
The Hadamard product of two vectors of the same size is the vector

Manipulations with Matrices

Extracting Blocks from Matrices

1) extract rows and columns

2) extract particular entries

Building Matrices from blocks

Example 2: Let us consider the 3-by-4 matrix 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}}]
Swapping rows or columns. Swap row 1 and row 3:
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 cut off some rows or/and columns. If one wants to delete, say 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,5 and column 4. Note 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 sum of diagonal elements (which is called trace), enter:
Total[Diagonal[mat]]
Out[13]= 12
   ■
End of Example 2

Building Matrices from Blocks

Example 3: Let us consider the 3-by-4 matrix    ■
End of Example 3

 


  1. Axier, S., Linear Algebra Done Right. Undergraduate Texts in Mathematics (3rd ed.). Springer. 2015, ISBN 978-3-319-11079-0.
  2. Beezer, R.A., A First Course in Linear Algebra, 2017.