Arrays have incompatible sizes for this operation error
6 次查看(过去 30 天)
显示 更早的评论
Hello i am working on an optimization problem where i have 2 dec. variables x and y. x is a matrix with dimensions 36x36 and y is 36x1. my objective function only uses one of the variables which is x. but since my number of variables is 1332= (1296 +36). Now my objective function gives this error. Arrays have incompatible sizes for this operation error.
Here is my objective function looks like ;
function z = objectivefunc(x,rij,ai)
z= (-1)*sum(x.*rij.*ai,'all');
end
rij and ai are my parameters. (rij 36x36, ai 36x1 ).
Thank you in advance.
0 个评论
回答(2 个)
Alan Weiss
2022-4-8
It looks like the issue is that ai is not 36-by-36. You probably want something like
ai = repmat(ai,1,36);
z= (-1)*sum(x.*rij.*ai,'all');
Alan Weiss
MATLAB mathematical toolbox documentation
Alan Weiss
2022-4-8
You are mixing up the two approaches, problem-based and solver-based. You cannot do that. You have x and y defined as optimization variables. That is fine. You cannot call ga on a problem with optimization variables; you have to call solve.
Also, I am not familiar with prob2matrices. Did you mean prob2struct? In any case, I think that you should stick with the problem-based approach in order to keep the variables x and y separated, not shoved together into one long, hard-to-understand variable.
Alan Weiss
MATLAB mathematical toolbox documentation
2 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Problem-Based Optimization Setup 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!