Python NumPy matlib.ones() Function

NumPy matlib.ones() Function:

The matlib.ones() function of the NumPy module returns a matrix of the specified shape and type, filled with ones.

Syntax:

numpy.matlib.ones(shape, dtype=None, order='C')

Parameters

shape: This is required. This is a tuple that specifies the shape of the matrix.

dtype: This is optional. The represents the data type of the matrix. float is the default value.

order: This is optional. It Specifies whether or not the result should be stored. C (C-style) and F (Fortran-style) are two possible values. ‘C’ is the default value. That means it is the matrix’s insertion order.

Return Value:

A matrix of ones with the specified shape, dtype, and order is returned.

NumPy matlib.ones() Function in Python

Example1

Approach:

  • Import numpy module using the import keyword.
  • Import matlib function of numpy module using the import keyword
  • Pass the shape of the matrix(as a tuple) as an argument to the matlib.ones() function of numpy module to create a matrix with ones of the given shape.
  • Store it in a variable.
  • Print the above-obtained matrix with ones of the given shape
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Import matlib function of numpy module using the import keyword
import numpy.matlib
# Pass the shape of the matrix(as a tuple) as an argument to the matlib.ones() function
# of numpy module to create a matrix with ones of the given shape
# Store it in a variable.
gvn_matrx = np.matlib.ones((3,3))
# Print the above obtained matrix with ones of the given shape
print("The above obtained matrix with ones of the given shape:")
print(gvn_matrx)

Output:

The above obtained matrix with ones of the given shape:
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]

Example2

The returned matrix will be a single row matrix of shape(1, N) if the shape has length one, i.e. (N,), or if the shape is a scalar N.

Approach:

  • Import numpy module using the import keyword.
  • Import matlib function of numpy module using the import keyword
  • Pass the shape of the matrix(here with only length one) as an argument to the matlib.ones() function of numpy module to create a matrix with ones of the given shape
  • Store it in a variable.
  • Print the above obtained first matrix.
  • Pass the shape of the matrix(here with only length one, ) as an argument to the matlib.ones() function of numpy module to create another matrix with ones of the given shape
  • Store it in another variable.
  • Print the above obtained second matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Import matlib function of numpy module using the import keyword
import numpy.matlib
# Pass the shape of the matrix(here with only length one) as an argument to the matlib.ones() function
# of numpy module to create a matrix with ones of the given shape
# Store it in a variable.
gvn_matrx1 = np.matlib.ones((3))
# Print the above obtained first matrix
print("The above obtained first matrix = ",gvn_matrx1)
# Pass the shape of the matrix(here length one,) as an argument to the matlib.ones() function
# of numpy module to create a matrix with ones of the given shape
# Store it in another variable.
gvn_matrx2 = np.matlib.ones((2,))
# Print the above obtained second matrix
print("The above obtained second matrix = ",gvn_matrx2)

Output:

The above obtained first matrix = [[1. 1. 1.]]
The above obtained second matrix = [[1. 1.]]

Example3

The dtype option can be used with the matlib.ones() function to specify the data type of the matrix’s elements.

# Import numpy module using the import keyword
import numpy as np
# Import matlib function of numpy module using the import keyword
import numpy.matlib
# Pass the shape of the matrix(as a tuple), datatype as complex as the arguments
# to the matlib.ones() function of numpy module to create a 
# matrix with ones of the given shape with complex values.
# Store it in a variable.
gvn_matrx = np.matlib.ones((3,3), dtype=complex)
# Print the above obtained matrix with ones of the given shape with complex values.
print("The above obtained matrix(complex) with ones of the given shape:")
print(gvn_matrx)

Output:

The above obtained matrix(complex) with ones of the given shape:
[[1.+0.j 1.+0.j 1.+0.j]
 [1.+0.j 1.+0.j 1.+0.j]
 [1.+0.j 1.+0.j 1.+0.j]]