Why do I get an error stating "Matrix dimensions must agree", whilst the dimensions appear to be equal to me.

조회 수: 2 (최근 30일)
Hi everyone,
I am runing the attached code, and for some reasons I get the error message stating:
Matrix dimensions must agree.
Error in ParametersFittingEulerMethodWater>EulerMethod (line 254)
Results = sum((Results - xdata(i,:)).^2);
Error in fminsearch (line 200)
fv(:,1) = funfcn(x,varargin{:});
Error in ParametersFittingEulerMethodWater (line 58)
[b, Results] = fminsearch(@EulerMethod,b);
Whereas, to me, the dimensions appear to be equal. What am I doing wrong?
Please help.
  댓글 수: 4
Dursman Mchabe
Dursman Mchabe 2018년 12월 23일
Basically I need the sum of least squares on element by element, using:
for i = 1:length(tdata)
%Results = sum((Results(i,:) - xdata(i,:)).^2);
Results = sum(([Results(:,2) Results(:,4) Results(:,9)]) - ([xdata(:,2) xdata(:,3) xdata(:,4)])).^2;
end
I cant figure how to do that.
Image Analyst
Image Analyst 2018년 12월 23일
편집: Image Analyst 2018년 12월 23일
Dursman, you have to put the size() commands into your function, not in the command window (unless you're stopped at a breakpoint), because when the function exits, the variables are gone.
I also noted that it requires the Optimization Toolbox. For some reason, you didn't fill this out, or even the release you're using, when you posted, so I'll fill in the Toolbox for you so others don't waste time trying to run your code if they don't have that toolbox.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2018년 12월 23일
When you do this:
Results - xdata(i,:)
then Results must be a column vector, not a row vector or 2-D matrix, because xdata(:, i) is a column vector. Basically : means "all", so xdata(:, i) means "All rows in column i".
Also we advise not to use i and j as variables since they also stand for the imaginary constant.
  댓글 수: 7
Dursman Mchabe
Dursman Mchabe 2018년 12월 23일
Thanks for the comment Image Analyst. If I understand you correctly, you are implying that I try
[b, Results] = fminsearch(@EulerMethod,~);
I am understanding correctly?
Stephen23
Stephen23 2018년 12월 24일
편집: Stephen23 2018년 12월 24일
"I have passed b as scalar structure."
Sorry, that was my mistake: I had one thing in my mind but wrote something else: it was those global variables that I would advise to convert to a scalar structure (or use nested functions). Then you can simplify/remove all of those global calls.
Of course the function input b must be a numeric array (e.g. a vector), because that is what fminserarch will provide to your function. So leave b exactly like you had it before.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by