Instagram
youtube
Facebook
Twitter

Element-wise Difference with Transpose in NumPy

A Element-wise Difference with Transpose in NumPy?
Code Explanation:
Library Import:
numpy is imported as np for array and numerical operations.
Array Creation: A 2D NumPy array arr is created with shape (2, 2).
Transpose: arr.T generates the transpose of the array, swapping rows with columns.
Element-wise Difference: arr - arr.T subtracts each element in the transposed array from the corresponding element in the original array.
Store Result: The difference is stored in the variable diff.

 

Program:

import numpy as np

arr = np.array([[1, 2], [3, 4]])

diff = arr - arr.T

print(diff)