Examples of how to create a constant matrix in python with numpy:
Table of contents
Constant matrix
To create for example a constant matrix of size (2,2) with a constant 4.2 a solution is to use numpy.full
import numpy as np
A = np.full((2, 2), 4.2)
returns
[[4.2 4.2]
[4.2 4.2]]
Another example with size (3,3) and an integer 12:
A = np.full((3, 3), 12)
returns
[[12 12 12]
[12 12 12]
[12 12 12]]
Matrice of ones
To generate a matrix of ones there is numpy.ones
A = np.ones((2,2), dtype=np.uint8)
returns
[[1. 1.]
[1. 1.]]
Note: it is possible to specify the type numpy data types:
A = np.ones((2,2), dtype=np.uint8)
returns
[[1 1]
[1 1]]
Matrice of zeros
To generate a matrix of zeros there is numpy.zeros
A = np.zeros((2,2))
returns
[[0. 0.]
[0. 0.]]