Why do I get the error message 'Inner matrix dimensions must agree. ' ?

22 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 12 Oct 2021
Edited: MathWorks Support Team on 17 Nov 2021
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 (.*, ./, .^).
https://www.mathworks.com/help/matlab/ref/vectorize.html
  2 Comments
Greg Heath
Greg Heath on 13 Feb 2018
[ I N ] = size("I"nput)
[ O N ] = size("O"utput)% = size(Target)
Walter Roberson
Walter Roberson on 19 Apr 2022
That is missing too much code for us to give much in the way of assistance. We have no idea what the sizes are for all those variables. Also, you did not indicate which MATLAB release you are using.
My pure guess is that your code has a / where it should have a ./ so you are getting a variable with a size you do not expect.

Sign in to comment.

More Answers (1)

Greg Heath
Greg Heath on 28 Apr 2016
For N I-dimensional inputs paired with N O-dimensional target outputs
[ I N ] = size(input)
[ O N ] = size(target)
I always place these right after I read the data in.
Hope this help.
Thank you for formally accepting my answer

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

No tags entered yet.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!