Instagram
youtube
Facebook
Twitter

Element-wise Sum with Transpose in NumPy

A Element-wise Sum with Transpose in NumPy?
Code Explanation:
Library Import:
numpy is imported as np to work with arrays and numerical operations.
Array Creation: A 2D NumPy array arr is created with shape (2, 2).
Transpose: arr.T calculates the transpose of the array by switching its rows and columns.
Element-wise Sum: arr + arr.T adds each element of the array to the corresponding element in the transposed array.
Store Result: The sum is stored in the variable sum_result.

 

Program:

import numpy as np

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

sum_result = arr + arr.T

print(sum_result)