Instagram
youtube
Facebook
Twitter

Concatenate Two NumPy Arrays Horizontally

A Concatenate Two NumPy Arrays Horizontally?
Code Explanation:
Library Import: NumPy is imported as np to access array functions.
Array Creation: Two 2D arrays a and b are created with shape (2, 2).
Horizontal Concatenation: np.hstack((a, b)) joins both arrays side-by-side (column-wise).
Result Storage: The result is stored in result, forming a shape of (2, 4).
Output Display: print(result) shows the final concatenated array.

 

Program:

import numpy as np

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

b = np.array([[5, 6], [7, 8]])

result = np.hstack((a, b))

print(result)