index exceeds the number of array elements. Index must not exceed 1.

j = 0;
for j = 1:class_size
if (Z(j) <= ZNew(j)); % Here we compare the new versus the max
X(j,:) = Xnew(j,:);
Z(j) = ZNew(j); % If new is better
end
X(j,:) = max(X(j,:),LL);
X(j,:) = min(X(j,:),UL);
end
Index exceeds the number of array elements. Index must not exceed 1.
Error in drjuri (line 79)
if (Z(j) <= ZNew(j)); % Here we compare the new versus the max
I tried to change j = 2 but problem still the same. What am i missing here what should i assign?
thanks.

 Respuesta aceptada

Jan
Jan el 30 de Dic. de 2022
Movida: Jan el 30 de Dic. de 2022
Omit the confusing
j = 0;
It does not have any effect, because the for j command overwrites the value immediately.
If Z and/or ZNew are scalar values, there is no Z(2) and/or ZNew(2). You did not include the definition of these variables, so the actual problem is hidden yet.

2 comentarios

nadzirul
nadzirul el 30 de Dic. de 2022
Editada: Jan el 30 de Dic. de 2022
The actual code is like these.
clear
clc
%% Phase 1: Input parameters
% General Variables
restrictions = zeros(5,1);
iteration = 100; % max_iter = 100;
design_variables = 5; % D = 2;
class_size = 35; % N = 20;
UL = [220 50 11 70 20]; % lb = [-5 -5];
LL = [210 30 9 50 10]; % ub = [5 5];
W = 1;
%% Phase 2: Defining the objective function
%% Phase 3: Generate initial population randomly
% Data
MeT = 210 + (220-210)*rand(class_size,1);
MoT = 30 + (50-30)*rand(class_size,1);
IP = 9 + (11-9)*rand(class_size,1);
IS = 50 + (70-50)*rand(class_size,1);
CT = 10 + (20-10)*rand(class_size,1);
% % Let's start the TLBO
WROut = [];
SMOut = '';
lastx = 0;
Xold = [];
Xnew = zeros(class_size,design_variables);
X = [MeT, MoT, IP, IS, CT];
firstX = X;
i = 1;
j = 1;
% % Phase 4 COmpute Xbest & Mean
while i <= iteration,
Mx = mean(X); % Mean
Mx = Mx-std(X);
WR = f1(X);
SM = f2(X);
Z = (W*(WR/min(WR)))-((1-W)*(SM/min(SM)));
[Z,ind] = sort(Z,1,'ascend');
X = X(ind,:);
[~,ind] = min(Z);
% With initial values we get Objective function
% Making teacher
Xteacher = X(ind,:);
Tf = round (1+rand());
r = rand();
difference = r*(Xteacher -Tf*(Mx));
% Obtaining Xnew
% Xnew = X + difference;
Xnew(:,1)=X(:,1)+difference(1);
Xnew(:,2)=X(:,2)+difference(2);
Xnew(:,3)=X(:,3)+difference(3);
Xnew(:,4)=X(:,4)+difference(4);
Xnew(:,5)=X(:,5)+difference(5);
% Teacher's proving Xnew vs Xold
WRNew = f1(Xnew);
SMNew = f2(Xnew);
ZNew = (W*(WRNew/min(WRNew)))-((1-W)*(SMNew/min(SMNew)));
j = 0;
for j = 1:class_size
if (Z(j) <= ZNew(j)); % Here we compare the new versus the max
X(j,:) = Xnew(j,:);
Z(j) = ZNew(j); % If new is better
end
X(j,:) = max(X(j,:),LL);
X(j,:) = min(X(j,:),UL);
end
If you append a trailing end, the code can be started. It stops with the error message:
Undefined function or variable 'f1'.
f2 is missing also.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2022b

Etiquetas

Preguntada:

el 30 de Dic. de 2022

Comentada:

Jan
el 30 de Dic. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by