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: trainEMGMM.m
    path of file: ./repos/PESTO/private
    
    the code of the file until where you have to start completion: function [likelihoodOfTestSet, res] = trainEMGMM(sample, opt)
   % This function provides the interface for an EstimationMaximation (EM)
   % algorithm. A gaussian mixture displayModel (GMM) is trained to suit a given data
   % set. The E-step labels all data points related to one of the GMM displayModes.
   % The M-step updates the GMM parameters. The algorithm is initialized using
   % kmeans. Estimates the optimal number of GMM displayModes by measuing the
   % likelihoods of test data set given the trained GMM with
   % n-displayModes. The results of this function may be be used to predict
   % labels of future sets.
   %
   % OUTPUT: 
   %  likelihood: Likelihood for the prediction of the test set given the
   %     trained GMM for each opt.modeNumberCandidates.
   %  res       : The results for optimized GMM parameters for each
   %     opt.modeNumberCandidates.
   %
   % Written by Benjamin Ballnus (2017)
   
   %% Options
%    oldSeed=rng; oldSeed = oldSeed.Seed;
%    rng(opt.rng);

    
function [likelihoodOfTestSet, res] = trainEMGMM(sample, opt)
   % This function provides the interface for an EstimationMaximation (EM)
   % algorithm. A gaussian mixture displayModel (GMM) is trained to suit a given data
   % set. The E-step labels all data points related to one of the GMM displayModes.
   % The M-step updates the GMM parameters. The algorithm is initialized using
   % kmeans. Estimates the optimal number of GMM displayModes by measuing the
   % likelihoods of test data set given the trained GMM with
   % n-displayModes. The results of this function may be be used to predict
   % labels of future sets.
   %
   % OUTPUT: 
   %  likelihood: Likelihood for the prediction of the test set given the
   %     trained GMM for each opt.modeNumberCandidates.
   %  res       : The results for optimized GMM parameters for each
   %     opt.modeNumberCandidates.
   %
   % Written by Benjamin Ballnus (2017)
   
   %% Options
%    oldSeed=rng; oldSeed = oldSeed.Seed;
%    rng(opt.rng);
   nSample              = ceil(0.5 * opt.nSample);
   crossValFraction     = opt.crossValFraction;
   modeNumberCandidates = opt.modeNumberCandidates;
   displayMode          = opt.displayMode;
   maxEMiterations      = opt.maxEMiterations;
   nDim                 = opt.nDim;
   nSubsetSize          = opt.nSubsetSize;
   lowerB               = opt.lowerBound;
   upperB               = opt.upperBound;
   tolMu                = opt.tolMu;
   tolSigma             = opt.tolSigma;
   dimensionsToPlot     = opt.dimensionsToPlot;
   isInformative        = logical(opt.isInformative);
   
   
   %% Partition data into training and test set & initialization
   testSetIndices   = randperm(length(sample),floor(length(sample)*crossValFraction));
   testSetPositions = zeros(1,length(sample)); 
   testSetPositions(testSetIndices) = ones(1,length(testSetIndices));
   testSet = sample(logical(testSetPositions),:);
   sample = sample(~logical(testSetPositions),:);
   likelihoodOfTestSet = zeros(1,length(modeNumberCandidates));
   
   %% EM for multiple number of displayModes
   for nModes = modeNumberCandidates
      
      %% Refreshing initialization if nModes changes
      
      % WORKARROUND: Matlab automatically squeezes trailing dimensions with size 1 :(
      if nModes == 1
         setTrail = 1;
         nModes = 2;
      else
         setTrail = 0;
      end