Python Numpy matrix.put() Function

NumPy Library 

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it into our program and then we can easily use the functionality of NumPy in our program.

NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype’s values.

Numpy matrix.put() Function:

Using the matrix.put() method of the Numpy module, we can put(insert) the value by passing index and value in a given particular matrix.

Syntax:

 matrix.put(index, value)

Return Value:

A new matrix (after putting values ) is returned by the put() function.

Numpy matrix.put() Function in Python

For 2-Dimensional (2D) Matrix (Inserting an element at Multiple Indices)

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing some random 2D matrix as an argument to it and store it in a variable
  • Pass the index/indices(as a tuple) and value as arguments to the put() function and apply it to the given matrix
  • Here we passed multiple indices and the value gets inserted at corresponding indices of the tuple.
  • Store it in another variable
  • Print the given matrix after inserting an element at the multiple indices.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing 
# some random 2D matrix as arguments to it and store it in a variable
gvn_matrx = np.matrix('[2, 1; 6, 3]')
            
# Pass the index/indices(as a tuple) and value as an argument to the put() function and apply it to the given matrix
# Here we passed multiple indices and the value gets inserted at corresponding indices of the tuple.
# Store it in another variable
gvn_matrx.put((0, 2), 50)
# Print the given matrix after inserting an element at the multiple indices
print("The given matrix after inserting an element at the multiple indices:")
print(gvn_matrx)

Output:

The given matrix after inserting an element at the multiple indices:
[[50 1]
[50 3]]

Explanation:

Here it inserts 50 at the indices 0 and 2.

NOTE:

The matrix indices starts from '0'

For 3-Dimensional (3D) Matrix (Inserting an element at Single Index)

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing some random 3D matrix as an argument to it and store it in a variable
  • Pass the index and value as arguments to the put() function and apply it to the given matrix
  • Here we passed a single index and the value gets inserted at that corresponding index.
  • Store it in another variable
  • Print the given matrix after inserting an element at the given index.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing 
# some random 3D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 4, 1; 8, 7, 3; 10, 9, 5]')
            
# Pass the index and value as arguments to the put() function and apply it to the given matrix
# Here we passed single index and the value gets inserted at that corresponding index.
# Store it in another variable
gvn_matrx.put((4), 100)
# Print the given matrix after inserting an element at the given index.
print("The given matrix after inserting an element {100} at the given index{4}:")
print(gvn_matrx)

Output:

The given matrix after inserting an element {100} at the given index{4}:
[[ 2 4 1]
 [ 8 100 3]
 [ 10 9 5]]