Time for action – creating a matrix from other matrices
We will create a matrix from two smaller matrices as follows:
- First, create a 2-by-2 identity matrix:
A = np.eye(2) print("A", A)
The identity matrix looks like the following:
A [[ 1. 0.] [ 0. 1.]]
- Create another matrix like
A
and multiply it by 2:B = 2 * A print("B", B)
The second matrix is as follows:
B [[ 2. 0.] [ 0. 2.]]
- Create the compound matrix from a string. The string uses the same format as the
mat()
function—use matrices instead of numbers:print("Compound matrix\n", np.bmat("A B; A B"))
The compound matrix is shown as follows:
Compound matrix [[ 1. 0. 2. 0.] [ 0. 1. 0. 2.] [ 1. 0. 2. 0.] [ 0. 1. 0. 2.]]
What just happened?
We created a block matrix from two smaller matrices with ...
Get NumPy : Beginner's Guide - Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.