# How to match indices of a matrix and how to define functions correctly in matlab

2 visualizaciones (últimos 30 días)
okoth ochola el 7 de Feb. de 2023
Movida: Torsten el 7 de Feb. de 2023
Hello community, allow me bother you guys again with yet another question again. Have been trying to write this code by the name Grey Wolf Optimizer to get Weibul parameters. I thought I got it right, till I gave it the real function to optimize, still I get some errors to do with matrix dimensions which have pasted below. What I dont understand, the test function worked well, I dont understand why it bring errors with the real log-likelihood weibull function function. Please assist, have stared on it now for long and I dont seem to recognize what am doing wrong. Please may someone assist,I will really appreciate. Am repasting the code and the function. Am really suspecting my function definition (let not this statement mislead you), thank you. The function am solving is also pasted below;
the script file is
clear allclear
clc
format short
fun = @wbl2;
a=3;
b=1:100;
X=[wblrnd(a,b)]';
N=300;
D=2;
lb=[0 0];
ub=[10 10];
itermax=500;
%generating intial popilation size
for i=1:N
for j=1:D
pos(i,j)=lb(j)+rand.*(ub(j)-lb(j));
end
end
%Evaluation of objective function
fx=fun(pos);
[fminvalue,ind]=min(fx);
% GWO main loop
iter=1;
while iter<=itermax
Fgbest=fminvalue;
a=2-2*iter/itermax;
for i=1:N
X=pos(i,:);
pos1=pos;
A1=2.*a.*rand(1,D)-a;
C1=2.*rand(1,D);
[alpha, alphaind]=min(fx);
alphapos=pos1(alphaind,:);
Dalpha=abs(C1.*alphapos-X);
X1=alphapos-A1.*Dalpha;
pos1(alphaind,:)=[];
fx1=fun(pos);
%finding beta position
[bet,betind]=min(fx1);
betpos=pos1(betind,:);
A2=2.*a.*rand(1,D)-a;
C2=2.*rand(1,D);
Dbet=abs(C2.*betpos-X);
X2=betpos-A2.*Dbet;
pos1(betind,:)=[];
fx1=fun(pos1);
%Delta position
[delta,deltaind]=min(fx1);
deltapos=pos1(deltaind,:);
A3=2.*a.*rand(1,D)-a;
C3=2.*rand(1,D);
Ddelta=abs(C3.*betpos-X);
X3=deltapos-A3.*Ddelta;
Xnew=(X1+X2+X3)./3;
size(Xnew)
%check bound
Xnew=max(Xnew,lb);
Xnew=min(Xnew,ub);
fnew=fun(Xnew);
%greedy slection
if fnew<fx(i)
pos(i,:)=Xnew;
fx(i,:)=fnew;
end
end
%Update Gbest
[fmin,find]=min(fx);
if fmin<Fgbest
Fgbest = fmin;
gbest=pos(find,:);
end
%memorize
[optval,optind]=min(fx);
BestFx(iter)=optval;
BestX(iter,:)=pos(optind,:);
%show iteration infomation
plot(BestFx, 'LineWidth',2);
iter=iter+1
end
ans = 1×2
2 2
Unable to perform assignment because the size of the left side is 1-by-2 and the size of the right side is 2-by-2.
out=BestX
the function file is
%X is a matrix containing wind velocities
function fx = wbl2(X)
n=numel(X);
A1=X(:,1);
A2=X(:,2);
%objective function
fx=-1*(n*log(A1)-n*log(A2)-sum((X./A2).^A1)+(A1-1)*sum(log(X)));
%define penalty term
end
when i run the code I get the following error
Unable to perform assignment because the size of the left side is 1-by-2 and the size of the right side is 2-by-2.
Error in Gwo7 (line 61)
pos(i,:)=Xnew;
I just ca'nt figure out what am doing wrong.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuestas (1)

Torsten el 7 de Feb. de 2023
Movida: Torsten el 7 de Feb. de 2023
As you can see, Xnew is a 2x2 matrix.
You want to assign a matrix to a vector in
pos(i,:)=Xnew;
which is not possible.
Trace back in your code how Xnew comes to be a matrix instead of a 1x2 vector.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Multiobjective Optimization en Help Center y File Exchange.

R2018a

### Community Treasure Hunt

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

Start Hunting!

Translated by