How do I fix this error: "Error using minus Matrix dimensions must agree"?
1 view (last 30 days)
Show older comments
Here is my script:
function f = objectiveL2(p, pIndex, data, trainingIndex, kinesinName, lambda)
if length(p) ~= size(pIndex,1)
error('p and pIndex must have the same length');
end
p00 = InitialParameterValues(kinesinName);
p0 = InitialParameterValues(kinesinName);
for i = 1:size(pIndex,1)
for j = 1:size(pIndex,2)
if pIndex(i,j) ~= 0
p00(pIndex(i,j)) = p(i)*p00(pIndex(i,j));
end
end
end
for i = 1:size(data,1)
if strcmp(data(i,1).kinesinName,kinesinName)
kinesinIndex = i;
break;
end
end
f = 0;
for i = 1:length(trainingIndex)
% Define initial conditions for the model
x0 = zeros(12,1);
x0(1) = data(kinesinIndex,trainingIndex(i)).kinesinConc; % Kinesin
x0(2) = data(kinesinIndex,trainingIndex(i)).MTConc; % MT
x0(8) = data(kinesinIndex,trainingIndex(i)).mantATPConc; % mantATP
tspan = [0;data(kinesinIndex,trainingIndex(i)).time];
[~,x] = ode45(@(t,x,p) SteppingModel_no78(1,x,p00), tspan, x0);
y_model = p00(15)*(x(2:end,9)+x(2:end,10));
y_data = data(kinesinIndex,trainingIndex(i)).fluorescence - 1;
f = f + sum((y_model - y_data).^2)/length(y_data);
end
f = f + lambda*sum(((p0(1:14) - p00(1:14))./p0(1:14)).^2);
end
Here is the error I get
Error using -
Matrix dimensions must agree.
Error in objectiveL2 (line 38)
f = f + sum((y_model - y_data).^2)/length(y_data);
Error in @(p)objectiveL2(p,pIndex,data,trainingIndex,kinesinName,0.0000000000001)
Error in fmincon (line 534)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in main (line 60)
[pFit, fval, flag] = fmincon(@(p) objectiveL2(p, pIndex, data, trainingIndex, kinesinName, 0.0000000000001), ...
Caused by:
Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.
I know where the error is occurring (line 38), but I really don't know how to fix it. It looks correct to me. Any help would be greatly appreciated.
Thanks!
2 Comments
Geoff Hayes
on 28 Jan 2016
Sean - what are the dimensions of y_model and y_data? Since you are subtracting one from the other, they must have the same number of rows and the same number of columns.
Walter Roberson
on 29 Jan 2016
Where is kinesinIndex being defined? Is it a function, or is it being defined in an outer function that this one is nested inside?
Answers (0)
See Also
Categories
Find more on Particle & Nuclear Physics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!