Active July 15, 2020 / Viewed 2099 / Comments 0 / Edit

Examples of how to create a constant matrix in python with numpy:

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]]`

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]]`

To generate a matrix of zeros there is numpy.zeros

`A = np.zeros((2,2))`

returns

`[[0. 0.]`

`[0. 0.]]`

Hi, I am Ben.

I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!

Did you find this content useful ?, If so, please consider donating a tip to the author(s). MoonBooks.org is visited by millions of people each year and it will help us to maintain our servers and create new contents.

Ads

License

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Ads

Ads