Subscripted assignment dimension mismatch. Error in Untitled4 (line 80) ffmin(ite,run)=fmin; % storing best fitness

2 views (last 30 days)
tahseen alshmary
tahseen alshmary on 27 Nov 2018
Commented: tahseen alshmary on 28 Nov 2018
Dear sir
i have this error
how can i solve it
((Subscripted assignment dimension mismatch))
Error in Untitled4 (line 80)
ffmin(ite,run)=fmin; % storing best fitness
the code is :-
%---------------------------------------------------------------------------------------------------------------------------------start
tic
clc
clear all
close all
rng default
LB=[0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05]; % lower bounds of variables
UB=[1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1]; % upper bounds of variables
% pso parameters values
m=14; % number of variables
n=800; % population size
wmax=0.9; % inertia weight
wmin=0.4; % inertia weight
c1=1; % acceleration factor
c2=0.2; % acceleration factor
% pso main program----------------------------------------------------start
maxite=10000; % set maximum number of iteration
maxrun=10; % set maximum number of runs need to be
for run=1:maxrun
run
% pso initialization----------------------------------------------start
for i=1:n
for j=1:m
x0(i,j)=round(LB(j)+rand()*(UB(j)-LB(j)));
end
end
x=x0; % initial population
v=0.1*x0; % initial velocity
f0=cell(1,n); %preallocation
for i=1:n
f0{i}=ofun(x0(i,:));
end
[fmin0,index0]=min(f0{i});
pbest=x0; % initial pbest
gbest=x0(index0,:); % initial gbest
% pso initialization-----------------------------------------------end
% pso algorithm---------------------------------------------------start
ite=1;
tolerance=1;
while ite<=maxite && tolerance>1e-12
w=wmax-(wmax-wmin)*ite/maxite; % update inertial weight
% pso velocity updates
for i=1:n
for j=1:m
v(i,j)=w*v(i,j)+c1*rand()*(pbest(i,j)-x(i,j))...
+c2*rand()*(gbest(1,j)-x(i,j));
end
end
% pso position update
for i=1:n
for j=1:m
x(i,j)=x(i,j)+v(i,j);
end
end
% handling boundary violations
for i=1:n
for j=1:m
if x(i,j)<LB(j)
x(i,j)=LB(j);
elseif x(i,j)>UB(j)
x(i,j)=UB(j);
end
end
end
% evaluating fitness
f=cell(1,50); %preallocation
for i=1:n
f{i}=ofun(x(i,:));
end
% updating pbest and fitness
for i=1:n
if f{i}<f0{i}
pbest(i,:)=x(i,:);
f0{i}=f{i};
end
end
[fmin,index]=min(f0{i});
% finding out the best particle
ffmin(ite,run)=fmin; % storing best fitness
ffite(run)=ite; % storing iteration count
% updating gbest and best fitness
if fmin<fmin0
gbest=pbest(index,:);
fmin0=fmin;
end
% calculating tolerance
if ite>100;
tolerance=abs(ffmin(ite-100,run)-fmin0);
end
% displaying iterative results
if ite==1
fprintf('Iteration Best particle Objective fun\n');
end
fprintf('%8g %8g %8.4f\n',ite,index,fmin0);
ite=ite+1;
end
% pso algorithm---------------------------------------------------end
fvalue=2.633*x(1)+2.992*x(2)+3.134*x(3)+3.678*x(4)+3.620*x(5)+2.948*x(6)+1.607*x(7)+2.952*x(8)+3.348*x(9)+3.680*x(10)+3.774*x(11)+2.995*x(12)+3.237*x(13)+1.608*x(14);
fff(run)=fvalue
rgbest(run,:)=gbest;
fprintf('--------------------------------------\n');
end
% pso main program------------------------------------------------------end
fprintf('\n\n');
fprintf('*********************************************************\n');
fprintf('Final Results-----------------------------\n');
[bestfun,bestrun]=min(fff)
best_variables=rgbest(bestrun,:)
fprintf('*********************************************************\n');
toc
% PSO convergence characteristic
plot(ffmin(1:ffite(bestrun),bestrun),'-k');
xlabel('Iteration');
ylabel('Fitness function value');
title('PSO convergence characteristic')

Answers (1)

Geoff Hayes
Geoff Hayes on 27 Nov 2018
tahseen - the problem with
ffmin(ite,run)=fmin;
might be because fmin is not a scalar but an array. There could be one or more elements with the same minimum value when calling
[fmin,index]=min(f0{i});
I think what you want to do is either check to see what the dimensions are for fmin (and then act accordingly) or just use the first element always
ffmin(ite,run)=fmin(1);
  7 Comments

Sign in to comment.

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!