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_cells_cells.m
    path of file: ./repos/spm12/toolbox/DEM
    
    the code of the file until where you have to start completion: function DEM = DEM_cells_cells

% This demo is a hierarchical extension of DEM_cells.m, where we have 16
% ensembles comprising 16 cells. Each cell has a generative model (i.e.,
% prior beliefs) about its possible local and global cell types (i.e.,
% internal, active or sensory). Given posterior beliefs about what sort of
% self it is at the local and global level, it can then predict the local
% and global intracellular signals it would expect to receive. The ensemble
% of ensembles then converges to a point attractor; where the ensemble has
% a Markov blanket and each element of the ensemble comprises a cell - that
% is itself a Markov blanket. The focus of this simulation is how the local
% level couples to the global level and vice versa. For simplicity (and
% computational expediency) we only model one ensemble at the local level
% and assume that the remaining ensembles conform to the same (local)
% dynamics. This is effectively a mean field approximation, where
% expectations of a cell in the first ensemble about its global type are
% coupled to the corresponding expectations and the ensemble level, and
% vice versa. The results of this simulation are provided in the form of a
% movie and graphs.The figure legend is included in the code below.
%
% In this example, we have used the same generative model at both levels to
% exploit the self similar hierarchical structure that emerges. However, we
% could have used different generative models at the global and local
% levels to simulate the morphogenesis of particular organelles that have a
% different form from their constituent cellular ensembles.
%__________________________________________________________________________
% Copyright (C) 2017 Wellcome Trust Centre for Neuroimaging
 
% Karl Friston
% $Id: DEM_cells_cells.m 7679 2019-10-24 15:54:07Z spm $


% preliminaries
%--------------------------------------------------------------------------
clear global
rng('default')
N         = 32;                              % duration of process

% generative process and model
%==========================================================================
M(1).E.d  = 1;                               % approximation order
M(1).E.n  = 2;                               % embedding order
M(1).E.s  = 1;                               % smoothness
M(1).E.dt = 2;                               % time step

% Target morphology (for both levels)
%==========================================================================
n     = [1 6 9];                             % internal, active and sensory
r     = [0 1 3];                             % radial distance from centre
pos   = [];                                  % position
typ   = [];                                  % cell type
for j = 1:length(n)
    for i = 1:n(j)
        pos(:,end + 1) = r(j)*[sin(2*pi*i/n(j));cos(2*pi*i/n(j))];
        typ(j,end + 1) = 1;
    end
end

% Target sensation - given the canonical location and types
%--------------------------------------------------------------------------
sen   = foxhound(pos,typ);
for i = 1:3
    P.sen(:,i) = mean(sen(:,find(typ(i,:))),2);
end

% check the predicted sensations are sufficiently orthogonal for inference
%--------------------------------------------------------------------------
disp(P.sen)

% initialise expectatio
    
function of Euclidean distance). The lower
% panels show the same results in a simpler format; namely, the evolution
% of subtype expectations (i.e., differentiation) at the local (left), and
% global (middle) level. The lower right panel shows the expectations of a
% single ensemble (the first) about its role at the global level. Here, the
% first ensemble is the internal state. Note the differentiation on both a
% local and global level; while local expectations about the cells' role
% the global level converge to the same (in general) type. In these
% simulations, we used a time step of two units (of arbitrary time) and a
% second order variational filtering scheme (heuristically, this is a
% second order generalisation of extended Kalman filtering) with hidden
% states corresponding to unknown identity in terms of cell type at the
% local and global level. Please see above for more details.
%==========================================================================  


% subroutines
%==========================================================================

% Generating sensations (self-signalling and extracellular signals)
%--------------------------------------------------------------------------
function g = Gg(x,v,a,P)

a       = spm_unvec(a,P);               % action
n       = size(a.l.pos,2);              % number of cells

% local level
%--------------------------------------------------------------------------
g.l.sec = a.l.sec;                      % secretion
g.l.sen = foxhound(a.l.pos,a.l.sec);    % sensation

% global level
%--------------------------------------------------------------------------
g.g.sec = a.g.sec;                      % secretion
g.g.sen = foxhound(a.g.pos,a.g.sec);    % sensation

% global to local coupling
%--------------------------------------------------------------------------
g.c.sec = a.c.sec;                      % secretion
g.c.sen = g.g.sen(:,1)*ones(1,n);       % sensation

% local to global coupling
%--------------------------------------------------------------------------
g.g.sec(:,1) = mean(g.c.sec,2);
g.g.sen(:,1) = mean(g.c.sen,2);

return

% Generating predictionsof cell signalling and extracellular signals
%--------------------------------------------------------------------------
function g = Mg(x,v,P)

% local level
%--------------------------------------------------------------------------
p       = spm_softmax(v.l);             % expected identity
g.l.sec = p;                            % secretion
g.l.sen = P.sen*p;                      % sensation

% global level
%--------------------------------------------------------------------------
p       = spm_softmax(v.g);             % expected identity
g.g.sec = p;                            % secretion
g.g.sen = P.sen*p;                      % sensation

% local predictions of global signals
%--------------------------------------------------------------------------
p       = spm_softmax(v.c);             % expected identity
g.c.sec = p;                            % secretion
g.c.sen = P.sen*p;                      % sensation


return

% Sensed signals
%--------------------------------------------------------------------------
function sen = foxhound(x,y)
% sen = sensation
% x   = position
% y   = cell type

[m,n] = size(y);
sen   = zeros(m,n);
k     = 2;
for i = 1:n
    for j = 1:n
        
        % distance between cells (local) or ensembles (global)
        %------------------------------------------------------------------
        d = x(:,i) - x(:,j);
        d = d'*d;
        
        % signal sensed
        %------------------------------------------------------------------
        if i ~= j
           sen(:,i) = sen(:,i) + exp(-k*d)*y(:,j);
        end