You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: DEM_dSprites.m
    path of file: ./repos/spm/toolbox/DEM
    
    the code of the file until where you have to start completion: function mdp = DEM_dSprites
% Demo of active inference and structure learning (i.e.,disentanglement)
%__________________________________________________________________________
%
% This routine uses a Markov decision process formulation of active
% inference to illustrate structure learning. Structure learning here is
% read as optimising the structure of a generative model that, crucially,
% includes dynamics. This foregrounds the sequential order and temporal
% scheduling of various updates. In this example, we start with a simple
% problem in which one or more objects can be removed around in a
% two-dimensional space. The kind of structure learning considered here can
% be likened to nonparametric Bayes; namely, the addition of a model
% component if licensed in terms of model evidence or marginal likelihood.
% Procedurally, this means that with each new stimulus (sequence of
% observations) various models are compared that entail the addition of a
% new latent state, path or factor. If the ELBO (i.e., negative variational
% free energy) increases the addition is accepted but not otherwise. The
% training sequences are carefully constructed to reflect the ordinal
% structure of observations. In other words, structure learning is
% predicated on both the content and dynamics of the generative process.
% 
% This demonstration calls a belief propagation scheme with factorisation
% of latent states into factors. Furthermore, the likelihood mapping is
% factorised into conditionally independent outcome modalities. This means
% that the size of any requisite tensor for belief updating is upper
% bounded by the factorisation or mean field approximation). This mitigates
% the van Neumann bottleneck; leading to increased efficiency at all three
% levels of optimisation (inference, learning and model selection).
% 
% A key aspect of this demonstration routine is that it deals with discrete
% state space generative models (and observations). This means that belief
% propagation and updating can be implemented using linear (tensor)
% operators; without worrying about nonlinearities of the sort found in
% continuous state space models.
%
%_________________________________________________________________________
% Copyright (C) 2019 Wellcome Trust Centre for Neuroimaging

% Karl Friston
% $Id: DEM_dSprites.m 8439 2023-03-27 18:41:45Z guillaume $

%% set up and preliminaries
%==========================================================================
rng(1)

% create three two-dimensional objects
%--------------------------------------------------------------------------
dSprite{1} = [ ...
    0 0 0 0 0 0 0 0;
    0 1 1 1 1 1 1 0;
    0 1 1 1 1 1 1 0;
    0 1 1 1 1 1 1 0;
    0 1 1 1 1 1 1 0;
    0 1 1 1 1 1 1 0;
    0 1 1 1 1 1 1 0;
    0 0 0 0 0 0 0 0];

dSprite{2} = [ ...
    0 0 0 0 0 0 0 0;
    0 0 1 1 0 1 1 0;
    0 1 1 1 1 1 1 1;
    0 1 1 1 1 1 1 1;
    0 0 1 1 1 1 1 0;
    0 0 0 1 1 1 0 0;
    0 0 0 0 1 0 0 0;
    0 0 0 0 0 0 0 0];

dSprite{3} = [ ...
    0 0 0 0 0 0 0 0;
    0 0 1 1 1 1 0 0;
    0 1 1 1 1 1 1 0;
    1 1 1 1 1 1 1 1;
    1 1 1 1 1 1 1 1;
    0 1 1 1 1 1 1 0;

    
function mdp = DEM_dSprites
% Demo of active inference and structure learning (i.e.,disentanglement)
%__________________________________________________________________________
%
% This routine uses a Markov decision process formulation of active
% inference to illustrate structure learning. Structure learning here is
% read as optimising the structure of a generative model that, crucially,
% includes dynamics. This foregrounds the sequential order and temporal
% scheduling of various updates. In this example, we start with a simple
% problem in which one or more objects can be removed around in a
% two-dimensional space. The kind of structure learning considered here can
% be likened to nonparametric Bayes; namely, the addition of a model
% component if licensed in terms of model evidence or marginal likelihood.
% Procedurally, this means that with each new stimulus (sequence of
% observations) various models are compared that entail the addition of a
% new latent state, path or factor. If the ELBO (i.e., negative variational
% free energy) increases the addition is accepted but not otherwise. The
% training sequences are carefully constructed to reflect the ordinal
% structure of observations. In other words, structure learning is
% predicated on both the content and dynamics of the generative process.
% 
% This demonstration calls a belief propagation scheme with factorisation
% of latent states into factors. Furthermore, the likelihood mapping is
% factorised into conditionally independent outcome modalities. This means
% that the size of any requisite tensor for belief updating is upper
% bounded by the factorisation or mean field approximation). This mitigates
% the van Neumann bottleneck; leading to increased efficiency at all three
% levels of optimisation (inference, learning and model selection).
% 
% A key aspect of this demonstration routine is that it deals with discrete
% state space generative models (and observations). This means that belief
% propagation and updating can be implemented using linear (tensor)
% operators; without worrying about nonlinearities of the sort found in
% continuous state space models.
%
%_________________________________________________________________________
% Copyright (C) 2019 Wellcome Trust Centre for Neuroimaging

% Karl Friston
% $Id: DEM_dSprites.m 8439 2023-03-27 18:41:45Z guillaume $

%% set up and preliminaries
%==========================================================================
rng(1)

% create three two-dimensional objects
%--------------------------------------------------------------------------
dSprite{1} = [ ...
    0 0 0 0 0 0 0 0;
    0 1 1 1 1 1 1 0;
    0 1 1 1 1 1 1 0;
    0 1 1 1 1 1 1 0;
    0 1 1 1 1 1 1 0;
    0 1 1 1 1 1 1 0;
    0 1 1 1 1 1 1 0;
    0 0 0 0 0 0 0 0];

dSprite{2} = [ ...
    0 0 0 0 0 0 0 0;
    0 0 1 1 0 1 1 0;
    0 1 1 1 1 1 1 1;
    0 1 1 1 1 1 1 1;
    0 0 1 1 1 1 1 0;
    0 0 0 1 1 1 0 0;
    0 0 0 0 1 0 0 0;
    0 0 0 0 0 0 0 0];

dSprite{3} = [ ...
    0 0 0 0 0 0 0 0;
    0 0 1 1 1 1 0 0;
    0 1 1 1 1 1 1 0;
    1 1 1 1 1 1 1 1;
    1 1 1 1 1 1 1 1;
    0 1 1 1 1 1 1 0;
    0 0 1 1 1 1 0 0;
    0 0 0 0 0 0 0 0];


% Latent states
%--------------------------------------------------------------------------
% First, specify latent states
%--------------------------------------------------------------------------
% This sets up a label structure with the names and cardinality of each
% factor and the accompanying latent states. These are further equipped
% with a number of actions that require a probability transition matrix for
% each allowable action. Here, the actions correspond to moving things in
% each dimension
%--------------------------------------------------------------------------
label.factor  = {'x','y','kind'};

Nx    = 8;                          % number of pixels
Nk    = numel(dSprite);             % number of dSprite kinds

for i = 1:Nx, label.name{1}{i} = sprintf('x%i',i); end