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

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')

Antworten (1)

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 Kommentare

you mean the code will be
[fmin(1),index]=min(fo{i})
ffmin(itr,run)=fmin(1);
yes, please try that (to account for the case where fmin is an array)
yes Mr Geoff
appear same error with
Subscripted assignment dimension mismatch.
Error in Untitled4 (line 106)
rgbest(run,:)=gbest;
tahseen - probably the same issue as with the other...**gbest** may be an array that has too many or too few columns to fit into rgbest. Please use the MATLAB debugger to verify this.
yes thank you
how can i use the MATLAB debugger?
okay thank you so much
i will try

Melden Sie sich an, um zu kommentieren.

Produkte

Version

R2017b

Gefragt:

am 27 Nov. 2018

Kommentiert:

am 28 Nov. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by