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

조회 수: 2 (최근 30일)
nadzirul
nadzirul 2022년 12월 30일
댓글: Jan 2022년 12월 30일
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.

채택된 답변

Jan
Jan 2022년 12월 30일
이동: Jan 2022년 12월 30일
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
nadzirul
nadzirul 2022년 12월 30일
편집: Jan 2022년 12월 30일
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
Jan
Jan 2022년 12월 30일
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.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by