I am writing a program to concatenate two numpy arrays and I want the program to print a message for each possible outcome (horizontal, vertical, or no concatenation). I have the following code and I don´t understand why even when the first condition (np.hstack) is met it continues evaluating the subsequent if and else statements and wrongly prints that there is both a horizontal concatenation (first condition is met) and that a concatenation is not possible.
import numpy as np
def fun1(a,b):
if a.shape[0] == b.shape[0]:
print("The horizontal concatenation is:", np.hstack((a,b)))
if a.shape[1] == b.shape[1]:
print("The vertical concatenation is:", np.vstack((a,b)))
else:
print("These arrays cannot be concatenated.")
a = np.floor(10*np.random.random((3,2)))
b = np.floor(10*np.random.random((3,4)))
fun1(a,b)
Output:
The horizontal concatenation is: [[5. 0. 1. 1. 3. 7.]
[4. 1. 8. 3. 1. 9.]
[9. 1. 5. 7. 0. 0.]]
These arrays cannot be concatenated.
elif. As it reads now you have two separateifstatements.