Overview:
- The matmul() function of NumPy module multiplies two n-dimensional matrices and returns the product of the two matrices.
Example 1 - Muliplication of two 2-dimensional matrices using matmul():
# Example Python program that multiplies two-dimensional matrices using # Create ndarrays of numbers n2 = numpy.ndarray(shape = (3, 3), # Populate the numbers # Matrix multiplication of two-dimensional numpy |
Output:
[[210 128 138] [244 173 237] [314 208 258]] |
Example 2: Multiplying an 3-dimensional matrix with a 2-dimensional matrix
# Example Python program that uses matmul() function of import numpy # Create a 3-dimensional matrix # Create a 2-dimensional matrix # Multiply the two matrices |
Output:
A 3-dimensional matrix: [[[ 0 2 4] [ 6 8 10] [12 14 16]]
[[18 20 22] [24 26 28] [30 32 34]]
[[36 38 40] [42 44 46] [48 50 52]]] A 2-dimensional matrix: [[0 1 2] [3 4 5] [6 7 8]] Result of multiplying a 3-dimensional matrix with a 2-dimensional matrix: [[[ 30 36 42] [ 84 108 132] [138 180 222]]
[[192 252 312] [246 324 402] [300 396 492]]
[[354 468 582] [408 540 672] [462 612 762]]] |