index exceeds the number of array elements (1).

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 Comment

Kindly help me in solving the issue. i have written in bold where i am getting that error. Hoping for the best solution

Sign in to comment.

 Accepted Answer

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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!