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 (., ./, .^).