Instagram
youtube
Facebook
Twitter

Subtract Two NumPy Arrays Element-wise

Subtract Two NumPy Arrays Element-wise

Short Description of the Program:
This program performs element-wise subtraction of two NumPy arrays using the np.subtract() function.

Explanation:

  • np.array()
    Creates two arrays of the same shape with numerical values.

     
  • np.subtract(array1, array2)
    Subtracts each element of array2 from the corresponding element in array1.

     
  • print()
    Displays the input arrays and the result of the subtraction.

     

Program:

import numpy as np

array1 = np.array([[1, 2, 3], [4, 5, 6]])

array2 = np.array([[10, 20, 30], [40, 50, 60]])

result = np.multiply(array1, array2)

print("Array 1:\n", array1)

print("\nArray 2:\n", array2)

print("\nElement-wise Multiplication Result:\n", result)