Using struct instead of class

1 次查看(过去 30 天)
shawin
shawin 2016-10-28
评论: KSSV 2016-10-28
Hi, I have the code below and I used struct instead of the class , but I'm receiving error ( Index exceed matrix dimension) the error is in shekel function ??:
clc;
clear al;
close all;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Population_Size = 50;
Dimension = 2;
upperLimit = 50;
lowerLimit = -50;
R_1 = 1;
R_2 = -0.5;
V_1 = 1;
V_2 = 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% use struct instead of class in c#
CCll = struct('f',[],'p',[],'c',[]);
Best_Clls =struct('f',[],'p',[],'c',[]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
range = upperLimit - lowerLimit;
randGenerate = rand()* range + lowerLimit;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
CCll.f= realmax;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1:Population_Size
for j = 1:Dimension
CCll(i).p(j)= randGenerate;
x= CCll(i).p(j);
CCll.f=Shekel(x);
if (CCll(i).f < Best_Clls.f)
Best_Clls.f = CCll(i).f;
Best_Clls.p = CCll(i).p;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
the Function is :
function foxholes = Shekel(x)
[xSize, Dim] = size(x);
i=1;
a=[-32 -16 0 16 32 -32 -16 0 16 32 -32 -16 0 16 32 -32 -16 0 16 32 -32 -16 0 16 32
-32 -32 -32 -32 -32 -16 -16 -16 -16 -16 0 0 0 0 0 16 16 16 16 16 32 32 32 32 32];
ss=zeros(xSize,25);
for j=1:25
ss(:,j)=j+(x(:,1)-a(i,j)).^6+(x(:,2)-a(i+1,j)).^6;
end
ss=ss.^(-1);
r=sum(ss')';
foxholes=(1/500+r).^(-1);
end
  1 个评论
KSSV
KSSV 2016-10-28
your x which is input to shekel is a scalar (1x1) and you are trying to access x(:,1), x(:,2) inside the function. You have to consider on this point.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by