Home »
Python »
Linear Algebra using Python
Ones matrix using numpy.ones() | Linear Algebra using Python
Linear Algebra using Python | Ones Matrix using numpy.ones(): Here, we are going to learn about creating ones matrix using numpy.ones() in Python.
Submitted by Anuj Singh, on May 29, 2020
Ones Matrix - When all the entries of a matrix are one, then it is called as ones matrix. It may be of any dimension (MxN).
Properties:
- The determinant of the matrix is 1 (when the number of columns and rows are 1, i.e. there is only one element in the matrix) or 0 otherwise.
- The Rank of any Ones Matrix is 1.
In python, we have an inbuilt function (defined in numpy library) numpy.ones() to define the ones matrix. Here is the code with examples.
Python code for ones matrix using numpy.ones()
# Linear Algebra Learning Sequence
# Ones matrix using numpy.ones()
import numpy as np
a = np.ones([3,5])
b = np.ones([6,6])
c = np.ones([9,5])
#printing the matrices
print("\n----Ones Matrix (3x5)----\n",a)
print("\n----Ones Matrix (6x6)----\n",b)
print("\n----Ones Matrix (9x5)----\n",c)
Output:
----Ones Matrix (3x5)----
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
----Ones Matrix (6x6)----
[[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]]
----Ones Matrix (9x5)----
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]