Arrays have incompatible sizes for this operation error

조회 수: 4 (최근 30일)
Azime Beyza Ari
Azime Beyza Ari 2022년 4월 8일
편집: Azime Beyza Ari 2022년 4월 8일
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.

답변 (2개)

Alan Weiss
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
  댓글 수: 1
Azime Beyza Ari
Azime Beyza Ari 2022년 4월 8일
편집: Azime Beyza Ari 2022년 4월 8일
No it did not solve the problem. I think problem is casued because this;
my x variables are 1296
and since i have another variable called y which hase 36 variables. i wrote nvars as 1332. after i write it 1332 it gives that error. Before i introduced the second variable i did not get any erorrs like that before.
I will attach the file. if you cna have look i would be happy.

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


Alan Weiss
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
Azime Beyza Ari
Azime Beyza Ari 2022년 4월 8일
편집: Azime Beyza Ari 2022년 4월 8일
i have used prob2matrices to create my constraints.
What i am doing which causes conflict? Is there any way without making it hard-to-understand?
Ps. I also wanna solve my problem with ga. It is a requirement.
Azime Beyza Ari
Azime Beyza Ari 2022년 4월 8일
편집: Azime Beyza Ari 2022년 4월 8일
Instead of using prob2matrices, if i write my constraints like following, would the error go away (Also since x's all have the same name and there are 1296 of them it think i would be very hard for me to write them in this way) ? or are there any other problems causing it? ;

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

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by