How to use the MMULT function
What is the MMULT function?
The MMULT function calculates the matrix product of two arrays, an array as the same number of rows as array1 and columns as array2.
Table of Contents
- Syntax
- Spilling - Excel 365 dynamic array formulas
- Example 1 - cell ranges with an equal number of rows and columns
- Example 2 - cell ranges with different number of rows and columns
- Example 3 - cell ranges with different numbers of rows and columns
- Example 4 - calculate totals row-wise
- Example 5 - calculate totals column-wise
- Function not working
- Get Excel *.xlsx file
1. Syntax
MMULT(array1, array2)
array1 | Required. The first array of numbers you want to multiply. |
array2 | Required. The second array of numbers you want to multiply. |
This function must be entered as an array formula.
2. Excel 365 dynamic arrays - spill values
Microsoft Excel has recently released an update to Excel 365 subscribers which allows you to use a new feature called dynamic array that replaces a regular array formula.
The image above shows the MMULT function in cell B12, however, it has been entered as a regular formula. Excel "knows" that it returns multiple values and automatically expands the range.
This is called "spilled array behavior" meaning the formula expands across cells automatically in order to show all values.
There is a blue border around the values from a spilled array but it will disappear as soon as you press with left mouse button on a cell outside the blue border. It reappears again if you press with left mouse button on any cell in the spilled array.
The following examples demonstrate how the function works.
3. Example 1
This example demonstrates cell ranges with an equal number of rows and columns.
Array1 (B2:C3) has 2 rows and array2 (E2:F3) has 2 columns, the returning array has 2 rows and 2 columns. Cell range B2:C3 and E2:F3 show the cell labels. Cell range H2:I3 shows the calculations made by the MMULT function in greater detail for each value in the array using the labels in B2:C3 and E2:F3.
Array formula in cell range L8:M9:
Cell L8: a11*b11+a12*b21 becomes 0*0+1*1 and returns 1.
Cell L9: a21*b11+a22*b21 becomes 1*0+0*1 and returns 0.
Cell M8: a11*b12+a12*b22 becomes 0*1+1*0 and returns 0.
Cell M9: a21*b12+a22*b22 becomes 1*1+0*0 and returns 1.
4. Example 2
This example demonstrates cell ranges with different numbers of rows and columns. Array1 (B2:C4) has 3 rows and array2 (E2:G3) has 3 columns, the returning array has 3 rows and 3 columns.
Array formula in cell range L8:M9:
Cell M8: a₁₁*b₁₁+a₁₂*b₂₁ becomes 0*1+1*4 and returns 4
Cell M9: a₂₁*b₁₁+a₂₂*b₂₁ becomes -4*1+0*4 and returns -4
Cell M10: a₃₁*b₁₁+a₃₂*b₂₁ becomes 3*1+1*4 and returns 7
Cell N8: a₁₁*b₁₂+a₁₂*b₂₂ becomes 0*2+1*-5 and returns -5
Cell N9: a₂₁*b₁₂+a₂₂*b₂₂ becomes -4*2+0*-5 and returns -8
Cell N10: a₃₁*b₁₂+a₃₂*b₂₂ becomes 3*2+1*-5 and returns 1
Cell O8: a₁₁*b₁₃+a₁₂*b₂₃ becomes 0*3+1*-6 and returns -6
Cell O9: a₂₁*b₁₃+a₂₂*b₂₃ becomes -4*3+0*-6 and returns -12
Cell O10: a₃₁*b₁₃+a₃₂*b₂₃ becomes 3*3+1*-6 and returns 3
5. Example 3
This example demonstrates cell ranges with different numbers of rows and columns, example 2. Array1 (B2:D3) has 2 rows and array2 (F2:G4) has 2 columns, the returning array has 2 rows and 2 columns.
Array formula in cell range L8:M9:
=MMULT(B8:D9,F8:G10)
Cell L8: a₁₁*b₁₁+a₁₂*b₂₁+a₁₃*b₃₁ becomes 1*0+2*-4+3*3 and returns 1
Cell L9: a₂₁*b₁₁+a₂₂*b₂₁+a₂₃*b₃₁ becomes 4*0+-5*-4+-6*3 and returns 2
Cell M8: a₁₁*b₁₂+a₁₂*b₂₂+a₁₃*b₃₂ becomes 1*1+2*0+3*1 and returns 4
Cell M9: a₂₁*b₁₂+a₂₂*b₂₂+a₂₃*b₃₂ becomes 4*1+-5*0+-6*1 and returns -2
6. Example 4
This example demonstrates how to calculate totals row-wise. The image above shows an array formula that returns an array containing the totals from each row in cell range C3:E7.
The number of columns in the first cell range or array must be equal to the number of rows in the second cell range or array.
Cell range C3:E7 has 3 columns and array {1; 1; 1} has three rows. The resulting array has the same number of rows as the cell range and the same number of columns as the array, in this case the array has 5 rows and 1 column. To be able to add all numbers in I use value 1 in the array.
You can also create an array that grows automatically based on the cell range, named range or an Excel defined Table.
This formula demonstrates how to create the array in the second argument using a cell range:
Let me explain this formula, the COLUMN function returns the corresponding column numbers from a cell range.
COLUMN(C3:E7) returns {3, 4, 5}.
COLUMN(C3:E7)^0 becomes {3, 4, 5}^0
and returns {1, 1, 1}. Any number (except zero) to the power of 0 (zero) returns 1. This array has values distributed across columns, we need them transposed row-wise.
TRANSPOSE(COLUMN(C3:E7)^0)
becomes TRANSPOSE({1, 1, 1}) and returns {1; 1; 1}.
This formula demonstrates how to create the array using a named range:
This formula demonstrates how to create the array using a reference to an Excel Table:
If you are an Excel 365 subscriber you can use the newly added SEQUENCE function:
7. Example 5
This example demonstrates how to calculate totals column-wise. This formula adds numbers and returns sums for each column in one calculation.
This article demonstrates how to search a data set with multiple strings using the MMULT function:
Recommended articles
RU asks: Can you please suggest if i want to find out the rows with fixed value in "First Name" […]
8. Function not working
MMULT returns a #VALUE! error if:
- any cells are empty or contain text.
- the number of columns in array1 is different from the number of rows in array2.
8.1 Troubleshooting the error value
When you encounter an error value in a cell a warning symbol appears, displayed in the image above. Press with mouse on it to see a pop-up menu that lets you get more information about the error.
- The first line describes the error if you press with left mouse button on it.
- The second line opens a pane that explains the error in greater detail.
- The third line takes you to the "Evaluate Formula" tool, a dialog box appears allowing you to examine the formula in greater detail.
- This line lets you ignore the error value meaning the warning icon disappears, however, the error is still in the cell.
- The fifth line lets you edit the formula in the Formula bar.
- The sixth line opens the Excel settings so you can adjust the Error Checking Options.
Here are a few of the most common Excel errors you may encounter.
#NULL error - This error occurs most often if you by mistake use a space character in a formula where it shouldn't be. Excel interprets a space character as an intersection operator. If the ranges don't intersect an #NULL error is returned. The #NULL! error occurs when a formula attempts to calculate the intersection of two ranges that do not actually intersect. This can happen when the wrong range operator is used in the formula, or when the intersection operator (represented by a space character) is used between two ranges that do not overlap. To fix this error double check that the ranges referenced in the formula that use the intersection operator actually have cells in common.
#SPILL error - The #SPILL! error occurs only in version Excel 365 and is caused by a dynamic array being to large, meaning there are cells below and/or to the right that are not empty. This prevents the dynamic array formula expanding into new empty cells.
#DIV/0 error - This error happens if you try to divide a number by 0 (zero) or a value that equates to zero which is not possible mathematically.
#VALUE error - The #VALUE error occurs when a formula has a value that is of the wrong data type. Such as text where a number is expected or when dates are evaluated as text.
#REF error - The #REF error happens when a cell reference is invalid. This can happen if a cell is deleted that is referenced by a formula.
#NAME error - The #NAME error happens if you misspelled a function or a named range.
#NUM error - The #NUM error shows up when you try to use invalid numeric values in formulas, like square root of a negative number.
#N/A error - The #N/A error happens when a value is not available for a formula or found in a given cell range, for example in the VLOOKUP or MATCH functions.
#GETTING_DATA error - The #GETTING_DATA error shows while external sources are loading, this can indicate a delay in fetching the data or that the external source is unavailable right now.
8.2 The formula returns an unexpected value
To understand why a formula returns an unexpected value we need to examine the calculations steps in detail. Luckily, Excel has a tool that is really handy in these situations. Here is how to troubleshoot a formula:
- Select the cell containing the formula you want to examine in detail.
- Go to tab “Formulas” on the ribbon.
- Press with left mouse button on "Evaluate Formula" button. A dialog box appears.
The formula appears in a white field inside the dialog box. Underlined expressions are calculations being processed in the next step. The italicized expression is the most recent result. The buttons at the bottom of the dialog box allows you to evaluate the formula in smaller calculations which you control. - Press with left mouse button on the "Evaluate" button located at the bottom of the dialog box to process the underlined expression.
- Repeat pressing the "Evaluate" button until you have seen all calculations step by step. This allows you to examine the formula in greater detail and hopefully find the culprit.
- Press "Close" button to dismiss the dialog box.
There is also another way to debug formulas using the function key F9. F9 is especially useful if you have a feeling that a specific part of the formula is the issue, this makes it faster than the "Evaluate Formula" tool since you don't need to go through all calculations to find the issue..
- Enter Edit mode: Double-press with left mouse button on the cell or press F2 to enter Edit mode for the formula.
- Select part of the formula: Highlight the specific part of the formula you want to evaluate. You can select and evaluate any part of the formula that could work as a standalone formula.
- Press F9: This will calculate and display the result of just that selected portion.
- Evaluate step-by-step: You can select and evaluate different parts of the formula to see intermediate results.
- Check for errors: This allows you to pinpoint which part of a complex formula may be causing an error.
The image above shows cell reference E8:F9 converted to hard-coded value using the F9 key. The MMULT function requires numerical values which is not the case in this example. We have found what is wrong with the formula.
Tips!
- View actual values: Selecting a cell reference and pressing F9 will show the actual values in those cells.
- Exit safely: Press Esc to exit Edit mode without changing the formula. Don't press Enter, as that would replace the formula part with the calculated value.
- Full recalculation: Pressing F9 outside of Edit mode will recalculate all formulas in the workbook.
Remember to be careful not to accidentally overwrite parts of your formula when using F9. Always exit with Esc rather than Enter to preserve the original formula. However, if you make a mistake overwriting the formula it is not the end of the world. You can “undo” the action by pressing keyboard shortcut keys CTRL + z or pressing the “Undo” button
8.3 Other errors
Floating-point arithmetic may give inaccurate results in Excel - Article
Floating-point errors are usually very small, often beyond the 15th decimal place, and in most cases don't affect calculations significantly.
Useful resources
MMULT function - Microsoft
MMULT in Excel
'MMULT' function examples
First, let me explain the difference between unique values and unique distinct values, it is important you know the difference […]
Table of Contents Count cells containing text from list Count entries based on date and time Count cells with text […]
This article describes how to count unique distinct values. What are unique distinct values? They are all values but duplicates are […]
Functions in 'Math and trigonometry' category
The MMULT function function is one of 62 functions in the 'Math and trigonometry' category.
Excel function categories
Excel categories
4 Responses to “How to use the MMULT function”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form
A great write-up as usual Oscar. Do you have any use cases to share? In 10 years of using Excel I can only think of one time where it occurred to me to use MMULT, and I can't put my finger on it now.
Hi Oscar,
Thanks for taking the request..
@ Andy.. & @Oscar
In addition.. to the previous discussion.. :)
Check the attached file.. there are few sample use of MMULT..
https://dl.dropboxusercontent.com/u/78831150/Excel/Sample%20use%20of%20MMULT.xlsx
https://dl.dropboxusercontent.com/u/78831150/Excel/Sample%20use%20of%20MMULT.jpg
https://postimg.org/image/tn0pfedvl/
Regards,
Deb
Andy,
Thank you. I have no examples to share, yet. I searched excelforum.com and found a few posts but nothing really sensational.
I googled "use of matrix multiplication" and found some practical examples in math and physics.
Deb,
Thank you for your contribution!
Formula #1
=SUM(MMULT(B2:C15;{1;-1}))
MMULT(B2:C15;{1;-1}) returns {29; 77; -1; 27; 69; 3; -3; 32; 54; -67; -65; -46; 21; -67}
equivalent to =B2:B15-C2:C15
So this formula does the same thing: =SUM(B2:B15-C2:C15)
Formula #2
=SUM(MMULT(B2:C15;{1;1})) sums of all values in B2:C15.
It is equivalent to this formula =SUM(B2:C15)
Good examples, I am beginning to understand. But I want to do someting creative and unique with the MMULT function, something that is not possible with other function or at least complicated to accomplish.
Thanks again for sharing!
[…] Mmult function […]