Numerical Optimization Code: ERROR Matrix Dimensions Must Agree
显示 更早的评论

I am trying to run a code for numerical optimization and I encountered the error message "matrix dimensions must agree". Both fitx and fitx_new were generated using a for loop. When I checked the size of x (for fitx) and the size of ui (for fitx_new), I found that they are the same. Has anyone encountered this kind of problem before? If so, what should I do? Thanks in advance!
4 个评论
"When I checked the size of x (for fitx) and the size of ui (for fitx_new), I found that they are the same"
No you didn't check the size, because you used length.
Do NOT use length ! length is an abomination that should be ignored and not used, because the dimension that its output measures changes depending on the size of the array. Ouch!
Always use size or numel. Please show us the output of these commands:
size(fitx)
size(fitx_new)
JDL
2018-8-1
Dennis
2018-8-1
fitx and fitx_new have different sizes so fitx-fitx_new returns 'Matrix dimensions must agree'. You said you created both in a loop, however you didn't show the code. It is quite challenging to guess why your loop creates 2 vectors of different length without having any information about it.
JDL
2018-8-1
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Linear Least Squares 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
