MATLAB: Do I get the error message ‘Inner matrix dimensions must agree. ‘
inner matrix dimensions must agreeMATLAB
Why do I get the following error message :
Inner matrix dimensions must agree.
Best Answer
Explanation:
You are attempting to perform a matrix operation, which requires certain matrix dimensions to agree, on matrices that do not satisfy this requirement.
Common causes:
You are attempting to multiply or divide two matrices where the number of columns in the first is not equal to the number of rows in the second (for *) or the number of rows do not match (for \). This often indicates that you are performing matrix operations when you instead intended to perform array operations.
Please refer to the attached example demonstrating this error:
MatrixDimensionsMustAgree.m
Solution:
Stop MATLAB on the line where the error occurs. Verify that you are not performing an extra transpose operation or omitting one where necessary. Also verify the sizes of the matrices, which you are multiplying or dividing, agree in the corresponding dimensions. You can do this using the Workspace browser or the SIZE function. If you intended to perform array operations instead of matrix operations, replace the, /, \, or ^ matrix operators with the ., ./, .\, or .^ array operators instead. If you pass your formula as a string to the VECTORIZE function, VECTORIZE will return the formula with the matrix operators (, /, and ^) replaced by the array operators (., ./, .^).
Starting with MATLAB R2016b, implicit expansion was updated so that arrays are considered to be compatible sizes if, for every dimension, the dimension sizes of the inputs are either the same or one of them is 1. This allows you to apply element-wise operations and functions to arrays that previously would have caused "matrix dimensions must agree" errors. In this case, starting with MATLAB R2016b, adding a 1x121 matrix to a 121x1 matrix would produce a 121x121 matrix.
More information about how different-sized matrices are operated on since this implicit expansion update can be found in this link:
Best Answer