Subscripted assignment dimension mismatch.

1 visualizzazione (ultimi 30 giorni)
tahseen alshmary
tahseen alshmary il 4 Dic 2018
i have this error
Subscripted assignment dimension mismatch.
Error in Untitled4 (line 106)
rgbest(run,:)=gbest;
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=100; % population size
wmax=0.9; % inertia weight
wmin=0.4; % inertia weight
c1=2; % acceleration factor
c2=2; % acceleration factor
% pso main program----------------------------------------------------start
maxite=500; % 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(1); % 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
gbest
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')
%##########################################--------------------------end
  2 Commenti
Adam
Adam il 4 Dic 2018
You should presize rgbest so it is the correct size before doing an assignment of that kind.
tahseen alshmary
tahseen alshmary il 4 Dic 2018
thank you for your answer .
how can i do that

Accedi per commentare.

Risposte (0)

Prodotti


Release

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by