One of the attractions of using Python from Excel is to gain access to the vast range of publicly available Python libraries for numerical computing. Since NumPy is the cornerstone of many of these libraries, the ExcelPython add-in makes it easy to pass function arguments as and convert return values from numpy arrays.
We will now define a simple function for doing matrix multiplication using NumPy.
-
Add the following code to
Book1.py
from the first tutorial@xlfunc @xlarg("x", "nparray", dims=2) @xlarg("y", "nparray", dims=2) def MatrixMult(x, y): return x.dot(y)
-
Click 'Import Python UDFs' to pick up the changes
-
The function
MatrixMult
can now be used as an array function from ExcelTo enter the above array formula in Excel
- fill in the values in the ranges
D1:E2
andG1:H2
- select cells
A1:B2
- type in the formula
=MatrixMult(D1:E2, G1:H2)
- press
Ctrl+Shift+Enter
.
- fill in the values in the ranges
To continue move onto the next tutorial.