필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
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
Image Analyst
Image Analyst 2015년 12월 13일
Please, just give us the error message. Copy and paste ALL the red text - don't snip out just a portion of it.
Tijjani Aminu Kuda
Tijjani Aminu Kuda 2015년 12월 13일
Please find below the error message:
Subscripted assignment dimension mismatch.
Error in PAD_SFA (line 39) sol(k,:) = lb + (ub-lb).*rand(1,d);

댓글을 달려면 로그인하십시오.

답변 (3개)

Guillaume
Guillaume 2015년 12월 13일
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);

Star Strider
Star Strider 2015년 12월 13일
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?

Image Analyst
Image Analyst 2015년 12월 13일
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.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by