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

Why do I get the following error message :
ERROR: Inner matrix dimensions must agree.

 Accepted 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 (.*, ./, .^).
https://www.mathworks.com/help/matlab/ref/vectorize.html

2 Comments

[ I N ] = size("I"nput)
[ O N ] = size("O"utput)% = size(Target)
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)

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

Products

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!