please i was trying to test a function that i have coded, but this message "subscripted assignment dimension mismatch" keeps appearing for every attempt. can someone help

function Y = trial_p(x,y)
Y = 100*(x.^2-y)+(1-x).^2;
end
lb=[0 0];
ub = [1 13];
d=2;
fobj = @trial_p
function [bestfitness avefitness maxfitness minfitness] = PAD_SFA(fobj,lb,ub,d,para)
%check input parameters
if nargin<5, para = [4 0.618 30 2.5 0.01 5];
if nargin<4, d = 1;
if nargin<3, ub=[];
if nargin<2,lb =[];
if nargin<1, disp('globaloptim:PAD_SFA:InvalidInputStruct');
return
end
end
end
end
end
%check if the confines are the same
if length(lb)~=length(ub)
disp('simple confines are improper');
return
end
MaxGeneration = para(1); delta = para(2);
n = para(3); visual= para(4);
step = para(5); try_number = para(6);
disp('/*=================================================================*/');
disp('/* Swarm Fish Optimistion */');
disp('/*=================================================================*/');
disp('>>>>');
disp('Begin to evaluate...waiting please...');
home;
tic;
t = 0;
sol = zeros(1,n);
best = zeros(1,n);
bestfitness = zeros(1,MaxGeneration);
avefitness = zeros(1,MaxGeneration);
maxfitness = zeros(1,MaxGeneration);
minfitness = zeros(1,MaxGeneration);
while (t<=MaxGeneration)
for k = 1:n
sol(k,:) = lb + (ub-lb).*rand(1,d);
end
zn= zeros(1,n);
for k = 1:n
zn(k) = sol(k,:);
neigh = zn(k) + visual.*rand;
x1 = search_SFA(fobj,step,visual,zn,neigh,try_number);
neigh = zn(k) + visual.*rand;
x2 = swarm_SFA(fobj,step,visual,zn,neigh,delta,n);
neigh = sol(k,d) + visual.*rand;
x3 = follow_SFA(fobj,step,visual,zn,neigh,delta,n);
yn = [x1 x2 x3];
for i = 1:3
yd = fhandle(yn(i));
end
[ybest ~] = sort(yd);
best(k) = ybest(1);
end
t =t+1;
[sfa ~] = sort(best);
bestfitness = sfa(1);
avefitness = mean(best);
maxfitness = avefitness + std(best);
minfitness = avefitness - std(best);
end
toc;
disp('End Swarmfish evaluation');
%---------------------------------------------------------------------
%----------------------subfuctions------------------------------------
%---------------------------------------------------------------------
function [ts] = search_SFA(fhandle,step,visual,zn,neigh,try_number)
if fhandle(neigh)<fhandle(sol)
ts = zn + step.*rand.*((neigh-zn)/abs(neigh-zn));
else
for i = 1:try_number
neighk = zn + visual.*rand;
end
%neighk(i) = zn;
for i = 1:5
if obj(neighk(i)) < obj(zn)
break
end
zy = zn + step.*rand.*((neigh-zn)/abs(neigh-zn));
end
if isempty(zy)
ts= zn + step.*randn;
end
end
function [th] = swarm_SFA(fhandle,step,visual,zn,neigh,delta,n)
df = fhandle(neigh); dg= fhandle(zn); dh =(visual/n);
if df < dg && dh < delta
th = zn + step.*rand.*((neigh-zn)/abs(neigh-zn));
else
th = search_SFA(fhandle,step,visual,zn,neigh,try_number);
end
function [tz] = follow_SFA(fhandle,step,visual,zn,neigh,delta,n)
if fhandle(neigh) < fhandle(zn)&& ((visual/n) < delta)
tz = zn + step.*rand.*((neigh-zn)/abs(neigh-zn));
else
tz = search_SFA(fhandle,step,visual,zn,neigh);
end

4 Comments

What line throws the error?
What are the dimensions of the arrays you are referencing in it?
What are the subscript values you are referencing?
line 38 of the main function (sol(k,:) = lb + (ub-lb).*rand(1,d);) and the dimension of the arrays is 2 the main function is [ ] = PAD_SFA(fobj,lb,ub,d,para)
Please, just give us the error message. Copy and paste ALL the red text - don't snip out just a portion of it.
Please find below the error message:
Subscripted assignment dimension mismatch.
Error in PAD_SFA (line 39) sol(k,:) = lb + (ub-lb).*rand(1,d);

Sign in to comment.

Answers (3)

I've not tried to understand your code, but sol is defined as a matrix with 1 row and n columns:
sol = zeros(1, n);
You then have a loop iterating from 1 to n, and use that iterator to index rows:
for k = 1:n
sol(k, :) = ...
Remember, there's only one row, so for k > 1, your code won't work. I have no idea, if it should be
sol(1, k) = ...
in the loop, or if you should declare your sol as:
sol = zeros(n, n); %which can also be written as sol = zeros(n);
I didn’t run your code (I don’t know what the appropriate values of ‘x’ and ‘y’ should be), but there are a few obvious problems:
if nargin<5, para = [4 0.618 30 2.5 0.01 5];
if nargin<4, d = 1;
if nargin<3, ub=[];
if nargin<2,lb =[];
if nargin<1, disp('globaloptim:PAD_SFA:InvalidInputStruct');
return
end
end
end
end
end
You only have 2 input arguments, so ‘para’ (and therefore ‘n’), ‘d’, and ‘ub’ will never be defined. That means your ‘sol’ variable will likely not be defined as well.
What do you want to do?
You have this:
sol = zeros(1, n);
meaning sol has n columns. But then you have this:
sol(k,:) = lb + (ub-lb).*rand(1,d);
and rand(1,d) is a 1-by-d row vector while sol(k,:) specifies n elements in the kth row. So does n equal d? If not, you'd get that message. For example it's not going to let you stuff a 20 element row vector into a row that is only 9 elements long - they MUST be the same size. I suggest you take a closer look at what n and d are and try to analyze what you're doing better.

Categories

Asked:

on 13 Dec 2015

Answered:

on 13 Dec 2015

Community Treasure Hunt

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

Start Hunting!