index exceeds the number of array elements (1).

조회 수: 2 (최근 30일)
Pavan Malghan
Pavan Malghan 2021년 3월 2일
댓글: Pavan Malghan 2021년 3월 2일
fobj=min(e);
Max_iter=20;
N=30;
dim=2;
lb=[2;1000];
ub=[15;10000];
if size(ub,1)==1
ub=ones(dim,1)*ub;
lb=ones(dim,1)*lb;
end
%Initialize the population of grasshoppers
if size(ub,1)==1
X=rand(N,dim).*(ub-lb)+lb;
end
if size(ub,1)>1
for i=1:dim
high=ub(i);
low=lb(i);
X(:,i)=rand(1,N).*(high-low)+low;
end
end
X1=round(X);
GrassHopperPositions=X1;
GrassHopperFitness = zeros(1,N);
fitness_history=zeros(N,Max_iter);
position_history=zeros(N,Max_iter,dim);
Convergence_curve=zeros(1,Max_iter);
Trajectories=zeros(N,Max_iter);
cMax=1;
cMin=0.00004;
%Calculate the fitness of initial grasshoppers
for i=1:size(GrassHopperPositions,1)
GrassHopperFitness(1,i)=fobj(GrassHopperPositions(i,:));
fitness_history(i,1)=GrassHopperFitness(1,i);
position_history(i,1,:)=GrassHopperPositions(i,:);
Trajectories(:,1)=GrassHopperPositions(:,1);
end
  댓글 수: 1
Pavan Malghan
Pavan Malghan 2021년 3월 2일
Kindly help me in solving the issue. i have written in bold where i am getting that error. Hoping for the best solution

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

채택된 답변

KSSV
KSSV 2021년 3월 2일
This error occurs when you try to extract more number of elements than present in the array.
EXample:
A = rand(1,10) ;
A(1) % no error
A(5) % no error
A(end) % no error
A(11) % error, as there are only 10 elements and you cannot extract 11th one.
Check your dimensions of the array and fix you loop indices.
  댓글 수: 1
Pavan Malghan
Pavan Malghan 2021년 3월 2일
thank you very much sir. I will go through it

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by