Search code examples
algorithmmatlabparticle-swarm

Explain Matlab Code


This code is part of PSO Algorithm, in MATLAB. What is the type of empty_particle and particle?

npop=100;

empty_particle.position=[];
empty_particle.velocity=[];
empty_particle.cost=[];
empty_particle.pbest=[];
empty_particle.pbestcost=[];

particle=repmat(empty_particle,npop,1);

Solution

  • empty_particle will be a structure which is then has entries initialised with a 0x0 array

    They can either be created via the struct command

    empty_particle = struct()
    empty_particle.position=[];
    

    or via matlab typecasting when you attempt to access an attribute

    empty_particle.position=[];
    

    if you type whos into the command window it will tell you a lot about the workspace variables.