Manipulation of Matrices
This section is devoted to basic manipulation with matrices.
For a given m×n matrix A, its transpose is the
n×m matrix, denoted either by
AT or
by At or just by A′, whose entries are formed by interchanging the rows with
the columns; that is, (A′)i,j=(A′)j,i.
Example: Let us consider the 3-by-4 matrix
[342219127287162122696942089].
Its transpose will be
[347269228769191624201212289].
Mathematica confirms
A = {{34, 22, 19, 12}, {72, 87, 162, 122}, {69, 69, 420, 89}}
Transpose[A] // MatrixForm
Out[2]= (347269228769191624201212289)
Theorem: Let A and B denote matrices whose sizes are appropriate for the following operations.
- (AT)T=A
- (A+B)T=AT+BT
- For any scalar s, (sA)T=sAT.
- (AB)T=BTAT.
- If A is a square matrix, then tr(AT)=tr(A).
- If A is a square nonsingular matrix, then (AT)−1=(A−1)T.
- If A is a square matrix, then det(AT)=det(A).
First we generate 3-by-4 matrix:
m = Range@12~Partition~4;
m // MatrixForm
Out[2]= (123456789101112)
Insert a column at position 2:
v = Range[21, 23];
Insert[m // Transpose, v, 2] // Transpose // MatrixForm
Out[4]= (121234522678923101112)
Extract row 3:
m[[3]]
Out[5]= {9, 10, 11, 12}
Extract column 2
m[[All, 2]] // MatrixForm
Out[6]= (2610)
Insert a row at position 2:
v = Range[30, 33];
Insert[m, v, 2] // MatrixForm
Out[8]= (12343031323356789101112)
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]= (1244561289102012)
Now row 2 = row 2 + row 3:
m2 = m;
m2[[2]] += m2[[3]];
m2 // MatrixForm
Out[8]= (1234141618209101112)
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]= (910111256781234)
Swap column 1 and 3:
m2[[All, {1, 3}]] = m2[[All, {3, 1}]];
m2 // MatrixForm
Out[13]= (111091276583214)
Multiply row 2 with 3:
m*{1, 3, 1} // MatrixForm
Out[14]= (1234151821249101112)
Multiply column 1 with 4:
((m // Transpose)*{4, 1, 1, 1}) // Transpose // MatrixForm
Out[15]= (42342067836101112)
Replace a block of a matrix:
mat = RandomInteger[10, {5, 5}]
mat // MatrixForm
Out[2]= (3810719510713604410323678938)
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]= (00001000010000410323678938)
To pick out a submatrix, you can use Span (;;). First, define a 4�5 matrix:
mat = RandomInteger[10, {4, 5}]
mat // MatrixForm
Out[6]= (318500086880010044898)
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]= (3185008680010)
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]= (3311099934381039081160)
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]= (3319810308110)
Extract all elements except the outermost rows and columns (negative indices count from the end):
mat // MatrixForm
mat[[2 ;; -2, 2 ;; -1]]
Out[9]= (318500086880010044898)
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