Index exceeds the number of array elements (1). in matlab

1 view (last 30 days)
%Spider Monkey Optimization Algorithm
clc;
clear;
close all;
M=4;
N=4;
SM=randi([10 50],M,N);
U=randi([0 1],M,N);
min=1;
max=10;
for i=1:M;
for j=1:N;
t1=SM(i,j);
t2=U(i,j);
fx(:,j)=t1*(min)+t2*(t1*(max)-t1*(min));
end
SMij(i,:)=fx;
end
SMij
LL=randi([5 10],M,N);
Unew=randi([-1 1],M,N);
for r=1:M;
for j=1:N;
t3=SMij(r,j);
t4=LL(r,j);
t5=Unew(r,j);
fxx(:,j)=t3+t5*(t4-t3)+t5*(t3-t3);
end
SMNewij(r,:)=fxx;
end
SMNewij
% Algorithm#1 LPL
xmin=0.1;
xmax=0.8;
pr=xmin+rand(M,N)*(xmax-xmin);
for k=1:M;
for j=1:N;
if U>=pr; %startif
t6=SM(k,j);
t7=U(k,j);
t8=LL(k,j);
t9(k,j)=t6+t7*(t8-t6)+t7*(t6-t6);
else
SMNewij=SMij;
end %endif
end
end
for i=1:M;
for j=1:N;
xi=SM(i,j);
t1(:,j)=(abs(xi)^2)^3;
end
fitnessFunction(i,:)=sum(t1,2);
end
fitnessFunction
[val, loc] = sort(fitnessFunction)
MinValue=min(fitnessFunction)

Accepted Answer

Walter Roberson
Walter Roberson on 10 May 2020
MinValue=min(fitnessFunction)
You are probably wanting to invoke the min() function there. But you assigned min to be a variable so that is an indexing request.
Lesson of the day: do not use min or max or sum as variable names
  3 Comments
Walter Roberson
Walter Roberson on 10 May 2020
You have
min=1;
Rename that to some other variable name, and use the new variable name in
fx(:,j)=t1*(min)+t2*(t1*(max)-t1*(min));
Are you sure about that line of code??? Are you sure you are not supposed to be doing something like
fx(:,j) = min(t1) + t2*(max(t1) - min(t1));

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!