Using struct instead of class
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
KSSV
le 28 Oct 2016
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.
Réponses (0)
Voir également
Catégories
En savoir plus sur Data Types dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!