The assignment cannot be performed because the size of the left side is 1×1 and the size of the right side is 0×0.
2 views (last 30 days)
Show older comments
The object function I code is as below, when I use the ga to call this function , occur the error:"The assignment cannot be performed because the size of the left side is 1×1 and the size of the right side is 0×0." But I do not know where is wrong.Anyone who can help me ? The code and the data needed I have upload,please help me. Thanks advanced.
function F = evaluate_objective(x)
global y_e beta lamda sample1 r0
f = zeros(1,3);%
Xt = [x(1) x(2) x(3) x(4) x(5) x(6) x(7) x(8) x(9) x(10)];
B = [1 1+sum(Xt)];% 一次项
[mS,nS]=size(sample1);[mX,nX]=size(Xt);
if nX~=nS %check that both matrices are of the right shape
Xt=Xt';
[mX,nX]=size(Xt);
end
Phi = zeros(mX,mS); %compute pairwise distances of points in X and S
for ii = 1:mX
for jj = 1:mS
Phi(ii,jj) = exp(-0.5*((norm(Xt(ii,:)-sample1(jj,:)))./r0)^2);
end
end
f(1) = abs((y_e(:,1) - (Phi*lamda(:,1)+B*beta(:,1))))/y_e(:,1);
f(2) = abs((y_e(:,2) - (Phi*lamda(:,2)+B*beta(:,2))))/y_e(:,2);
f(3) = abs((y_e(:,3) - (Phi*lamda(:,3)+B*beta(:,3))))/y_e(:,3);
F = max(f);
end
0 Comments
Accepted Answer
Stephen23
on 17 Nov 2021
Edited: Stephen23
on 17 Nov 2021
"But I do not know where is wrong"
The problem is caused by using GLOBAL variables instead of parameterizing the function properly:
One/some/all of those global variables are empty (probably because they are still set to the default empty array) and so the calculations on the RHS of these lines are empty:
f(1) = .. empty
f(2) = .. empty
f(3) = .. empty
But clearly you cannot assign zero elements to one element, thus the error.
The solution, as I stated at the start, is to parameterize the function properly:
S = load('var.mat');
fnh = @(x) evaluate_objective(x, S.Y, S.beta, S.lamda, S.sample1, S.r0);
out = ga(fnh,..)
But becuse your function has other errors, I cannot run this.
9 Comments
More Answers (0)
See Also
Categories
Find more on Logical 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!