Q:

Write a NumPy program to check whether the dimensions of two given arrays are same or not

0

Write a NumPy program to check whether the dimensions of two given arrays are same or not.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np
def test_array_dimensions(ar1,ar2):
 try:
   ar1 + ar2
 except ValueError:
   return "Different dimensions"
 else:
   return "Same dimensions"
ar1 = np.arange(20).reshape(4,5)
ar2 = np.arange(20).reshape(4,5)
print(test_array_dimensions(ar1, ar2))
ar1 = np.arange(20).reshape(5,4)
ar2 = np.arange(20).reshape(4,5)
print(test_array_dimensions(ar1, ar2))

Sample Output:

Same dimensions
Different dimensions

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now