diff --git a/.github/workflows/helpfiles-pages.yml b/.github/workflows/helpfiles-pages.yml new file mode 100644 index 0000000..c383736 --- /dev/null +++ b/.github/workflows/helpfiles-pages.yml @@ -0,0 +1,51 @@ +name: Helpfiles Pages + +on: + workflow_dispatch: + push: + branches: + - master + - main + paths: + - "helpfiles/**" + - ".github/workflows/helpfiles-pages.yml" + +concurrency: + group: pages + cancel-in-progress: true + +permissions: + contents: read + pages: write + id-token: write + +jobs: + deploy: + runs-on: ubuntu-latest + environment: + name: github-pages + url: ${{ steps.deployment.outputs.page_url }} + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Configure Pages + uses: actions/configure-pages@v5 + + - name: Build Pages artifact from helpfiles + run: | + mkdir -p _site + cp -R helpfiles/. _site/ + touch _site/.nojekyll + if [ ! -f _site/index.html ]; then + cp _site/NeuralSpikeAnalysis_top.html _site/index.html + fi + + - name: Upload artifact + uses: actions/upload-pages-artifact@v3 + with: + path: _site + + - name: Deploy to GitHub Pages + id: deployment + uses: actions/deploy-pages@v4 diff --git a/.github/workflows/python-ci.yml b/.github/workflows/python-ci.yml new file mode 100644 index 0000000..7c17d3c --- /dev/null +++ b/.github/workflows/python-ci.yml @@ -0,0 +1,34 @@ +name: Python CI + +on: + pull_request: + push: + branches: + - main + - master + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Install package and test dependencies + run: | + python -m pip install --upgrade pip + python -m pip install -e ./python[dev] + + - name: Run pytest + run: | + cd python + python -m pytest -q diff --git a/.gitignore b/.gitignore index 18a1148..7d0bcea 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,7 @@ .svn __pycache__/ *.pyc +python/plots/ + +python/docs/_build/ +python/.pytest_cache/ diff --git a/CIF.m b/CIF.m index 7cb8339..57743d3 100644 --- a/CIF.m +++ b/CIF.m @@ -1,9 +1,9 @@ -classdef CIF < handle - %CIF - Conditional Intensity function. - %methods - % - %Reference page in Help browser - %doc CIF +classdef CIF < handle + %CIF - Conditional Intensity function. + %CIF Example + % + %Reference page in Help browser + %CIF Reference Workflow % diff --git a/ConfidenceInterval.m b/ConfidenceInterval.m index c4070ce..f5715da 100644 --- a/ConfidenceInterval.m +++ b/ConfidenceInterval.m @@ -1,9 +1,9 @@ -classdef ConfidenceInterval < SignalObj -%ConfidenceInterval-represents the confidence interval for a time -%series or Covariate. -% methods -% Reference page in Help browser -% doc ConfidenceInterval +classdef ConfidenceInterval < SignalObj +%ConfidenceInterval-represents the confidence interval for a time +%series or Covariate. +% ConfidenceInterval Overview +% Reference page in Help browser +% ConfidenceInterval Reference % % nSTAT v1 Copyright (C) 2012 Masschusetts Institute of Technology diff --git a/ConfigColl.m b/ConfigColl.m index f6d8268..b35574f 100644 --- a/ConfigColl.m +++ b/ConfigColl.m @@ -1,14 +1,14 @@ -classdef ConfigColl < handle -%CONFIGCOLL - a collection of different configurations -% methods -% ConfigColl Examples -% -% see also TrialConfig, Trial, Analysis -% -% Reference page in Help browser -% doc ConfigColl +classdef ConfigColl < handle +%CONFIGCOLL - a collection of different configurations +% ConfigColl Reference +% ConfigColl Examples +% +% see also TrialConfig, CovColl, Analysis +% +% Reference page in Help browser +% ConfigColl Reference % diff --git a/CovColl.m b/CovColl.m index 407ee40..5ffd200 100644 --- a/CovColl.m +++ b/CovColl.m @@ -8,14 +8,14 @@ % the original covariates remain intact. The covariate collection % remembers the masked states, shifts, etc. so that these are applied % to the signal right before it is returned. -% -% methods -% CovColl Examples -% -% see also SignalObj, Covariate -% -% Reference page in Help browser -% doc CovColl +% +% CovColl Reference +% CovColl Examples +% +% see also SignalObj, Covariate +% +% Reference page in Help browser +% CovColl Reference % % diff --git a/Covariate.m b/Covariate.m index c099347..2052190 100644 --- a/Covariate.m +++ b/Covariate.m @@ -14,13 +14,13 @@ % can also just use cov for the standard representation % cov.getSigRep('zero-mean') is a zero mean version of the Signal % -% methods -% Covariate Examples -% -% see also SignalObj, CovColl -% -% Reference page in Help browser -% doc Covariate +% methods +% Covariate Examples +% +% see also SignalObj, CovColl +% +% Reference page in Help browser +% Covariate Reference % % nSTAT v1 Copyright (C) 2012 Masschusetts Institute of Technology diff --git a/DecodingAlgorithms.m b/DecodingAlgorithms.m index 86a575e..9327907 100644 --- a/DecodingAlgorithms.m +++ b/DecodingAlgorithms.m @@ -2,12 +2,12 @@ % DECODINGALGORITHMS A class that contains static functions for % decoding the hidden states of linear discrete stochastic systems or % hybrid linear discrete stochastic systems subject to gaussian noise. -% The observations can come from either a gaussian observation model -% or via a point process observation model. -% -% methods -% Reference page in Help browser -% doc DecodingAlgorithms +% The observations can come from either a gaussian observation model +% or via a point process observation model. +% +% Decoding Algorithms Reference +% Reference page in Help browser +% Decoding Algorithms Examples % diff --git a/Events.m b/Events.m index d549a3e..2824fc2 100644 --- a/Events.m +++ b/Events.m @@ -14,11 +14,11 @@ % % The length of eventTimes and eventLabels must match for an Events % object to be successfully created. -% methods -% Events Examples -% -% Reference page in Help browser -% doc Events +% methods +% Events Examples +% +% Reference page in Help browser +% Events Reference % @@ -186,4 +186,4 @@ - \ No newline at end of file + diff --git a/FitResSummary.m b/FitResSummary.m index 3f73bc5..ab3445c 100644 --- a/FitResSummary.m +++ b/FitResSummary.m @@ -4,11 +4,11 @@ % summary statistics across neurons. This is to allows visualization of % commonalities in the data across multiple neurons. % -% % methods -% see also FitResult +% % FitResSummary Reference +% see also FitResult % % Reference page in Help browser -% doc FitResSummary +% FitResSummary Reference % diff --git a/History.m b/History.m index e6b1cf1..f503cc5 100644 --- a/History.m +++ b/History.m @@ -9,10 +9,10 @@ % them in order and use them to specify windows in which we are % interested in computing the history; % -% methods -% History Examples -% Reference page in Help browser -% doc History +% methods +% History Examples +% Reference page in Help browser +% History Reference % diff --git a/README.md b/README.md index f76fb84..40236f1 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,36 @@ Although created with neural signal processing in mind, nSTAT can be used as a g Like all open-source projects, nSTAT will benefit from your involvement, suggestions and contributions. This platform is intended as a repository for extensions to the toolbox based on your code contributions as well as for flagging and tracking open issues. -The current release version of nSTAT can be downloaded from http://www.neurostat.mit.edu/nstat . +The current release version of nSTAT can be downloaded from https://www.neurostat.mit.edu/nstat . +Lab websites: +- Neuroscience Statistics Research Laboratory: https://www.neurostat.mit.edu +- RESToRe Lab: https://www.med.upenn.edu/cajigaslab/ + +Rendered help documentation (GitHub Pages): +- https://cajigaslab.github.io/nSTAT/ For mathematical and programmatic details of the toolbox, see: Cajigas I, Malik WQ, Brown EN. nSTAT: Open-source neural spike train analysis toolbox for Matlab. Journal of Neuroscience Methods 211: 245–264, Nov. 2012 http://doi.org/10.1016/j.jneumeth.2012.08.009 +PMID: 22981419 + +Paper-aligned toolbox map +------------------------- + +To keep terminology and workflows consistent with the 2012 toolbox paper, +the MATLAB help system includes a dedicated mapping page: + +- `helpfiles/PaperOverview.m` (published as `PaperOverview.html`) + +This page ties major toolbox components to the paper's workflow categories: + +- Class hierarchy and object model (`SignalObj`, `Covariate`, `Trial`, + `Analysis`, `FitResult`, `DecodingAlgorithms`) +- Fitting and assessment workflow (GLM fitting, diagnostics, summaries) +- Simulation workflow (conditional intensity and thinning examples) +- Decoding workflow (univariate/bivariate and history-aware decoding) +- Example-to-paper section mapping via `nSTATPaperExamples` If you use nSTAT in your work, please remember to cite the above paper in any publications. nSTAT is protected by the GPL v2 Open Source License. diff --git a/SignalObj.m b/SignalObj.m index cd772a0..e3eb294 100644 --- a/SignalObj.m +++ b/SignalObj.m @@ -50,11 +50,11 @@ % number of components in the data. % % -% methods -% SignalObj Examples -% -% Reference page in Help browser -% doc SignalObj +% methods +% SignalObj Examples +% +% Reference page in Help browser +% SignalObj Reference % diff --git a/Trial.m b/Trial.m index 7d1fa28..b646e1f 100644 --- a/Trial.m +++ b/Trial.m @@ -9,16 +9,16 @@ % covarColl: CovColl Object. Required. % nspikeColl: nstColl Obj % -% methods -% Trial Examples -% -% see also CovColl, -% Covariate, -% SignalObj, -% nspikeTrain -% -% Reference page in Help browser -% doc Trial +% methods +% Trial Examples +% +% see also CovColl, +% Covariate, +% SignalObj, +% nspikeTrain +% +% Reference page in Help browser +% Trial Reference % % nSTAT v1 Copyright (C) 2012 Masschusetts Institute of Technology diff --git a/TrialConfig.m b/TrialConfig.m index f6f00c5..2204450 100644 --- a/TrialConfig.m +++ b/TrialConfig.m @@ -4,15 +4,15 @@ % >>tcObj=TrialConfig(covMask,sampleRate, history,ensCovHist,covLag,name) % % All parameters are optional -% methods -% TrialConfig Examples -% -% see also Trial, -% CovColl, -% nstColl -% -% Reference page in Help browser -% doc TrialConfig +% methods +% TrialConfig Examples +% +% see also Trial, +% CovColl, +% nstColl +% +% Reference page in Help browser +% TrialConfig Reference % % nSTAT v1 Copyright (C) 2012 Masschusetts Institute of Technology diff --git a/helpfiles/Analysis.html b/helpfiles/Analysis.html index cc331e8..ecbdc6b 100644 --- a/helpfiles/Analysis.html +++ b/helpfiles/Analysis.html @@ -1,89 +1,134 @@ - - - - - Analysis

Contents

classdef Analysis
+      -->
+Analysis
+
+
+
+
+
+
+
+
+

Contents

+
+ +
+
+classdef Analysis
+
+
 % ANALYSIS Collection of functions (static methods) used for GLM analysis
 % of point process data.
+% <a href="matlab:nstatOpenHelpPage('AnalysisExamples.html')">Analysis Examples</a>
 %
-% <a href="AnalysisExamples.html">Analysis Examples</a>
-%
-% see also <a href="TrialExamples.html">Trial</a>, <a
-% href="CovCollExamples.html">CovColl</a>, <a
-% href="nstCollExamples.html">nstColl</a>,<a
-% href="HistoryExamples.html">History</a>
-%
+% see also <a href="matlab:nstatOpenHelpPage('TrialExamples.html')">Trial</a>, <a
+% href="matlab:nstatOpenHelpPage('CovCollExamples.html')">CovColl</a>, <a
+% href="matlab:nstatOpenHelpPage('nstCollExamples.html')">nstColl</a>,<a
+% href="matlab:nstatOpenHelpPage('HistoryExamples.html')">History</a>
 %
 % Reference page in Help browser
-% <a href="Analysis.html">Analysis Reference</a>
-
-properties (Constant)
+% <a href="matlab:nstatOpenHelpPage('Analysis.html')">Analysis Reference</a>
+
+

nSTAT v1 Copyright (C) 2012 Masschusetts Institute of Technology Cajigas, I, Malik, WQ, Brown, EN This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

+

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

+

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

+
properties (Constant)
     colors = {'b','g','r','c','m','y','k'};
 end
 
-    methods (Static)
-        function fitResults =RunAnalysisForNeuron(tObj,neuronNumber,configColl,makePlot,Algorithm)
-
            % fitResults =RunAnalysisForNeuron(tObj,neuronNumber,configColl,makePlot,Algorithm)
+    methods (Static)
+        function fitResults =RunAnalysisForNeuron(tObj,neuronNumber,configColl,makePlot,Algorithm,DTCorrection,batchMode)
+
+
            % fitResults =RunAnalysisForNeuron(tObj,neuronNumber,configColl,makePlot,Algorithm)
             % tObj: Trial to be analyzed
             % neuronNumber: number of the neuron to be analyzed. Can be a
             %               vector to specify multiple neurons to be analyzed.
@@ -99,11 +144,22 @@
             %            BNLRCG - faster Truncated, L-2 Regularized,
             %            Binomial Logistic Regression with Conjugate
             %            Gradient Solver by Demba Ba (demba@mit.edu).
+            % DTCorrection: 0 for no DT Correction of KS plot, 1 is the
+            %               default.
             %
-            if(nargin<5)
+            % batchMode: when batchMode=1 neurons with same name are fit at once rather than separetely
+
+            if(nargin<7 || isempty(batchMode))
+                batchMode = 0; %default treat each spike train separately
+            end
+
+            if(nargin<6 || isempty(DTCorrection))
+                DTCorrection =1;
+            end
+            if(nargin<5 || isempty(Algorithm))
                 Algorithm = 'GLM';
             end
-            if(nargin<4)
+            if(nargin<4 || isempty(makePlot))
                 makePlot=1;
             end
             numNeurons = length(neuronNumber);
@@ -117,43 +173,305 @@
             ensHistObj=cell(numNeurons,1);
             AIC   =zeros(numNeurons,1);
             BIC   =zeros(numNeurons,1);
+            logLL =zeros(numNeurons,1);
             windowSize = .01; % 1/tObj.sampleRate;% for Residual Computation;
             spikeTraining = cell(1,numNeurons);
             XvalData =cell(numNeurons,1);
             XvalTime =cell(numNeurons,1);
             spikeValidation = cell(1,numNeurons);
-

Fit Using Training Data

            if(diff(tObj.validationWindow)~=0)
+
+

Fit Using Training Data

+
            if(diff(tObj.validationWindow)~=0)
                 tObj.setTrialTimesFor('training');
             end
+            if(batchMode==1)
+               display('Running in batch mode: neurons with same name are fit simultaneously');
+            end
+
             for i=1:configColl.numConfigs
                 configColl.setConfig(tObj,i);
-                display(strcat('Analyzing Configuration #',num2str(i)));
-
-                for j=1:numNeurons
-
                    display(strcat('Analyzing Configuration #',num2str(i),': Neuron #',num2str(neuronNumber(j))));
-                    %clear tempLabels;
-                    %tObj.setCurrentNeuron(neuronNumber);
-                    otherLabels  = tObj.getLabelsFromMask(neuronNumber(j));
-%                     labels{j}{i}  = horzcat('Baseline',otherLabels); % Labels change depending on presence/absense of History or ensCovHist
-                    labels{j}{i}  = otherLabels; % Labels change depending on presence/absense of History or ensCovHist
-                    numHist{j}{i} = tObj.getNumHist;
-                    histObj{j}{i} = tObj.history;
-                    ensHistObj{j}{i} = tObj.ensCovHist;
-                    [lambdaTemp, bTemp, devTemp, statsTemp,AICTemp,BICTemp,distribTemp] = Analysis.GLMFit(tObj,neuronNumber(j),i,Algorithm);
-                    lambda{j}{i} = lambdaTemp; b{j}{i} = bTemp; stats{j}{i} = statsTemp;
-                    dev(j,i) = devTemp;  AIC(j,i)= AICTemp; BIC(j,i)= BICTemp;
-                    distrib{j}{i} =distribTemp;
-                    spikeTraining{j} = tObj.nspikeColl.getNST(neuronNumber(j)).nstCopy;
-                    spikeTraining{j}.setName(num2str(neuronNumber(j)));
-

Collect the validation Data

                    if(diff(tObj.validationWindow)~=0)
-                          tObj.setTrialTimesFor('validation');
-                          XvalData{j}{i}=tObj.getDesignMatrix(neuronNumber(j));
-                          XvalTime{j}{i}=tObj.covarColl.getCov(1).time;
-                          spikeValidation{j} = tObj.nspikeColl.getNST(neuronNumber(j)).nstCopy;
-                          spikeTraining{j}.setName(num2str(neuronNumber(j)));
-                          tObj.setTrialTimesFor('training')
+%                 fprintf(strcat('Analyzing Configuration #',num2str(i)));
+                pool = gcp('nocreate');
+                if(isempty(pool))
+                    pools = 0;
+                else
+                    pools = pool.NumWorkers;
+                end%matlabpool('size');
+                if(pools==0)
+                    if(batchMode==0)
+                        fprintf(strcat('Analyzing Configuration #',num2str(i),': Neuron #'));
+                        for j=1:numNeurons
+
+
        %                         fprintf(strcat('Analyzing Configuration #',num2str(i),': Neuron #',num2str(neuronNumber(j))));
+                            if(j==1)
+                                fprintf('%d',neuronNumber(j));
+                            else
+                                fprintf(',%d',neuronNumber(j));
+                            end
+                            %clear tempLabels;
+                            %tObj.setCurrentNeuron(neuronNumber);
+                            otherLabels  = tObj.getLabelsFromMask(neuronNumber(j));
+        %                     labels{j}{i}  = horzcat('Baseline',otherLabels); % Labels change depending on presence/absense of History or ensCovHist
+                            labels{j}{i}  = otherLabels; % Labels change depending on presence/absense of History or ensCovHist
+                            numHist{j}{i} = tObj.getNumHist;
+                            histObj{j}{i} = tObj.history;
+                            ensHistObj{j}{i} = tObj.ensCovHist;
+                            [lambdaTemp, bTemp, devTemp, statsTemp,AICTemp,BICTemp,logLLTemp, distribTemp] = Analysis.GLMFit(tObj,neuronNumber(j),i,Algorithm);
+                            lambda{j}{i} = lambdaTemp; b{j}{i} = bTemp; stats{j}{i} = statsTemp;
+                            dev(j,i) = devTemp;  AIC(j,i)= AICTemp; BIC(j,i)= BICTemp; logLL(j,i) = logLLTemp;
+                            distrib{j}{i} =distribTemp;
+                            spikeTraining{j} = tObj.nspikeColl.getNST(neuronNumber(j));%.nstCopy;
+                            spikeTraining{j}.setName(num2str(neuronNumber(j)));
+
+

Collect the validation Data

+
                            if(diff(tObj.validationWindow)~=0)
+                                  tObj.setTrialTimesFor('validation');
+                                  XvalData{j}{i}=tObj.getDesignMatrix(neuronNumber(j));
+                                  XvalTime{j}{i}=tObj.covarColl.getCov(1).time;
+                                  spikeValidation{j} = tObj.nspikeColl.getNST(neuronNumber(j));%.nstCopy;
+                                  spikeTraining{j}.setName(num2str(neuronNumber(j)));
+                                  tObj.setTrialTimesFor('training')
+                            end
+
+
                        end
+                    elseif(batchMode==1)
+                        neuronNames=neuronNumber; % This is an index of names in the batchMode case
+                        fprintf(strcat('Analyzing Configuration #',num2str(i),': Neuron #'));
+                        for j=1:numNeurons
+
+
        %                         if(isa(neuronNames,'cell'))
+        %                             fprintf(strcat('Analyzing Configuration #',num2str(i),': Neuron #'));
+        %                             display(strcat('Analyzing Configuration #',num2str(i),': Neuron #',neuronNames{j}));
+        %                         elseif(isa(neuronNames,'char'))
+        %                             display(strcat('Analyzing Configuration #',num2str(i),': Neuron #',neuronNames));
+        %                         elseif(isa(neuronNames,'double'))
+        %                             display(strcat('Analyzing Configuration #',num2str(i),': Neuron #',num2str(neuronNames)));
+        %                         end
+                            if(isa(neuronNames,'cell'))
+                                if(j==1)
+                                    fprintf('%s',neuronNames{j});
+                                else
+                                    fprintf(',%s',neuronNames{j});
+                                end
+                            elseif(isa(neuronNames,'char'))
+                                if(j==1)
+                                    fprintf('%s',neuronNames);
+                                else
+                                    fprintf(',%s',neuronNames);
+                                end
+                            elseif(isa(neuronNames,'double'))
+                                if(j==1)
+                                    fprintf('%d',neuronNames);
+                                else
+                                    fprintf(',%d',neuronNames);
+                                end
+                            end
+
+                            %clear tempLabels;
+                            %tObj.setCurrentNeuron(neuronNumber);
+                            otherLabels  = tObj.getLabelsFromMask(neuronNumber(j));
+        %                     labels{j}{i}  = horzcat('Baseline',otherLabels); % Labels change depending on presence/absense of History or ensCovHist
+                            labels{j}{i}  = otherLabels; % Labels change depending on presence/absense of History or ensCovHist
+                            numHist{j}{i} = tObj.getNumHist;
+                            histObj{j}{i} = tObj.history;
+                            ensHistObj{j}{i} = tObj.ensCovHist;
+                             if(isa(neuronNames,'cell'))
+                                 [lambdaTemp, bTemp, devTemp, statsTemp,AICTemp,BICTemp,logLLTemp,distribTemp] = Analysis.GLMFit(tObj,neuronNumber{j},i,Algorithm);
+                             elseif(isa(neuronNames,'char'))
+                                 [lambdaTemp, bTemp, devTemp, statsTemp,AICTemp,BICTemp,logLLTemp,distribTemp] = Analysis.GLMFit(tObj,neuronNumber,i,Algorithm);
+                             else
+                                [lambdaTemp, bTemp, devTemp, statsTemp,AICTemp,BICTemp,logLLTemp,distribTemp] = Analysis.GLMFit(tObj,neuronNumber(j),i,Algorithm);
+                             end
+
+                            lambda{j}{i} = lambdaTemp; b{j}{i} = bTemp; stats{j}{i} = statsTemp;
+                            dev(j,i) = devTemp;  AIC(j,i)= AICTemp; BIC(j,i)= BICTemp; logLL(j,i) = logLLTemp;
+                            distrib{j}{i} =distribTemp;
+                             if(isa(neuronNames,'cell'))
+                                currSpikes=tObj.nspikeColl.getNST(tObj.getNeuronIndFromName(neuronNames{j}));
+                             elseif(isa(neuronNames,'char'))
+                                currSpikes=tObj.nspikeColl.getNST(tObj.getNeuronIndFromName(neuronNames));
+                             else
+                                currSpikes=tObj.nspikeColl.getNST(neuronNames(j));
+                             end
+
+
+                            for n=1:length(currSpikes)
+                                if(isa(currSpikes,'cell'))
+                                     currSpikes{n} = currSpikes{n}.nstCopy;
+                                     if(isa(neuronNames,'cell'))
+                                        currSpikes{n}.setName(neuronNames{j});
+                                     elseif(isa(neuronNames,'char'))
+                                        currSpikes{n}.setName(neuronNames);
+                                     else
+                                        currSpikes{n}.setName(neuronNames(j));
+                                     end
+
+                                else
+                                    currSpikes = currSpikes.nstCopy;
+
+                                    if(isa(neuronNames,'cell'))
+                                        currSpikes.setName(neuronNames{j});
+                                    elseif(isa(neuronNames,'char'))
+                                        currSpikes.setName(neuronNames);
+                                    else
+                                        currSpikes.setName(neuronNames(j));
+                                    end
+
+                                end
+                            end
+
+                            spikeTraining{j} = currSpikes;
+
+

Collect the validation Data

+
                            if(diff(tObj.validationWindow)~=0)
+                                tObj.setTrialTimesFor('validation');
+                                tempIndices=tObj.getNeuronIndFromName(neuronNames{j});
+                                currSpikes=tObj.nspikeColl.getNST(tempIndices);
+                                tempX = [];
+                                tempTime=[];
+                                for n=1:length(tempIndices)
+                                    currSpikes{n} = currSpikes{n}.nstCopy;
+                                    currSpikes{n}.setName(neuronNames{j});
+                                    if(n==1)
+                                        tempX =tObj.getDesignMatrix(tempIndices(n));
+                                        tempTime =tObj.covarColl.getCov(1).time;
+                                    else
+                                        tempX = [tempX; tObj.getDesignMatrix(tempIndices(n))];
+                                        offset = max(tempTime)+1/tObj.sampeRate;
+                                        tempTime = [tempTime;(tObj.covarColl.getCov(1).time+offset)];
+                                    end
+                                end
+                                spikeValidation{j} = currSpikes;
+                                XvalData{j}{i}=tempX;
+                                XvalTime{j}{i}=tempTime;
+
+                                tObj.setTrialTimesFor('training')
+                            end
+
+
                        end
+                    end
+                    fprintf('\n');
+                else %use parallel toolbox
+                    if(batchMode==0)
+                        fprintf(strcat('Analyzing Configuration #',num2str(i),': Neuron #',num2str(neuronNumber)));
+                        for j=1:numNeurons
+
+
                            %clear tempLabels;
+                            %tObj.setCurrentNeuron(neuronNumber);
+                            otherLabels  = tObj.getLabelsFromMask(neuronNumber(j));
+        %                     labels{j}{i}  = horzcat('Baseline',otherLabels); % Labels change depending on presence/absense of History or ensCovHist
+                            labels{j}{i}  = otherLabels; % Labels change depending on presence/absense of History or ensCovHist
+                            numHist{j}{i} = tObj.getNumHist;
+                            histObj{j}{i} = tObj.history;
+                            ensHistObj{j}{i} = tObj.ensCovHist;
+                            [lambdaTemp, bTemp, devTemp, statsTemp,AICTemp,BICTemp,logLLTemp,distribTemp] = Analysis.GLMFit(tObj,neuronNumber(j),i,Algorithm);
+                            lambda{j}{i} = lambdaTemp; b{j}{i} = bTemp; stats{j}{i} = statsTemp;
+                            dev(j,i) = devTemp;  AIC(j,i)= AICTemp; BIC(j,i)= BICTemp; logLL(j,i)=logLLTemp;
+                            distrib{j}{i} =distribTemp;
+                            spikeTraining{j} = tObj.nspikeColl.getNST(neuronNumber(j));%.nstCopy;
+                            spikeTraining{j}.setName(num2str(neuronNumber(j)));
+
+

Collect the validation Data

+
                            if(diff(tObj.validationWindow)~=0)
+                                  tObj.setTrialTimesFor('validation');
+                                  XvalData{j}{i}=tObj.getDesignMatrix(neuronNumber(j));
+                                  XvalTime{j}{i}=tObj.covarColl.getCov(1).time;
+                                  spikeValidation{j} = tObj.nspikeColl.getNST(neuronNumber(j));%.nstCopy;
+                                  spikeTraining{j}.setName(num2str(neuronNumber(j)));
+                                  tObj.setTrialTimesFor('training')
+                            end
+
+
                        end
+                    elseif(batchMode==1)
+                        neuronNames=neuronNumber; % This is an index of names in the batchMode case
+                        fprintf(strcat('Analyzing Configuration #',num2str(i),': Neuron #',num2str(neuronNames)));
+                        for j=1:numNeurons
+
+
                            %clear tempLabels;
+                            %tObj.setCurrentNeuron(neuronNumber);
+                            otherLabels  = tObj.getLabelsFromMask(neuronNumber(j));
+        %                     labels{j}{i}  = horzcat('Baseline',otherLabels); % Labels change depending on presence/absense of History or ensCovHist
+                            labels{j}{i}  = otherLabels; % Labels change depending on presence/absense of History or ensCovHist
+                            numHist{j}{i} = tObj.getNumHist;
+                            histObj{j}{i} = tObj.history;
+                            ensHistObj{j}{i} = tObj.ensCovHist;
+                             if(isa(neuronNames,'cell'))
+                                 [lambdaTemp, bTemp, devTemp, statsTemp,AICTemp,BICTemp,logLLTemp,distribTemp] = Analysis.GLMFit(tObj,neuronNumber{j},i,Algorithm);
+                             elseif(isa(neuronNames,'char'))
+                                 [lambdaTemp, bTemp, devTemp, statsTemp,AICTemp,BICTemp,logLLTemp,distribTemp] = Analysis.GLMFit(tObj,neuronNumber,i,Algorithm);
+                             else
+                                [lambdaTemp, bTemp, devTemp, statsTemp,AICTemp,BICTemp,logLLTemp,distribTemp] = Analysis.GLMFit(tObj,neuronNumber(j),i,Algorithm);
+                             end
+
+                            lambda{j}{i} = lambdaTemp; b{j}{i} = bTemp; stats{j}{i} = statsTemp;
+                            dev(j,i) = devTemp;  AIC(j,i)= AICTemp; BIC(j,i)= BICTemp; logLL(j,i) = logLLTemp;
+                            distrib{j}{i} =distribTemp;
+                             if(isa(neuronNames,'cell'))
+                                currSpikes=tObj.nspikeColl.getNST(tObj.getNeuronIndFromName(neuronNames{j}));
+                             elseif(isa(neuronNames,'char'))
+                                currSpikes=tObj.nspikeColl.getNST(tObj.getNeuronIndFromName(neuronNames));
+                             else
+                                currSpikes=tObj.nspikeColl.getNST(neuronNames(j));
+                             end
+
+
+                            for n=1:length(currSpikes)
+                                if(isa(currSpikes,'cell'))
+                                     currSpikes{n} = currSpikes{n}.nstCopy;
+                                     if(isa(neuronNames,'cell'))
+                                        currSpikes{n}.setName(neuronNames{j});
+                                     elseif(isa(neuronNames,'char'))
+                                        currSpikes{n}.setName(neuronNames);
+                                     else
+                                        currSpikes{n}.setName(neuronNames(j));
+                                     end
+
+                                else
+                                    currSpikes = currSpikes.nstCopy;
+
+                                    if(isa(neuronNames,'cell'))
+                                        currSpikes.setName(neuronNames{j});
+                                    elseif(isa(neuronNames,'char'))
+                                        currSpikes.setName(neuronNames);
+                                    else
+                                        currSpikes.setName(neuronNames(j));
+                                    end
+
+                                end
+                            end
+
+                            spikeTraining{j} = currSpikes;
+
+

Collect the validation Data

+
                            if(diff(tObj.validationWindow)~=0)
+                                tObj.setTrialTimesFor('validation');
+                                tempIndices=tObj.getNeuronIndFromName(neuronNames{j});
+                                currSpikes=tObj.nspikeColl.getNST(tempIndices);
+                                tempX = [];
+                                tempTime=[];
+                                for n=1:length(tempIndices)
+                                    currSpikes{n} = currSpikes{n}.nstCopy;
+                                    currSpikes{n}.setName(neuronNames{j});
+                                    if(n==1)
+                                        tempX =tObj.getDesignMatrix(tempIndices(n));
+                                        tempTime =tObj.covarColl.getCov(1).time;
+                                    else
+                                        tempX = [tempX; tObj.getDesignMatrix(tempIndices(n))];
+                                        offset = max(tempTime)+1/tObj.sampeRate;
+                                        tempTime = [tempTime;(tObj.covarColl.getCov(1).time+offset)];
+                                    end
+                                end
+                                spikeValidation{j} = currSpikes;
+                                XvalData{j}{i}=tempX;
+                                XvalTime{j}{i}=tempTime;
+
+                                tObj.setTrialTimesFor('training')
+                            end
+
+
                        end
                     end
-
                end
+                    fprintf('\n');
+                end
             end
 
 
@@ -174,36 +492,43 @@
 %                 %tObj.setTrialTimesFor('training');
 %             end
 %
-

Store the results

            fitResults =cell(length(neuronNumber),1);
+
+

Store the results

+
            fitResults =cell(length(neuronNumber),1);
             for j=1:numNeurons
-
                fitResults{j}=FitResult(spikeTraining{j},labels{j},numHist{j},histObj{j},ensHistObj{j},lambda{j},b{j}, dev(j,:), stats{j},AIC(j,:),BIC(j,:),configColl,XvalData{j},XvalTime{j},distrib{j});
+
+
                fitResults{j}=FitResult(spikeTraining{j},labels{j},numHist{j},histObj{j},ensHistObj{j},lambda{j},b{j}, dev(j,:), stats{j},AIC(j,:),BIC(j,:),logLL(j,:),configColl,XvalData{j},XvalTime{j},distrib{j});
                 if(diff(tObj.validationWindow)~=0)
                     tObj.setTrialTimesFor('validation');
-                    lambdaValidation = fitResults{j}.computeValLambda;
-                    ValResults = FitResult(spikeValidation{j},labels{j},numHist{j},histObj{j},ensHistObj{j},lambdaValidation,b{j}, dev(j,:), stats{j},AIC(j,:),BIC(j,:),configColl,XvalData{j},XvalTime{j},distrib);
+                    [lambdaValidation, logLLValidation] = fitResults{j}.computeValLambda;
+                    ValResults = FitResult(spikeValidation{j},labels{j},numHist{j},histObj{j},ensHistObj{j},lambdaValidation,b{j}, dev(j,:), stats{j},AIC(j,:),BIC(j,:),logLLValidation,configColl,XvalData{j},XvalTime{j},distrib);
                     fitResults{j}.validation = ValResults; %validation field is actually another fitResults object but with the validation data
                 end
-

Process the results and compute further parameters

                if(makePlot==1)
+
+

Process the results and compute further parameters

+
                if(makePlot==1)
                     scrsz = get(0,'ScreenSize');
                     figure('Position',[scrsz(3)*.1 scrsz(4)*.1 scrsz(3)*.8 scrsz(4)*.8]);
-                    subplot(2,4,[1 2]);     Analysis.KSPlot(fitResults{j},makePlot); %make the plot
+                    subplot(2,4,[1 2]);     Analysis.KSPlot(fitResults{j},DTCorrection,makePlot); %make the plot
                     hold on; text(.45, .95,strcat('Neuron:',num2str(neuronNumber(j))));
                     subplot(2,4,3);         Analysis.plotInvGausTrans(fitResults{j},makePlot);
                     subplot(2,4,4);         Analysis.plotSeqCorr(fitResults{j});
                     subplot(2,4,[7 8]);     Analysis.plotFitResidual(fitResults{j},windowSize,makePlot);
                     subplot(2,4,[5 6]);     Analysis.plotCoeffs(fitResults{j});
                 else
-                    Analysis.KSPlot(fitResults{j},makePlot);
+                    Analysis.KSPlot(fitResults{j},DTCorrection,makePlot);
                     Analysis.plotInvGausTrans(fitResults{j},makePlot);
                     Analysis.plotFitResidual(fitResults{j},windowSize,makePlot);
                     %fitResults.computePlotParams;
                 end
-
            end
+
+
            end
             if(length(neuronNumber)==1)
                 fitResults = fitResults{1};
             end
-
        end
-        function fitResults = RunAnalysisForAllNeurons(tObj,configs,makePlot,Algorithm)
+
+
        end
+        function fitResults = RunAnalysisForAllNeurons(tObj,configs,makePlot,Algorithm,DTCorrection,batchMode)
             % fitResults = RunAnalysisForAllNeurons(tObj,configs,makePlot,Algorithm)
             % Runs the fits specifed by configs (a ConfigColl object) on
             % all the neurons that are unmasked in the trial tObj.
@@ -217,15 +542,30 @@
             %            BNLRCG - faster Truncated, L-2 Regularized,
             %            Binomial Logistic Regression with Conjugate
             %            Gradient Solver by Demba Ba (demba@mit.edu).
+            % DTCorrection: 0 for no DT Correction of KS plot, 1 is the
+            %               default.
+            % batchMode: when batchMode=1 neurons with same name are fit at once rather than separetely
 
-            if(nargin<4)
+            if(nargin<6 || isempty(batchMode))
+                batchMode = 0; %default treat each spike train separately
+            end
+            if(nargin<5 || isempty(DTCorrection))
+               DTCorrection =1;
+            end
+
+            if(nargin<4 || isempty(Algorithm))
                 Algorithm = 'GLM';
             end
-            if(nargin<3)
+            if(nargin<3 || isempty(makePlot))
                 makePlot=1; %default to plotting results
             end
 
-            neuronIndex=tObj.getNeuronIndFromMask;
+
+            if(batchMode==0)
+                neuronIndex=tObj.getNeuronIndFromMask;
+            elseif(batchMode==1)
+                neuronIndex=tObj.getUniqueNeuronNames;
+            end
 %             numLoops = floor(length(neuronIndex)/4);
 %             loopArray = cell(1,numLoops);
 %             for k=1:numLoops
@@ -237,13 +577,13 @@
 %             end
 
            % parfor i=1:length(neuronIndex)
-                fitResults = Analysis.RunAnalysisForNeuron(tObj,neuronIndex,configs,makePlot,Algorithm);
+                fitResults = Analysis.RunAnalysisForNeuron(tObj,neuronIndex,configs,makePlot,Algorithm,DTCorrection,batchMode);
             %end
 
         end
 
 
-        function [lambda,b, dev, stats,AIC, BIC,distribution] = GLMFit(tObj,neuronNumber,lambdaIndex,Algorithm)
+        function [lambda,b, dev, stats,AIC, BIC,logLL, distribution] = GLMFit(tObj,neuronNumber,lambdaIndex,Algorithm)
             % [lambda,b, dev, stats,AIC, BIC] = GLMFit(tObj,neuronNumber,lambdaIndex,Algorithm)
             % Given a trial, tObj, and a neuronNumber specifying a neuron
             % from the trial, extracts the design matrix X from the current
@@ -268,36 +608,87 @@
             %          (p-values,std dev, etc.)
             % AIC    - Akaike's information criteria for this regression.
             % BIC    - Bayes Information Criteria for this regression.
+            % logLL  - Log Likelihood evaluated with the fit parameters.
 
             if(nargin<4)
               Algorithm='GLM';
             end
+            if(isa(neuronNumber,'double'))
+                binaryRep=tObj.nspikeColl.getNST(neuronNumber).isSigRepBinary;
+                indices=neuronNumber;
+            elseif(isa(neuronNumber,'char'))
+                indices=tObj.getNeuronIndFromName(neuronNumber);
+                binRep=zeros(size(indices));
+                for i=1:length(indices)
+                   binRep(i)=tObj.nspikeColl.getNST(indices(i)).isSigRepBinary;
+                end
+                binaryRep=prod(binRep);
+            elseif(isa(neuronNumber,'cell'))
+                indices=tObj.getNeuronIndFromName(neuronNumber{1});
+                binRep=zeros(size(indices));
+                for i=1:length(indices)
+                   binRep(i)=tObj.nspikeColl.getNST(indices(i)).isSigRepBinary;
+                end
+                binaryRep=prod(binRep);
+            end
 
-            if(strcmp(Algorithm,'BNLRCG') && ~tObj.nspikeColl.getNST(neuronNumber).isSigRepBinary)
+            if(strcmp(Algorithm,'BNLRCG') && ~binaryRep)
                error('To use BNLRCG Algorithm, spikeTrain must have a binary representation. Increase sampleRate and try again');
             end
-                    %For a single neuron given covariates,perform the GLM fit.
-            y=tObj.getSpikeVector(neuronNumber);
-            X=tObj.getDesignMatrix(neuronNumber);
-
 
-            if(tObj.nspikeColl.getNST(neuronNumber).isSigRepBinary)
-                distribution = 'binomial';
-                linkfunction = 'logit';
-            else
-                distribution = 'poisson';
-                linkfunction = 'log';
-            end
+           %If performing batchMode analysis, this stacks up the
+           %corresponding spike vectors and the design matrices
+           for i=1:length(indices)
+               if(i==1)
+                    y=tObj.getSpikeVector(indices(i));
+                    X=tObj.getDesignMatrix(indices(i));
+                    lambdaTime = tObj.getCov(1).time;
+               else
+                    y=[y; tObj.getSpikeVector(indices(i))];
+                    X=[X; tObj.getDesignMatrix(indices(i))];
+                    offset = max(lambdaTime)+1/tObj.sampleRate;
+                    lambdaTime = [lambdaTime; (tObj.getCov(1).time +offset)];
+
+               end
+           end
+           %For a single neuron given covariates,perform the GLM fit.
+%
+%             if(binaryRep)
+%                 distribution = 'binomial';
+%                 linkfunction = 'logit';
+%             else
+%                 distribution = 'poisson';
+%                 linkfunction = 'log';
+%             end
 %             size(X)
 %             size(y)
+                y = y(:);
+                if(size(X,1)~=numel(y))
+                    nObs = min(size(X,1),numel(y));
+                    X = X(1:nObs,:);
+                    y = y(1:nObs);
+                    if(exist('lambdaTime','var') && ~isempty(lambdaTime))
+                        lambdaTime = lambdaTime(1:min(numel(lambdaTime),nObs));
+                    end
+                end
+
                 if(strcmp(Algorithm,'GLM'))
+                    distribution = 'poisson';
+                    linkfunction = 'log';
                     [b,dev,stats] = glmfit(X,y,distribution, 'link', linkfunction,'constant','off');
                 elseif(strcmp(Algorithm,'BNLRCG'))
+                    distribution = 'binomial';
+                    linkfunction = 'logit';
                     rrflag=0; %ML estimation
                     [b,dev,stats] = bnlrCG(X,y,rrflag);
+
                 else
                     error('Algorithm not supported!');
                 end
+                b=real(b); %make sure to avoid complex coefficients ... sometimes algorithms return
+                           %complex b with the imaginary part near zero.
+                           %Need to explore why. For now just keep the real
+                           %part.
                 if(length(b)>=1)
                     if(strcmp(distribution,'binomial'))
                         data = exp(X*b(1:end));
@@ -310,14 +701,44 @@
                     end
                 end
 
-%                 size(tObj.covarColl.getCov(1).getSigRep.time)
-%                 size(data)
 
-                lambda=Covariate(tObj.getCov(1).time,data,...
-                       '\Lambda(t)',tObj.getCov(1).xlabelval,...
-                        tObj.getCov(1).xunits,'Hz',strcat('\lambda_{',num2str(lambdaIndex),'}'));
-                AIC = 2*length(b)+dev;
-                BIC = length(b)*log(length(y))+dev;
+
+                lambdaIndexStr = num2str(lambdaIndex);
+
+                lambda=Covariate(lambdaTime,data,...
+                       '\lambda(t)',tObj.getCov(1).xlabelval,...
+                        tObj.getCov(1).xunits,'Hz',strcat('\lambda_{',lambdaIndexStr,'}'));
+                                mu=b;
+                        s=stats.se;
+%             Mc=30;
+%             for c=1:Mc
+%                 z=normrnd(0,1,length(s),1);
+%                 bKDraw(:,c)=mu+(s.*z);
+%             end
+%             if(strcmp(distribution,'poisson'))
+%                 lambdaDraw=exp(X*bKDraw)*(tObj.sampleRate);
+%             else
+%                 lambdaDraw=exp(X*bKDraw)./(1+exp(X*bKDraw))*(tObj.sampleRate);
+%             end
+%             lambdaDraw(isinf(lambdaDraw))=0;
+%             alphaVal=.05;
+%             for k=1:length(lambdaDraw)
+%               [f,x] = ecdf(squeeze(lambdaDraw(k,:)));
+%               CIs(k,1) = x(find(f<alphaVal/2,1,'last'));
+%               CIs(k,2) = x(find(f>(1-alphaVal/2),1,'first'));
+%             end
+%
+%
+%             ciPSTHGLM = ConfidenceInterval(lambdaTime,CIs,'CI_{psth_GLM}',lambda.xlabelval,lambda.xunits,lambda.yunits);
+%             lambda.setConfInterval(ciPSTHGLM);
+
+                %The deviance should be real since it a probability measure
+                %and therefore any imaginary part is ignored.
+                AIC = 2*length(b)+real(dev);
+                BIC = length(b)*log(length(y))+real(dev);
+                delta = 1/tObj.sampleRate;
+                logLL =sum(y.*log(data*delta)+(1-y).*(1-data*delta));
+
         end
         function handle = plotInvGausTrans(fitResults,makePlot)
             % handle = plotInvGausTrans(fitResults,makePlot)
@@ -354,6 +775,12 @@
             % intensity function.
             % The result is stored in fitResult.
             %
+            if(nargin<3 || isempty(makePlot))
+                makePlot=1;
+            end
+            if(nargin<2 || isempty(windowSize))
+                windowSize=.01;
+            end
             M = Analysis.computeFitResidual(fitResults.neuralSpikeTrain,fitResults.lambda,windowSize);
             fitResults.setFitResidual(M);
 
@@ -366,23 +793,29 @@
               fitResults.plotResidual;
             end
         end
-        function handle = KSPlot(fitResults,makePlot)
+        function handle = KSPlot(fitResults,DTCorrection,makePlot)
             %handle = KSPlot(fitResults,makePlot)
             % Computes the KS statistics and makes the plot. Stores
             % appropriate parameters in fitResults.
             % If validation data is also available, it does the same for
             % the validation data.
-            if(nargin <2)
+            % DTCorrection: 0 for no DT Correction of KS plot, 1 is the
+            %               default.
+            if(nargin <3)
                 makePlot =1; %By default make the plot
             end
+            if(nargin<2)
+               DTCorrection = 1;
+            end
 
-            [Z, U, xAxis, KSSorted, ks_stat] = Analysis.computeKSStats(fitResults.neuralSpikeTrain,fitResults.lambda);
+
+            [Z, U, xAxis, KSSorted, ks_stat] = Analysis.computeKSStats(fitResults.neuralSpikeTrain,fitResults.lambda,DTCorrection);
             fitResults.setKSStats(Z,U, xAxis, KSSorted, ks_stat);
 
 
             if(fitResults.isValDataPresent)
                  %make sure nst is in appropriate window
-                    [Z, U, xAxis, KSSorted, ks_stat] = Analysis.computeKSStats(fitResults.validation.neuralSpikeTrain,fitResults.validation.lambda);
+                    [Z, U, xAxis, KSSorted, ks_stat] = Analysis.computeKSStats(fitResults.validation.neuralSpikeTrain,fitResults.validation.lambda,DTCorrection);
                     fitResults.validation.setKSStats(Z, U, xAxis, KSSorted, ks_stat);
 
             end
@@ -419,39 +852,48 @@
             % for independence of the xj's. Independence of the xj's
             % suggests indepence of the uj's and zj's (a condition
             % necessary for the Time Rescaling Theorem).
-
             U=1-exp(-Z);
-            U(U>=1)=.99999; %Prevent any 1 values which lead to infinity in X
+            U(U>=.999999)=.999999; %Prevent any 1 values which lead to infinity in X
+            U(U==0)=.000001;
+            U(U<0)=.000001;
             X = norminv(U,0,1);
             %X=erfinv(U);
             [~,colm] = size(X);
-            for i=1:colm
-                [c(:,i),lags] = xcov(X(:,i));
+            if(~isempty(X))
+                for i=1:colm
+                    [c(:,i),lags] = xcov(X(:,i));
+                end
+            else
+                 [c,lags] = xcov(X);
             end
             index=find(lags==1);
             lags=lags(index:end);
             rho=c(index:end,:)./repmat(c(index-1,:),length(lags),1);
             n=length(X);
+            % Defaults to the 95% confidence intervals
+            % Can extend to allow selection of 95% or 99% CI
             confBound = 1.96/sqrt(n)*ones(length(lags),1);
-%              size(lags)
-%              size(rho)
+    %              size(lags)
+    %              size(rho)
 
             confBoundSig = SignalObj(lags,[confBound -confBound],'ACF[ \Phi^{-1}(u_i) ]','\Delta \tau','sec');
             confBoundSig.setPlotProps({' ''r'', ''LineWidth'' ,3'},1);
             confBoundSig.setPlotProps({' ''r'', ''LineWidth'' ,3'},2);
 
             handle=[];
-            rhoSig = SignalObj(lags,rho, 'ACF[ \Phi^-1(u_i) ]','\Delta \tau','sec');
+            rhoSig = SignalObj(lags,rho, 'ACF[ \Phi^-1(u_i) ]','Lag \Delta \tau','sec');
             plotProps = cell(1,colm);
-            for i=1:colm
-                plotProps{i}=strcat('''', '.',Analysis.colors{mod(i-1,length(Analysis.colors))+1},'''');
+            if(~isempty(X))
+                for i=1:colm
+                    plotProps{i}=strcat('''', '.',Analysis.colors{mod(i-1,length(Analysis.colors))+1},'''');
+                end
+            else
+                plotProps=strcat('''', '.',Analysis.colors{1},'''');
             end
             rhoSig.setPlotProps(plotProps);
 
-
-
         end
-        function [Z,U,xAxis,KSSorted, ks_stat] = computeKSStats(nspikeTrain,lambdaInput,dtCorrection)
+        function [Z,U,xAxis,KSSorted, ks_stat] = computeKSStats(nspikeObj,lambdaInput,DTCorrection)
             % [Z,U,xAxis,KSSorted, ks_stat] = computeKSStats(nspikeTrain,lambdaInput)
             % Given a neural spike train (a sequence of spike times) and a
             % conditional intensity function, computes the rescaled ISIs
@@ -460,6 +902,8 @@
             % (exponential rate 1 (according to T-R theorem) to be
             % uniform(0,1).
             %
+            % DTCorrection: 0 for no DT Correction of KS plot, 1 is the
+            %               default.
             % nspikeTrain: a nspikeTrain object
             % lambdaInput: candidate conditional intensity function (a Covariate)
             % Z: rescaled spike times
@@ -471,24 +915,46 @@
 
             %get the relevant spike train
             if(nargin<3)
-                dtCorrection =1;
+                DTCorrection =1;
             end
 
-            nCopy =nspikeTrain.nstCopy;
-%             minTime = nCopy.minTime;
-%             maxTime = nCopy.maxTime;
-%             nCopy.setMinTime(minTime);
-%             nCopy.setMaxTime(maxTime);
 
+            if(length(nspikeObj)>1) %in batch analysis we get multiple trials
+                nstCollObj = nstColl(nspikeObj);
+                nCopy = nstCollObj.toSpikeTrain;
 
+            else
+                nCopy =nspikeObj.nstCopy;
+%                   nCopy =nspikeObj;
+
+            end
+
+
+%             minTime = nCopy.minTime;
+%             maxTime = nCopy.maxTime;
+            nCopy.resample(lambdaInput.sampleRate);
+            nCopy.setMinTime(lambdaInput.minTime);
+            nCopy.setMaxTime(lambdaInput.maxTime);
+
+            repBin = nCopy.isSigRepBin;
+            if(~repBin)
+               lambdaInput=lambdaInput.resample(2*lambdaInput.sampleRate);
+               nCopy.resample(lambdaInput.sampleRate);
+            end
 
-            if(dtCorrection==1 && nCopy.isSigRepBin)
+            if(DTCorrection==1 && repBin)
                 % Use DT Correction for Time Rescaling Theorem - Haslinger, Pipa and Brown (2010)
-                pkSignal=lambdaInput.*(1/lambdaInput.sampleRate);
-                pk = pkSignal.data;
+                pkSignal=lambdaInput;
+                pk = pkSignal.data.*(1/lambdaInput.sampleRate);
+                pk = max(pk,1e-10);
                 spikeTrain = nCopy.getSigRep.data;
+                minDim = min(size(pk,1),size(spikeTrain,1));
+                pk=pk(1:minDim,:);
+                spikeTrain=spikeTrain(1:minDim,:);
+
                 intValues=zeros(length(nCopy.getSpikeTimes)-1,lambdaInput.dimension);
                 for i=1:lambdaInput.dimension
+                    pk(:,i) = nanmin(nanmax(pk(:,i),0),1);
                     temp = ksdiscrete(pk(:,i),spikeTrain,'spiketrain');
 %                     length(temp)
 %                     length(intValues(:,i))
@@ -506,16 +972,20 @@
 %                 lambda=tempLambda.getSigInTimeWindow(minTime,maxTime);%.dataToMatrix;
                 lambdaPosdata = max(tempLambda.data,0);
                 lambda = Covariate(tempLambda.time,lambdaPosdata,tempLambda.name,tempLambda.xlabelval,tempLambda.xunits,tempLambda.yunits,tempLambda.dataLabels);
-
                 lambdaInt = lambda.integral;
 
+
                 if(nCopy.isSigRepBin)
                     spikeTimes = nCopy.getSpikeTimes;
+                    spikeTimes = [0 spikeTimes];
 
                 else
-                    nstSignal = nCopy.getSigRep;
-                    spikeTimes=nstSignal.time(nstSignal.data~=0);
-
+%                     spikeTimes = nCopy.getSpikeTimes;
+%                     maxBinSize=nCopy.getMaxBinSizeBinary;
+%                     lambdaInt = lambda.resample(1/maxBinSize).integral;
+                     nstSignal = nCopy.getSigRep;
+                     spikeTimes=nstSignal.time(nstSignal.data~=0);
+                     spikeTimes = [0 spikeTimes'];
 
                 end
 
@@ -537,7 +1007,7 @@
 
 
             KSSorted = sort( U,'ascend' );
-            N = length(KSSorted);
+            N = size(KSSorted,1);
             if(N~=0)
                 xAxis=(([1:N]-.5)/N)'*ones(1,lambdaInput.dimension);
                 ks_stat = max(abs(KSSorted - (([1:N]-.5)/N)'*ones(1,lambdaInput.dimension)));
@@ -546,7 +1016,7 @@
                 xAxis=[];
             end
         end
-        function M=computeFitResidual(nspikeTrain,lambda,windowSize)
+        function M=computeFitResidual(nspikeObj,lambda,windowSize)
             % M=computeFitResidual(nspikeTrain,lambda,windowSize)
             % Computes the Point Process residual defined in
             % 'A point process framework for relating neural spiking
@@ -561,26 +1031,41 @@
             % M: the point process residual (a Covariate object).
             %
             if(nargin<3 || isempty(windowSize))
-                windowSize=.1;
-            end
-            windowTimes = nspikeTrain.minTime:windowSize:nspikeTrain.maxTime;
-            nCopy=nspikeTrain.nstCopy.getSigRep(windowSize);%tObj.getNeuron(fitResults.neuronNumber).nstCopy;
-            %nCopy.windowedSignal(windowTimes)
-            % this gets us the SUM over a window of length windowSize
-            % y[n] = y[n-1] + x[n] -- running sum filter
-            B=1; A=[1 -1];
-            sumSpikes = nCopy.filter(B,A);
-            %spikeTimes =nCopy.getSpikeTimes';
-            sumSpikesOverWindow= sumSpikes.getValueAt(windowTimes(2:end))-sumSpikes.getValueAt(windowTimes(1:(end-1)));
+                windowSize=.01;
+            end
+
+
+            if(length(nspikeObj)>1) %in batch analysis we get multiple trials
+                nstCollObj = nstColl(nspikeObj);
+                nCopy = nstCollObj.toSpikeTrain;
+            else
+                nCopy =nspikeObj.nstCopy;
+%                 nCopy =nspikeObj;
+            end
+
+            nCopy.resample(lambda.sampleRate);
+            nCopy.setMinTime(lambda.minTime);
+            nCopy.setMaxTime(lambda.maxTime);
+
+
+            sumSpikes=nCopy.getSigRep(windowSize);%tObj.getNeuron(fitResults.neuronNumber).nstCopy;
+%             sumSpikesOverWindow = sumSpikes.data(1:end);
+%             windowTimes = nCopy.minTime:windowSize:lambda.maxTime;
+            windowTimes = linspace(nCopy.minTime,nCopy.maxTime,length(sumSpikes.time));
             lambdaInt = lambda.integral;
             lambdaIntVals = lambdaInt.getValueAt(windowTimes(2:end))-lambdaInt.getValueAt(windowTimes(1:(end-1)));
+            if(length(lambdaIntVals)==length(sumSpikes.data))
+                sumSpikesOverWindow = sumSpikes.data(1:end);
+            elseif(length(lambdaIntVals)<length(sumSpikes.data))
+                sumSpikesOverWindow = sumSpikes.data(2:end);
+            end
             Mdata=repmat(sumSpikesOverWindow,[1 lambdaInt.dimension])-lambdaIntVals;
             dataLabels = cell(1,lambdaInt.dimension);
             for i=1:lambdaInt.dimension
                 dataLabels{i} = lambda.dataLabels{i};
             end
 
-            M=Covariate(windowTimes(2:end),Mdata,'M(t_k)-Residual',lambdaInt.xlabelval, ...
+            M=Covariate(windowTimes(1:end),[zeros(1,size(Mdata,2));Mdata],'M(t_k)',lambdaInt.xlabelval, ...
                         lambdaInt.xunits,lambdaInt.yunits,dataLabels);
 
         end
@@ -634,7 +1119,72 @@
             tcc = ConfigColl(tc);
             fitResults =Analysis.RunAnalysisForNeuron(ensembTrial,neuronNum,tcc,makePlot);
         end
-        function [fitResults,tcc] = computeHistLag(tObj,neuronNum,windowTimes,CovLabels,sampleRate,makePlot)
+
+        function [fitResults,gammaMat,phiMat,devianceMat,sigMat] = computeGrangerCausalityMatrix(tObj, Algorithm,confidenceInterval, batchMode)
+            if(nargin<4 || isempty(batchMode))
+                batchMode = 0;
+            end
+            if(nargin<3 || isempty(confidenceInterval))
+                confidenceInterval = 0.95;
+            end
+            if(nargin<2 || isempty(Algorithm))
+                Algorithm = 'GLM';
+            end
+
+            neuronNum = tObj.getNeuronIndFromMask;
+            covMask   = tObj.covMask;
+            history   = tObj.history;
+            ensCovHist = tObj.ensCovHist;
+            ensCovMask= tObj.ensCovMask;
+            sampleRate= tObj.sampleRate;
+            DTCorrection = 1;
+            makePlot=0;
+            clear fitResults;
+            gammaMat=zeros(length(neuronNum),length(neuronNum));
+            for i=1:length(neuronNum)
+                    tc{1}=TrialConfig(covMask, sampleRate, history, ensCovHist, ensCovMask); tc{1}.setName('Baseline');
+                    neighbors = find(ensCovMask(:,i)==1);
+
+                    for j=1:length(neighbors)
+                        ensCovMaskTemp = ensCovMask;
+                        ensCovMaskTemp(neighbors(j),neuronNum)=0;
+
+                        tc{2}=TrialConfig(covMask, sampleRate, history, ensCovHist, ensCovMaskTemp); tc{2}.setName([num2str(neighbors(j)) 'excluded from ' num2str(neuronNum(i))]);
+                        tcc = ConfigColl(tc);
+
+                        fitResults{i}{j} =Analysis.RunAnalysisForNeuron(tObj,neuronNum(i),tcc,makePlot,Algorithm,DTCorrection,batchMode);
+                    end
+            end
+            for i=1:length(neuronNum)
+                neighbors = find(ensCovMask(:,i)==1);
+                for j=1:length(neighbors)
+                    gammaMat(neighbors(j),neuronNum(i)) = fitResults{i}{j}.logLL(2)-fitResults{i}{j}.logLL(1);
+                    [coeffMat, labels, SEMat] = fitResults{i}{j}.getCoeffs(1);
+                    coeffInd=strfind(labels,[num2str(neighbors(j)) ':[']);
+                    gammaVals =coeffMat(~isempty(coeffInd));
+                    phiMat(neighbors(j),neuronNum(i)) = -sign(sum(gammaVals))*gammaMat(neighbors(j),neuronNum(i));
+                    dimDiff(neighbors(j),neuronNum(i)) = abs(diff(fitResults{i}{j}.numCoeffs));
+                    valConfInt(neighbors(j),neuronNum(i)) = chi2inv(confidenceInterval,dimDiff(neighbors(j),neuronNum(i)));
+                end
+            end
+            devianceMat = -2*gammaMat;
+            nonZeros = find(devianceMat~=0);
+            pVals = chi2cdf(devianceMat(nonZeros),dimDiff(nonZeros),'upper');
+
+
+            [h, crit_p, adj_p]=fdr_bh(pVals,1-confidenceInterval,'pdep','no');
+
+%             sigMat = (devianceMat>valConfInt);
+            sigMat = zeros(size(devianceMat));
+            sigMat(nonZeros) = h;
+
+            tObj.resetEnsCovMask;
+
+            %RunAnalysisForAllNeurons(tObj,configs,makePlot,Algorithm,DTCorrection,batchMode)
+
+
+        end
+        function [fitResults,tcc] = computeHistLag(tObj,neuronNum,windowTimes,CovLabels,Algorithm,batchMode,sampleRate,makePlot,histMinTimes,histMaxTimes)
             % [fitResults,tcc] = computeHistLag(tObj,tObj,neuronNum,windowTimes,CovLabels,sampleRate,makePlot)
             % For the neuron in neuronNum, runs an analysis with self
             % history, and no extrinsic covariates, and no ensemble history
@@ -642,12 +1192,24 @@
             % of windowTimes. There will be length(windowTimes) different
             % results (configurations) corresponding to increasing number of history
             % windows.
-            if(nargin<6)
+            if(nargin<10)
+                histMaxTimes =[];
+            end
+            if(nargin<9)
+                histMinTimes=[];
+            end
+            if(nargin<8)
                 makePlot=1;
             end
-            if(nargin<5 || isempty(sampleRate))
+            if(nargin<7 || isempty(sampleRate))
                 sampleRate = tObj.sampleRate;
             end
+            if(nargin<6 || isempty(batchMode))
+                batchMode = 0;
+            end
+            if(nargin<5 || isempty(Algorithm))
+                Algorithm = 'GLM';
+            end
             if(nargin<4)
                 CovLabels ={};
             end
@@ -658,29 +1220,50 @@
                 neuronNum = tObj.getNeuronIndFromMask;
             end
 
-            % tcObj=TrialConfig(covMask,sampleRate, history,minTime,maxTime)
+            % tcObj=TrialConfig(covMask,sampleRate, history,ensCovHist,ensCovMask,covLag,name)
             tc=cell(1,length(windowTimes)-1);
             for i=1:length(tc)+1
                 %use no covariates
                 if(i==1)
                     tc{i} = TrialConfig(CovLabels,sampleRate,[],[]); tc{i}.setName('Baseline');
                 else
-                    tc{i} = TrialConfig(CovLabels,sampleRate,windowTimes(1:i));
+                    if(and(isempty(histMinTimes),isempty(histMaxTimes)))
+                        tc{i} = TrialConfig(CovLabels,sampleRate,windowTimes(1:i)); tc{i}.setName(strcat('Window',num2str(i-1)));
+                    else
+                        hTemp = History(windowTimes(1:i),histMinTimes,histMaxTimes);
+                        tc{i} = TrialConfig(CovLabels,sampleRate,hTemp); tc{i}.setName(strcat('Window',num2str(i-1)));
+                    end
                 end
 
             end
+            DTCorrection=1;
             tcc = ConfigColl(tc);
-            fitResults =Analysis.RunAnalysisForNeuron(tObj,neuronNum,tcc,makePlot);
+
+            fitResults =Analysis.RunAnalysisForNeuron(tObj,neuronNum,tcc,makePlot,Algorithm,DTCorrection,batchMode);
+
         end
-        function fitResults = computeHistLagForAll(tObj,windowTimes,CovLabels,sampleRate,makePlot)
+        function fitResults = computeHistLagForAll(tObj,windowTimes,CovLabels,Algorithm,batchMode,sampleRate,makePlot,histMinTimes,histMaxTimes)
             % [fitResults,tcc] = computeHistLagAll(tObj,windowTimes,CovLabels,sampleRate,makePlot)
             % Calls computeHistLab for each neuron in the trial that is not masked.
-            if(nargin<5)
+            if(nargin<9)
+                histMaxTimes =[];
+            end
+            if(nargin<8)
+                histMinTimes=[];
+            end
+            if(nargin<7)
                 makePlot=1;
             end
-            if(nargin<4 || isempty(sampleRate))
+            if(nargin<6 || isempty(sampleRate))
                 sampleRate = tObj.sampleRate;
             end
+            if(nargin<5 || isempty(batchMode))
+                batchMode=0;
+            end
+            if(nargin<4 || isempty(Algorithm))
+                Algorithm = 'GLM';
+            end
+
             if(nargin<3)
                 CovLabels ={};
             end
@@ -691,7 +1274,7 @@
             neuronIndex=tObj.getNeuronIndFromMask;
             fitResults = cell(1,length(neuronIndex));
             for i=1:length(neuronIndex)
-               fitResults{i} = Analysis.computeHistLag(tObj,neuronIndex(i),windowTimes,CovLabels,sampleRate,makePlot);
+               fitResults{i} = Analysis.computeHistLag(tObj,neuronIndex(i),windowTimes,CovLabels,Algorithm,batchMode,sampleRate,makePlot,histMinTimes,histMaxTimes);
             end
 
 
@@ -769,49 +1352,11 @@
             cc=CovColl(cov);
 
         end
-        function plotStatHistData(index,spdata,svdata,sfdata)
-        % plotStatHistData(index,spdata,svdata,sfdata)
-        % Helpfer function to view historgrams of the position, velocity,
-        % and force data at each spike time.
-        % index is used to label the plot and should correspond to the
-        % neuron that was used to compute this data.
-        %
-        % position (x,y,z) array
-        % veloctity vx vy vz array
-        % force: fz, fzmag, fld, flf array
-
-        sfdata((sfdata==0))=nan; %
-            fig(1)=figure('visible','off'); a1=scatterhist(spdata(:,1),spdata(:,2));  set(get(gca,'YLabel'),'String','y'); set(get(gca,'XLabel'),'String','x')
-            fig(2)=figure('visible','off'); a2=scatterhist(spdata(:,1),spdata(:,3));  set(get(gca,'YLabel'),'String','z'); set(get(gca,'XLabel'),'String','x')
-            fig(3)=figure('visible','off'); a3=scatterhist(spdata(:,2),spdata(:,3));  set(get(gca,'YLabel'),'String','z'); set(get(gca,'XLabel'),'String','y')
-            fig(4)=figure('visible','off'); a4=scatterhist(svdata(:,1),svdata(:,2));  set(get(gca,'YLabel'),'String','v_y'); set(get(gca,'XLabel'),'String','v_x')
-            fig(5)=figure('visible','off'); a5=scatterhist(svdata(:,1),svdata(:,3));  set(get(gca,'YLabel'),'String','v_z'); set(get(gca,'XLabel'),'String','v_x')
-            fig(6)=figure('visible','off'); a6=scatterhist(svdata(:,2),svdata(:,3));  set(get(gca,'YLabel'),'String','v_z'); set(get(gca,'XLabel'),'String','v_y')
-            fig(7)=figure('visible','off'); a7=scatterhist(sfdata(:,1),sfdata(:,3));  set(get(gca,'YLabel'),'String','f_{ld}'); set(get(gca,'XLabel'),'String','f_z')
-            fig(8)=figure('visible','off'); a8=scatterhist(sfdata(:,1),sfdata(:,4));  set(get(gca,'YLabel'),'String','f_{lf}'); set(get(gca,'XLabel'),'String','f_z')
-            fig(9)=figure('visible','off'); a9=scatterhist(sfdata(:,3),sfdata(:,4));  set(get(gca,'YLabel'),'String','f_{lf}'); set(get(gca,'XLabel'),'String','f_{ld}')
-
-
-            h=figure(index+100);
-            scrsz = get(0,'ScreenSize');
-
-            set(h,'Name',strcat('Neuron #',num2str(index)),'Position',[scrsz(3)*.1 scrsz(4)*.1 scrsz(3)*.8 scrsz(4)*.8]);
-            u1 = uipanel(h,'position',[.0  .66 .33 .33]); set(a1,'parent',u1);
-            u2 = uipanel(h,'position',[.33 .66 .33 .33]); set(a2,'parent',u2);
-            u3 = uipanel(h,'position',[.66 .66 .33 .33]); set(a3,'parent',u3);
-            u4 = uipanel(h,'position',[.0  .33 .33 .33]); set(a4,'parent',u4);
-            u5 = uipanel(h,'position',[.33 .33 .33 .33]); set(a5,'parent',u5);
-            u6 = uipanel(h,'position',[.66 .33 .33 .33]); set(a6,'parent',u6);
-            u7 = uipanel(h,'position',[.0   0 .33 .33]); set(a7,'parent',u7);
-            u8 = uipanel(h,'position',[.33  0 .33 .33]); set(a8,'parent',u8);
-            u9 = uipanel(h,'position',[.66  0 .33 .33]); set(a9,'parent',u9);
-            close(fig);
-        end
-
-    end
-
 
 
+    end
+
+
 end
 
 
@@ -865,7 +1410,7 @@
 %     X = [ones(rows,1), X];
     % CG parameters
     cgmax = 30;
-    cgeps = 1e-3;
+    cgeps = 1e-6;
 
     % LR parameters
     lrmax = 100;
@@ -886,17 +1431,25 @@
     devnew = 0;
     devdiff = abs(devnew - devold);
 
-
+% B=[];
     while (i < lrmax && devdiff > lreps)
         % Do CG -> beta_new, i.e. solve for beta_new: XtWX*beta_new = XtWz(beta_old) using CG
 
         A = X'.*W*X; b = X'.*W*z;
 
+        % A needs to be positive definite so any zero or negative
+        % eigenvalues are set to machine precision.
+        [vec,val]=eig(A); val(val<=0)=eps;
+        A=vec*val*vec';
+
+        if(any(isnan(b)))
+           b(isnan(b))=0;
+        end
         if rrflag == 1
             A = A + lambda*eye(size(A));
         end
 
-        beta_new = cgs(A,b,cgeps,cgmax,[],[],beta_old);
+        [beta_new, flag] = cgs(A,b,cgeps,cgmax,[],[],beta_old);
         beta_old = beta_new;
 
         n = X*beta_old;
@@ -910,7 +1463,7 @@
 
         i = i+1;
 
-
+%         B=[B,beta_new];
     end
 
     % Compute a few statistics
@@ -1034,6 +1587,10 @@
 
     % check that those indicies are in [1:length(pk)];
 
+    if(isempty(spikeindicies))
+    	rst = pk;
+        return;
+    end
     if spikeindicies(1)<1;
          error('There is at least one spike with index less than 0');
     end;
@@ -1048,8 +1605,7 @@
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
     % initialize random number generator
-    s = RandStream('mt19937ar','Seed', sum(100*clock));
-    RandStream.setDefaultStream(s);
+    rng('shuffle','twister');
     %rand('twister',sum(100*clock));
 
     % make the qk's
@@ -1072,8 +1628,9 @@
 
         delta=-(1/qk(ind2))*log(1-rand()*(1-exp(-qk(ind2))));
 
-        total=total+qk(ind2)*delta;
-
+        if(delta~=0)
+            total=total+qk(ind2)*delta;
+        end
         rst(r)=total;
 
         rstold(r)=sum(qk(ind1+1:ind2));
@@ -1097,30 +1654,281 @@
 
     varargout{4}=sort(rstold);
 end
-
\ No newline at end of file +--> + + diff --git a/helpfiles/AnalysisExamples.html b/helpfiles/AnalysisExamples.html index ac6fc75..fd4af22 100644 --- a/helpfiles/AnalysisExamples.html +++ b/helpfiles/AnalysisExamples.html @@ -1,13 +1,18 @@ - - - - - Analysis Examples

Analysis Examples

This is an example on the standard approach to fitting GLM models to spike train data. This data set was obtained at the Society For Neuroscience '08 Workshop on Workshop on Neural Signal Processing Compare to analysis with Neural Spike Analysis Toolbox

Contents

Example 1: Tradition Preliminary Analysis

% Script glm_part1.m
+  
+
+
+
+

Analysis Examples

+ +

This is an example on the standard approach to fitting GLM models to spike train data. This data set was obtained at the Society For Neuroscience '08 Workshop on Workshop on Neural Signal Processing Compare to analysis with Neural Spike Analysis Toolbox +

+ +

Contents

+ +

Example 1: Tradition Preliminary Analysis

+
+% Script glm_part1.m
 % MATLAB code to visualize data, fit a GLM model of the relation between
 % spiking and the rat's position, and visualize this model for the
 % Neuroinformatics GLM problem set.
@@ -75,18 +101,30 @@
 % load the rat trajectory and spiking data;
 close all;
 warning off;
-load('glm_data.mat');
-

visualize the raw data

figure;
+installPath = which('nSTAT_Install');
+if isempty(installPath)
+    error('AnalysisExamples:MissingInstallPath', ...
+        'Could not locate nSTAT_Install.m on the MATLAB path.');
+end
+glmDataPath = fullfile(fileparts(installPath), 'data', 'glm_data.mat');
+load(glmDataPath);
+
+

visualize the raw data

+
figure;
 plot(xN,yN,x_at_spiketimes,y_at_spiketimes,'r.');
 axis tight square;
 xlabel('x position (m)'); ylabel('y position (m)');
-

fit a GLM model to the x and y positions.

[b,dev,stats] = glmfit([xN yN (xN.^2-mean(xN.^2)) (yN.^2-mean(yN.^2)) (xN.*yN-mean(xN.*yN))],spikes_binned,'poisson');
+
+

fit a GLM model to the x and y positions.

+
[b,dev,stats] = glmfit([xN yN (xN.^2-mean(xN.^2)) (yN.^2-mean(yN.^2)) (xN.*yN-mean(xN.*yN))],spikes_binned,'poisson');
 figure;
 errorbar(1:length(b), b, stats.se,'.');
 xticks=1:length(b);
 xtickLabels= {'baseline','x','y','x^2','y^2','x*y'};
 set(gca,'xtick',xticks,'xtickLabel',xtickLabels);
-

visualize your model construct a grid of positions to plot the model against...

figure;
+
+

visualize your model construct a grid of positions to plot the model against...

+
figure;
 [x_new,y_new]=meshgrid(-1:.1:1);
 y_new = flipud(y_new);
 x_new = fliplr(x_new);
@@ -104,12 +142,17 @@
 plot(xN,yN,x_at_spiketimes,y_at_spiketimes,'r.');
 axis tight square;
 xlabel('x position (m)'); ylabel('y position (m)');
-

Compare a linear model versus a Gaussian GLM model.

[b_lin,dev_lin,stats_lin] = glmfit([xN yN],spikes_binned,'poisson');
+
+

Compare a linear model versus a Gaussian GLM model.

+
[b_lin,dev_lin,stats_lin] = glmfit([xN yN],spikes_binned,'poisson');
 [b_quad,dev_quad,stats_quad] = glmfit([xN yN xN.^2 yN.^2 xN.*yN],spikes_binned,'poisson');
 
 lambdaEst_lin = exp( b_lin(1) + b_lin(2)*xN+b_lin(3)*yN);  % based on our GLM model with the log "link function"
 lambdaEst_quad = exp( b_quad(1) + b_quad(2)*xN+b_quad(3)*yN+b_quad(4)*xN.^2 +b_quad(5)*yN.^2 +b_quad(6)*xN.*yN);
-

Make the KS Plot

% *******  K-S Plot  *******************
+
+

Make the KS Plot

+
+% *******  K-S Plot  *******************
 % graph the K-S plot and confidence intervals for the K-S statistic
 
 %first generate the conditional intensity at each timestep
@@ -124,14 +167,14 @@
 lambdaInt = 0;
 j=0;
 KS=[];
-for t=1:length(spikes_binned),
+for t=1:length(spikes_binned)
     lambdaInt = lambdaInt + lambdaEst(t,:)*timestep;
-    if (spikes_binned(t)),
+    if (spikes_binned(t))
         j = j + 1;
         KS(j,:) = 1-exp(-lambdaInt);
         lambdaInt = [0 0];
-    end;
-end;
+    end
+end
 KSSorted = sort( KS );
 N = length( KSSorted);
 figure;
@@ -141,7 +184,14 @@
 ylabel('Empirical CDF of Rescaled ISIs');
 title('KS Plot with 95% Confidence Intervals');
 legend('Linear','Quadratic');
-
\ No newline at end of file +--> + + diff --git a/helpfiles/AnalysisExamples.m b/helpfiles/AnalysisExamples.m index 44c5784..0032221 100644 --- a/helpfiles/AnalysisExamples.m +++ b/helpfiles/AnalysisExamples.m @@ -14,10 +14,16 @@ % The code is initialized with an overly simple GLM model construction. % Please improve it! -% load the rat trajectory and spiking data; -close all; -warning off; -load('glm_data.mat'); +% load the rat trajectory and spiking data; +close all; +warning off; +installPath = which('nSTAT_Install'); +if isempty(installPath) + error('AnalysisExamples:MissingInstallPath', ... + 'Could not locate nSTAT_Install.m on the MATLAB path.'); +end +glmDataPath = fullfile(fileparts(installPath), 'data', 'glm_data.mat'); +load(glmDataPath); %% % visualize the raw data diff --git a/helpfiles/AnalysisExamples.png b/helpfiles/AnalysisExamples.png index b69c76a..0dae44f 100644 Binary files a/helpfiles/AnalysisExamples.png and b/helpfiles/AnalysisExamples.png differ diff --git a/helpfiles/AnalysisExamples2.html b/helpfiles/AnalysisExamples2.html index 4e57647..0a2f20e 100644 --- a/helpfiles/AnalysisExamples2.html +++ b/helpfiles/AnalysisExamples2.html @@ -1,13 +1,18 @@ - - - - - AnalysisExamples2

Contents

Analysis Examples 2

Compare with traditional Neural Spike Train Analysis here

% load the rat trajectory and spiking data;
+  
+
+
+
+

Contents

+ +

Analysis Examples 2

+

Compare with traditional Neural Spike Train Analysis here +

+
+% load the rat trajectory and spiking data;
 close all;
 warning off;
-load('glm_data.mat');
+installPath = which('nSTAT_Install');
+if isempty(installPath)
+    error('AnalysisExamples2:MissingInstallPath', ...
+        'Could not locate nSTAT_Install.m on the MATLAB path.');
+end
+glmDataPath = fullfile(fileparts(installPath), 'data', 'glm_data.mat');
+load(glmDataPath);
 
 nst = nspikeTrain(spiketimes);
 baseline = Covariate(T,ones(length(xN),1),'Baseline','time','s','',{'mu'});
@@ -78,14 +113,22 @@
 % could just define velocity = postion.derivative;
 
 %possibly add view as vector for covariates of dimension 3 or less
-

In the original analysis, we already had vectors of the covariates sampled at the spiketimes. This step would require interpolating the covariates and then sampling them at each of the spikeTimes. In our case this is quite simple.

[values_at_spiketimes] =position.getValueAt(spiketimes);
-

We could also upsample our data to get better estimates of the covariates at these points

[values_at_spiketimes] =position.resample(1/min(diff(spiketimes))).getValueAt(spiketimes);
-

visualize the raw data

figure;
+
+

In the original analysis, we already had vectors of the covariates sampled at the spiketimes. This step would require interpolating the covariates and then sampling them at each of the spikeTimes. In our case this is quite simple.

+
[values_at_spiketimes] =position.getValueAt(spiketimes);
+
+

We could also upsample our data to get better estimates of the covariates at these points

+
[values_at_spiketimes] =position.resample(1/min(diff(spiketimes))).getValueAt(spiketimes);
+
+

visualize the raw data

+
figure;
 plot(position.getSubSignal('x').dataToMatrix,position.getSubSignal('y').dataToMatrix,...
      values_at_spiketimes(:,1),values_at_spiketimes(:,2),'r.');
 axis tight square;
 xlabel('x position (m)'); ylabel('y position (m)');
-

Create a trial object and define the fits that we want to run

spikeColl = nstColl({nst});
+
+

Create a trial object and define the fits that we want to run

+
spikeColl = nstColl({nst});
 covarColl = CovColl({baseline,radial});
 trial     = Trial(spikeColl,covarColl);
 clear tc;
@@ -94,13 +137,18 @@
 tc{1} = TrialConfig({{'Baseline','mu'},{'Radial','x','y'}},sampleRate,[]); tc{1}.setName('Linear');
 tc{2} = TrialConfig({{'Baseline','mu'},{'Radial','x','y','x^2','y^2','x*y'}},sampleRate,[]); tc{2}.setName('Quadratic');
 tc{3} = TrialConfig({{'Baseline','mu'},{'Radial','x','y','x^2','y^2','x*y'}},sampleRate,[0 1]./sampleRate); tc{3}.setName('Quadratic+Hist');
-

Create our collection of configurations and run the analysis;

tcc = ConfigColl(tc); makePlot=1; neuronNum=1;
+
+

Create our collection of configurations and run the analysis;

+
tcc = ConfigColl(tc); makePlot=1; neuronNum=1;
 fitResults =Analysis.RunAnalysisForAllNeurons(trial,tcc,0);
 fitResults.plotResults;
-
Analyzing Configuration #1: Neuron #1
+
+
Analyzing Configuration #1: Neuron #1
 Analyzing Configuration #2: Neuron #1
 Analyzing Configuration #3: Neuron #1
-

Visualize the firing rates as a function of the spatial covariates

figure;
+
+

Visualize the firing rates as a function of the spatial covariates

+
figure;
 [x_new,y_new]=meshgrid(-1:.1:1); %define new x and y
 y_new = flipud(y_new);
 x_new = fliplr(x_new);
@@ -127,26 +175,33 @@
      values_at_spiketimes(:,1),values_at_spiketimes(:,2),'r.');
 axis tight square;
 xlabel('x position (m)'); ylabel('y position (m)');
-

Toolbox vs. Standard GLM comparison

Compare the results using our approach with the standard approach used in the first example previous standard regression

[b,dev,stats] = glmfit([xN yN xN.^2 yN.^2 xN.*yN],spikes_binned,'poisson');
+
+

Toolbox vs. Standard GLM comparison

+

Compare the results using our approach with the standard approach used in the first example previous standard regression

+
[b,dev,stats] = glmfit([xN yN xN.^2 yN.^2 xN.*yN],spikes_binned,'poisson');
 b-fitResults.b{2} % should be close to zero
-
+
+
 ans =
 
-    3.5041
-    0.0099
-    0.0102
-    0.0210
-    0.0215
-    0.0172
+    4.2365
+   -1.4577
+   -3.2263
+   -6.3420
+   -4.1806
+   -0.3615
 
-

Compute the history effect

sampleRate=1000;  makePlot=1; neuronNum = 1;
+
+

Compute the history effect

+
sampleRate=1000;  makePlot=1; neuronNum = 1;
 covLabels = {{'Baseline','mu'},{'Radial','x','y','x^2','y^2','x*y'}};
 Algorithm = 'GLM';
 batchMode=0;
 windowTimes =(0:1:10)./sampleRate;
 % [fitResults,tcc] = computeHistLag(tObj,neuronNum,windowTimes,CovLabels,Algorithm,batchMode,sampleRate,makePlot,histMinTimes,histMaxTimes)
 [fitResults,tcc] = Analysis.computeHistLag(trial,neuronNum,windowTimes,covLabels,Algorithm,batchMode,sampleRate,makePlot);
-
Analyzing Configuration #1: Neuron #1
+
+
Analyzing Configuration #1: Neuron #1
 Analyzing Configuration #2: Neuron #1
 Analyzing Configuration #3: Neuron #1
 Analyzing Configuration #4: Neuron #1
@@ -157,16 +212,29 @@
 Analyzing Configuration #9: Neuron #1
 Analyzing Configuration #10: Neuron #1
 Analyzing Configuration #11: Neuron #1
-
\ No newline at end of file +--> + + diff --git a/helpfiles/AnalysisExamples2.m b/helpfiles/AnalysisExamples2.m index 8ef8a8b..9f83872 100644 --- a/helpfiles/AnalysisExamples2.m +++ b/helpfiles/AnalysisExamples2.m @@ -2,10 +2,16 @@ % Compare with traditional Neural Spike Train Analysis % -% load the rat trajectory and spiking data; -close all; -warning off; -load('glm_data.mat'); +% load the rat trajectory and spiking data; +close all; +warning off; +installPath = which('nSTAT_Install'); +if isempty(installPath) + error('AnalysisExamples2:MissingInstallPath', ... + 'Could not locate nSTAT_Install.m on the MATLAB path.'); +end +glmDataPath = fullfile(fileparts(installPath), 'data', 'glm_data.mat'); +load(glmDataPath); nst = nspikeTrain(spiketimes); baseline = Covariate(T,ones(length(xN),1),'Baseline','time','s','',{'mu'}); diff --git a/helpfiles/AnalysisExamples2.png b/helpfiles/AnalysisExamples2.png index 745b81e..6fae7fb 100644 Binary files a/helpfiles/AnalysisExamples2.png and b/helpfiles/AnalysisExamples2.png differ diff --git a/helpfiles/AnalysisExamples2_01.png b/helpfiles/AnalysisExamples2_01.png index 6d62d35..7d0531b 100644 Binary files a/helpfiles/AnalysisExamples2_01.png and b/helpfiles/AnalysisExamples2_01.png differ diff --git a/helpfiles/AnalysisExamples2_02.png b/helpfiles/AnalysisExamples2_02.png index e687798..10c01b4 100644 Binary files a/helpfiles/AnalysisExamples2_02.png and b/helpfiles/AnalysisExamples2_02.png differ diff --git a/helpfiles/AnalysisExamples2_03.png b/helpfiles/AnalysisExamples2_03.png index 01c4694..d8c623d 100644 Binary files a/helpfiles/AnalysisExamples2_03.png and b/helpfiles/AnalysisExamples2_03.png differ diff --git a/helpfiles/AnalysisExamples2_04.png b/helpfiles/AnalysisExamples2_04.png index 75b6470..25428f9 100644 Binary files a/helpfiles/AnalysisExamples2_04.png and b/helpfiles/AnalysisExamples2_04.png differ diff --git a/helpfiles/AnalysisExamples2_05.png b/helpfiles/AnalysisExamples2_05.png new file mode 100644 index 0000000..ad32af9 Binary files /dev/null and b/helpfiles/AnalysisExamples2_05.png differ diff --git a/helpfiles/AnalysisExamples_01.png b/helpfiles/AnalysisExamples_01.png index a00c49f..9370669 100644 Binary files a/helpfiles/AnalysisExamples_01.png and b/helpfiles/AnalysisExamples_01.png differ diff --git a/helpfiles/AnalysisExamples_02.png b/helpfiles/AnalysisExamples_02.png index 06465b0..b129ab2 100644 Binary files a/helpfiles/AnalysisExamples_02.png and b/helpfiles/AnalysisExamples_02.png differ diff --git a/helpfiles/AnalysisExamples_03.png b/helpfiles/AnalysisExamples_03.png index 132e7a6..7d77fbc 100644 Binary files a/helpfiles/AnalysisExamples_03.png and b/helpfiles/AnalysisExamples_03.png differ diff --git a/helpfiles/AnalysisExamples_04.png b/helpfiles/AnalysisExamples_04.png index e4e81f1..3851d41 100644 Binary files a/helpfiles/AnalysisExamples_04.png and b/helpfiles/AnalysisExamples_04.png differ diff --git a/helpfiles/ClassDefinitions.html b/helpfiles/ClassDefinitions.html index 65ecab9..30df6ea 100644 --- a/helpfiles/ClassDefinitions.html +++ b/helpfiles/ClassDefinitions.html @@ -1,13 +1,18 @@ - - - - - Class Definitions +

Summaries of each of the available classes along with corresponding source code can be accessed below:

+ + + +
+ \ No newline at end of file +--> + + diff --git a/helpfiles/ConfidenceIntervalOverview.html b/helpfiles/ConfidenceIntervalOverview.html index 2df194f..c31a17b 100644 --- a/helpfiles/ConfidenceIntervalOverview.html +++ b/helpfiles/ConfidenceIntervalOverview.html @@ -1,23 +1,124 @@ - - + + - - ConfidenceInterval + + +ConfidenceInterval + + + + + -

ConfidenceInterval

-

- The ConfidenceInterval class extends SignalObj - and represents lower and upper confidence bounds over time. -

-

Related Documentation

- -

Source File

- +
+

ConfidenceInterval

+

The `ConfidenceInterval` class extends `SignalObj` and represents lower and upper confidence bounds over time.

+

Related documentation:

+ +

Source file:

+ + +
+ diff --git a/helpfiles/ConfigCollExamples.html b/helpfiles/ConfigCollExamples.html index 7358c51..325247b 100644 --- a/helpfiles/ConfigCollExamples.html +++ b/helpfiles/ConfigCollExamples.html @@ -1,13 +1,18 @@ - - - - - ConfigColl Examples

ConfigColl Examples

tcObj=TrialConfig(covMask,sampleRate, history,minTime,maxTime)

tc1 = TrialConfig({'Force','f_x'},2000,[.1 .2],-1,2);
+  
+
+
+
+

ConfigColl Examples

+

tcObj=TrialConfig(covMask,sampleRate, history,minTime,maxTime)

+
tc1 = TrialConfig({'Force','f_x'},2000,[.1 .2],-1,2);
 tc2 = TrialConfig({'Position','x'},2000,[.1 .2],-1,2);
 tcc = ConfigColl({tc1,tc2});
-
\ No newline at end of file +--> + + diff --git a/helpfiles/CovCollExamples.html b/helpfiles/CovCollExamples.html index ed7af83..c5c5b6b 100644 --- a/helpfiles/CovCollExamples.html +++ b/helpfiles/CovCollExamples.html @@ -1,13 +1,18 @@ - - - - - Test CovColl

Test CovColl

close all;
+  
+
+
+
+

Test CovColl

+
close all;
 load CovariateSample.mat;
 cc=CovColl({position,force});
 figure; cc.plot; %plots all covariates and their components
@@ -84,7 +96,14 @@
 % setMinTime
 % removeCovariate
 % nActCovar
-
\ No newline at end of file +--> + + diff --git a/helpfiles/CovCollExamples.png b/helpfiles/CovCollExamples.png index ae12678..fabacbd 100644 Binary files a/helpfiles/CovCollExamples.png and b/helpfiles/CovCollExamples.png differ diff --git a/helpfiles/CovCollExamples_01.png b/helpfiles/CovCollExamples_01.png index 7f64977..4d6dad0 100644 Binary files a/helpfiles/CovCollExamples_01.png and b/helpfiles/CovCollExamples_01.png differ diff --git a/helpfiles/CovCollExamples_02.png b/helpfiles/CovCollExamples_02.png index 4cc8276..4049915 100644 Binary files a/helpfiles/CovCollExamples_02.png and b/helpfiles/CovCollExamples_02.png differ diff --git a/helpfiles/CovariateExamples.html b/helpfiles/CovariateExamples.html index 232a0f4..1390909 100644 --- a/helpfiles/CovariateExamples.html +++ b/helpfiles/CovariateExamples.html @@ -1,68 +1,96 @@ - - - - - Test the Cov class

Test the Cov class

Covariates are just like signals with a mean and a standard deviation They have two representations, the default (original representation) and a zero-mean representation

Contents

Example 1: Using Covariates

Create some Data

close all;
+
+
+
+
+
+Test the Cov class
+
+
+
+
+
+
+
+
+

Test the Cov class

+ +

Covariates are just like signals with a mean and a standard deviation They have two representations, the default (original representation) and a zero-mean representation

+ +

Contents

+ +

Example 1: Using Covariates

+

Create some Data

+
close all;
 t=0:.01:5; t=t';
 x=exp(-t);
 y=sin(2*pi*t);
@@ -70,7 +98,9 @@
 
 fx=abs(y);
 fy=abs(y).^2;
-

Define labels and plotting properties for each Covariate

dLabels1={'f_x','f_y'};
+
+

Define labels and plotting properties for each Covariate

+
dLabels1={'f_x','f_y'};
 dLabels2={'x','y','z'};
 
 plotProps = {{' ''g'', ''LineWidth'' ,.5'},... %for x
@@ -79,7 +109,9 @@
 
 force = Covariate(t, [fx fy], 'Force', 'time', 's', 'N', dLabels1);
 position=Covariate(t,[x y z], 'Position','time','s','cm', dLabels2);
-

Plot the covariates and change their properties

position.getSigRep.plot('all',plotProps); %same as position.plot
+
+

Plot the covariates and change their properties

+
position.getSigRep.plot('all',plotProps); %same as position.plot
 plotPropsForce = {{' ''b'' '},{' ''k'' '}};
 figure;
 subplot(1,2,1); force.getSigRep.plot('all',plotPropsForce);
@@ -88,8 +120,14 @@
 % >>force.plot;
 
 subplot(1,2,2); force.getSigRep('zero-mean').plot('all',plotPropsForce);
-
\ No newline at end of file +--> + + diff --git a/helpfiles/CovariateExamples.png b/helpfiles/CovariateExamples.png index d8373eb..b326a37 100644 Binary files a/helpfiles/CovariateExamples.png and b/helpfiles/CovariateExamples.png differ diff --git a/helpfiles/CovariateExamples_01.png b/helpfiles/CovariateExamples_01.png index 18cf178..c969068 100644 Binary files a/helpfiles/CovariateExamples_01.png and b/helpfiles/CovariateExamples_01.png differ diff --git a/helpfiles/CovariateExamples_02.png b/helpfiles/CovariateExamples_02.png index accab20..b8bcc49 100644 Binary files a/helpfiles/CovariateExamples_02.png and b/helpfiles/CovariateExamples_02.png differ diff --git a/helpfiles/DecodingExample.html b/helpfiles/DecodingExample.html index 2f1f35e..08fee27 100644 --- a/helpfiles/DecodingExample.html +++ b/helpfiles/DecodingExample.html @@ -1,13 +1,18 @@ - - - - - STIMULUS DECODING

STIMULUS DECODING

In this example we show how to decode a univariate and a bivariate stimulus based on a point process observations using nSTAT. Even though due to the simulated nature of the data, we know the exact condition intensity function, we estimate the parameters before moving on to the decoding stage.

Contents

Generate the conditional Intensity Function

    close all;
+  
+
+
+
+

STIMULUS DECODING

+ +

In this example we show how to decode a univariate and a bivariate stimulus based on a point process observations using nSTAT. Even though due to the simulated nature of the data, we know the exact condition intensity function, we estimate the parameters before moving on to the decoding stage.

+ +

Contents

+ +

Generate the conditional Intensity Function

+
    close all;
     delta = 0.001; Tmax = 10;
     time = 0:delta:Tmax;
     f=.1; b1=1;b0=-3;
@@ -80,7 +107,9 @@
     figure;
     subplot(2,1,1); spikeColl.plot;
     subplot(2,1,2); lambda.plot;
-

Fit a model to the spikedata to obtain a model CIF

stim = Covariate(time,sin(2*pi*f*time),'Stimulus','time','s','V',{'stim'});
+
+

Fit a model to the spikedata to obtain a model CIF

+
stim = Covariate(time,sin(2*pi*f*time),'Stimulus','time','s','V',{'stim'});
 baseline = Covariate(time,ones(length(time),1),'Baseline','time','s','',...
                     {'constant'});
 figure;
@@ -104,9 +133,13 @@
 
 paramEst = squeeze(Summary.bAct(:,2,:));
 meanParams = mean(paramEst,2);
-
Analyzing Configuration #1: Neuron #1,2,3,4,5,6,7,8,9,10
+
+
Analyzing Configuration #1: Neuron #1,2,3,4,5,6,7,8,9,10
 Analyzing Configuration #2: Neuron #1,2,3,4,5,6,7,8,9,10
-

So we now have a model for lambda lambda = exp(b_0 + b_1*x(t))./(1+exp(b_0 + b_1*x(t)) * 1/delta because exp(b_0 + b_1*x(t))<<1 we can approximate this lambda by just the numerator i.e. lambda = exp(b_0 + b_1*x(t))./delta

Now suppose we wanted to decode x(t) based on only having observed lambda

clear lambdaCIF;
+
+

So we now have a model for lambda lambda = exp(b_0 + b_1*x(t))./(1+exp(b_0 + b_1*x(t)) * 1/delta because exp(b_0 + b_1*x(t))<<1 we can approximate this lambda by just the numerator i.e. lambda = exp(b_0 + b_1*x(t))./delta

+

Now suppose we wanted to decode x(t) based on only having observed lambda

+
clear lambdaCIF;
 b0=paramEst(1,:);
 b1=paramEst(2,:);
 for i=1:numRealizations
@@ -133,8 +166,16 @@
 legend([hEst(1) hEst(2) hEst(3) hStim],'x_{k|k}(t)',strcat('x_{k|k}(t)-',num2str(zVal),'\sigma_{k|k}'),...
         strcat('x_{k|k}(t)+',num2str(zVal),'\sigma_{k|k}'),'x_{k|k}(t)','x(t)');
 title(['Decoded Stimulus +/- 99% confidence intervals using ' num2str(numRealizations) ' cells']);
-
Warning: Ignoring extra legend entries. 
-
\ No newline at end of file +--> + + diff --git a/helpfiles/DecodingExample.png b/helpfiles/DecodingExample.png index 0fb71d5..a912e40 100644 Binary files a/helpfiles/DecodingExample.png and b/helpfiles/DecodingExample.png differ diff --git a/helpfiles/DecodingExampleWithHist.html b/helpfiles/DecodingExampleWithHist.html index 3a72196..82a3ec5 100644 --- a/helpfiles/DecodingExampleWithHist.html +++ b/helpfiles/DecodingExampleWithHist.html @@ -1,13 +1,18 @@ - - - - - 1-D Stimulus Decode with History Effect

1-D Stimulus Decode with History Effect

In the above decoding example, the simulated neurons did not have memory. That is their previous firing activity did not modulate their current probability of firing. In reality the firing history does affect the probabilty of neuronal firing (eg. refractory period, bursting, etc.). In this example, we simulate a population a neurons that exhibit this type of history dependence. We then decode the stimulus activity based on a conditional intensity function that includes the correct history terms and one that assumes no history dependence.

close all;
+  
+
+
+
+

1-D Stimulus Decode with History Effect

+

In the above decoding example, the simulated neurons did not have memory. That is their previous firing activity did not modulate their current probability of firing. In reality the firing history does affect the probabilty of neuronal firing (eg. refractory period, bursting, etc.). In this example, we simulate a population a neurons that exhibit this type of history dependence. We then decode the stimulus activity based on a conditional intensity function that includes the correct history terms and one that assumes no history dependence.

+
close all;
 % clear all;
 delta = 0.001; Tmax = 1;
 time = 0:delta:Tmax;
@@ -142,9 +155,18 @@
 legend([hEst(1) hEst(2) hEst(3) hStim],'x_{k|k}(t)',strcat('x_{k|k}(t)-',num2str(zVal),'\sigma_{k|k}'),...
         strcat('x_{k|k}(t)+',num2str(zVal),'\sigma_{k|k}'),'x_{k|k}(t)','x(t)');
 title(['Decoded Stimulus No Hist +/- 99% confidence intervals using ' num2str(numRealizations) ' cells']);
-
Warning: Ignoring extra legend entries. 
+
+
Warning: Ignoring extra legend entries. 
 Warning: Ignoring extra legend entries. 
-

We see that inclusion of history effect improves (as expected) the decoding of the stimulus based on the point process observations

\ No newline at end of file +--> + + diff --git a/helpfiles/DecodingExampleWithHist.png b/helpfiles/DecodingExampleWithHist.png index 7538143..af79f35 100644 Binary files a/helpfiles/DecodingExampleWithHist.png and b/helpfiles/DecodingExampleWithHist.png differ diff --git a/helpfiles/DecodingExampleWithHist_01.png b/helpfiles/DecodingExampleWithHist_01.png index fbcf08b..a20f4c0 100644 Binary files a/helpfiles/DecodingExampleWithHist_01.png and b/helpfiles/DecodingExampleWithHist_01.png differ diff --git a/helpfiles/DecodingExampleWithHist_02.png b/helpfiles/DecodingExampleWithHist_02.png index 43b8c34..61d1f15 100644 Binary files a/helpfiles/DecodingExampleWithHist_02.png and b/helpfiles/DecodingExampleWithHist_02.png differ diff --git a/helpfiles/DecodingExample_01.png b/helpfiles/DecodingExample_01.png index dc7a0ea..f82b7b8 100644 Binary files a/helpfiles/DecodingExample_01.png and b/helpfiles/DecodingExample_01.png differ diff --git a/helpfiles/DecodingExample_02.png b/helpfiles/DecodingExample_02.png index 58d3b21..ad719e0 100644 Binary files a/helpfiles/DecodingExample_02.png and b/helpfiles/DecodingExample_02.png differ diff --git a/helpfiles/DecodingExample_03.png b/helpfiles/DecodingExample_03.png index 25cf34e..392313e 100644 Binary files a/helpfiles/DecodingExample_03.png and b/helpfiles/DecodingExample_03.png differ diff --git a/helpfiles/DecodingExample_04.png b/helpfiles/DecodingExample_04.png index c3236b7..9f20d14 100644 Binary files a/helpfiles/DecodingExample_04.png and b/helpfiles/DecodingExample_04.png differ diff --git a/helpfiles/DecodingExample_05.png b/helpfiles/DecodingExample_05.png index 7a59be9..f1dfd1e 100644 Binary files a/helpfiles/DecodingExample_05.png and b/helpfiles/DecodingExample_05.png differ diff --git a/helpfiles/DecodingExample_06.png b/helpfiles/DecodingExample_06.png new file mode 100644 index 0000000..fc93118 Binary files /dev/null and b/helpfiles/DecodingExample_06.png differ diff --git a/helpfiles/DocumentationSetup2025b.html b/helpfiles/DocumentationSetup2025b.html index 71609cf..cc671b8 100644 --- a/helpfiles/DocumentationSetup2025b.html +++ b/helpfiles/DocumentationSetup2025b.html @@ -9,7 +9,7 @@ MATLAB 2025b Help Integration for nSTAT - +

Events

Events are simple object to use and are aimed to facilitate illustration of epochs in any time of data.

close all;
+      -->
+Events
+
+
+
+
+
+
+
+
+

Events

+

Events are simple object to use and are aimed to facilitate illustration of epochs in any time of data.

+
close all;
 eTimes = sort(rand(1,3)*1);
 eLabels={'E_1','E_2','E_3'};
 eventColor = 'b';
 e=Events(eTimes,eLabels,eventColor);
 e.plot;
-

The color of the event markers can also be specified

figure; e.plot([],'r'); %dont specify handle, use red; handel = gca;
+
+

The color of the event markers can also be specified

+
figure; e.plot([],'r'); %dont specify handle, use red; handel = gca;
 figure; e.plot([],'g'); %dont specify handle, use green;
-
\ No newline at end of file +--> + + diff --git a/helpfiles/EventsExamples.png b/helpfiles/EventsExamples.png index d7cd43a..536d38b 100644 Binary files a/helpfiles/EventsExamples.png and b/helpfiles/EventsExamples.png differ diff --git a/helpfiles/EventsExamples_01.png b/helpfiles/EventsExamples_01.png index 3449566..6241d5e 100644 Binary files a/helpfiles/EventsExamples_01.png and b/helpfiles/EventsExamples_01.png differ diff --git a/helpfiles/EventsExamples_02.png b/helpfiles/EventsExamples_02.png index 86480db..9312263 100644 Binary files a/helpfiles/EventsExamples_02.png and b/helpfiles/EventsExamples_02.png differ diff --git a/helpfiles/EventsExamples_03.png b/helpfiles/EventsExamples_03.png index 7e75cf6..9312263 100644 Binary files a/helpfiles/EventsExamples_03.png and b/helpfiles/EventsExamples_03.png differ diff --git a/helpfiles/EventsExamples_04.png b/helpfiles/EventsExamples_04.png new file mode 100644 index 0000000..9312263 Binary files /dev/null and b/helpfiles/EventsExamples_04.png differ diff --git a/helpfiles/Examples.html b/helpfiles/Examples.html index e291f84..abdb5e0 100644 --- a/helpfiles/Examples.html +++ b/helpfiles/Examples.html @@ -9,7 +9,7 @@ Examples - +

EXPLICIT STIMULUS EXAMPLE - WHISKER STIMULATION/THALAMIC NEURON

In the worksheet with analyze the stimulus effect and history effect on the firing of a thalamic neuron under a known stimulus consisting of whisker stimulation. Data from Demba Ba (demba@mit.edu)

Contents

Load the data

close all; currdir = pwd;
-index = strfind(currdir,'helpfiles')-1;
-rootpath = currdir(1:index);
+  
+
+
+
+

EXPLICIT STIMULUS EXAMPLE - WHISKER STIMULATION/THALAMIC NEURON

+ +

In the worksheet with analyze the stimulus effect and history effect on the firing of a thalamic neuron under a known stimulus consisting of whisker stimulation. Data from Demba Ba (demba@mit.edu)

+ +

Contents

+ +

Load the data

+
close all;
+[~,~,explicitStimulusDir] = getPaperDataDirs();
 
 Direction=3; Neuron=1; Stim=2;
-datapath = fullfile(rootpath,'data','Explicit Stimulus',strcat('Dir', num2str(Direction)),...
+datapath = fullfile(explicitStimulusDir,strcat('Dir', num2str(Direction)),...
     strcat('Neuron', num2str(Neuron)), strcat('Stim', num2str(Stim)));
 data=load(fullfile(datapath,'trngdataBis.mat'));
 
@@ -94,7 +129,10 @@
 nst2.setMaxTime(21);nst.plot;
 subplot(2,1,2);
 stim.getSigInTimeWindow(0,21).plot;
-

Fit a constant baseline and Find Stimulus Lag

We fit a constant rate (Poisson) model to the data and use the fit residual to determine the appropriate lag for the stimulus.

clear c;
+
+

Fit a constant baseline and Find Stimulus Lag

+

We fit a constant rate (Poisson) model to the data and use the fit residual to determine the appropriate lag for the stimulus.

+
clear c;
 selfHist = [] ; NeighborHist = []; sampleRate = 1000;
 c{1} = TrialConfig({{'Baseline','constant'}},sampleRate,selfHist,NeighborHist);
 c{1}.setName('Baseline');
@@ -116,8 +154,12 @@
 nspikeColl = nstColl(nst);
 cc = CovColl({stim,baseline});
 trial = Trial(nspikeColl,cc);
-
Analyzing Configuration #1: Neuron #1
-

Compare constant rate model with model including stimulus effect

Addition of the stimulus improves the fits in terms of the KS plot and the making the rescaled ISIs less correlated. The Point Process Residula also looks more "white"

clear c;
+
+
Analyzing Configuration #1: Neuron #1
+
+

Compare constant rate model with model including stimulus effect

+

Addition of the stimulus improves the fits in terms of the KS plot and the making the rescaled ISIs less correlated. The Point Process Residula also looks more "white"

+
clear c;
 selfHist = [] ; NeighborHist = []; sampleRate = 1000;
 c{1} = TrialConfig({{'Baseline','constant'}},sampleRate,selfHist,...
     NeighborHist);
@@ -128,9 +170,13 @@
 cfgColl= ConfigColl(c);
 results = Analysis.RunAnalysisForAllNeurons(trial,cfgColl,0);
 results.plotResults;
-
Analyzing Configuration #1: Neuron #1
+
+
Analyzing Configuration #1: Neuron #1
 Analyzing Configuration #2: Neuron #1
-

History Effect

Determine the best history effect model using AIC, BIC, and KS statistic

sampleRate=1000;
+
+

History Effect

+

Determine the best history effect model using AIC, BIC, and KS statistic

+
sampleRate=1000;
 delta=1/sampleRate*1;
 maxWindow=1; numWindows=30;
 windowTimes =unique(round([0 logspace(log10(delta),...
@@ -161,7 +207,8 @@
     selfHist = [];
 end
 NeighborHist = []; sampleRate = 1000;
-
Analyzing Configuration #1: Neuron #1
+
+
Analyzing Configuration #1: Neuron #1
 Analyzing Configuration #2: Neuron #1
 Analyzing Configuration #3: Neuron #1
 Analyzing Configuration #4: Neuron #1
@@ -194,7 +241,8 @@
 
      8
 
-
figure;
+
+
figure;
 x=1:length(windowTimes);
 subplot(3,1,1); plot(x,results{1}.KSStats.ks_stat,'.'); axis tight; hold on;
 plot(x(windowIndex),results{1}.KSStats.ks_stat(windowIndex),'r*');
@@ -222,7 +270,10 @@
 if(max(xticks)>=1)
     xticklabel_rotate([],90,[],'Fontsize',8);
 end
-

Compare Baseline, Baseline+Stimulus Model, Baseline+History+Stimulus

Addition of the history effect yields a model that falls within the 95% CI of the KS plot.

c{1} = TrialConfig({{'Baseline','constant'}},sampleRate,[],NeighborHist);
+
+

Compare Baseline, Baseline+Stimulus Model, Baseline+History+Stimulus

+

Addition of the history effect yields a model that falls within the 95% CI of the KS plot.

+
c{1} = TrialConfig({{'Baseline','constant'}},sampleRate,[],NeighborHist);
 c{1}.setName('Baseline');
 c{2} = TrialConfig({{'Baseline','constant'},{'Stimulus','stim'}},...
                     sampleRate,[],[]);
@@ -233,10 +284,18 @@
 cfgColl= ConfigColl(c);
 results = Analysis.RunAnalysisForAllNeurons(trial,cfgColl,0);
 results.plotResults;
-
Analyzing Configuration #1: Neuron #1
+
+
Analyzing Configuration #1: Neuron #1
 Analyzing Configuration #2: Neuron #1
 Analyzing Configuration #3: Neuron #1
-
\ No newline at end of file +--> + + diff --git a/helpfiles/ExplicitStimulusWhiskerData.m b/helpfiles/ExplicitStimulusWhiskerData.m index a091edc..2980b3a 100644 --- a/helpfiles/ExplicitStimulusWhiskerData.m +++ b/helpfiles/ExplicitStimulusWhiskerData.m @@ -4,15 +4,14 @@ % whisker stimulation. % Data from Demba Ba (demba@mit.edu) -%% Load the data -close all; currdir = pwd; -index = strfind(currdir,'helpfiles')-1; -rootpath = currdir(1:index); - -Direction=3; Neuron=1; Stim=2; -datapath = fullfile(rootpath,'data','Explicit Stimulus',strcat('Dir', num2str(Direction)),... - strcat('Neuron', num2str(Neuron)), strcat('Stim', num2str(Stim))); -data=load(fullfile(datapath,'trngdataBis.mat')); +%% Load the data +close all; +[~,~,explicitStimulusDir] = getPaperDataDirs(); + +Direction=3; Neuron=1; Stim=2; +datapath = fullfile(explicitStimulusDir,strcat('Dir', num2str(Direction)),... + strcat('Neuron', num2str(Neuron)), strcat('Stim', num2str(Stim))); +data=load(fullfile(datapath,'trngdataBis.mat')); time=0:.001:(length(data.t)-1)*.001; stimData = data.t; diff --git a/helpfiles/ExplicitStimulusWhiskerData.png b/helpfiles/ExplicitStimulusWhiskerData.png index 7857b79..8012a47 100644 Binary files a/helpfiles/ExplicitStimulusWhiskerData.png and b/helpfiles/ExplicitStimulusWhiskerData.png differ diff --git a/helpfiles/ExplicitStimulusWhiskerData_01.png b/helpfiles/ExplicitStimulusWhiskerData_01.png index d432728..c9001fd 100644 Binary files a/helpfiles/ExplicitStimulusWhiskerData_01.png and b/helpfiles/ExplicitStimulusWhiskerData_01.png differ diff --git a/helpfiles/ExplicitStimulusWhiskerData_02.png b/helpfiles/ExplicitStimulusWhiskerData_02.png index 0bf1e2c..15bc498 100644 Binary files a/helpfiles/ExplicitStimulusWhiskerData_02.png and b/helpfiles/ExplicitStimulusWhiskerData_02.png differ diff --git a/helpfiles/ExplicitStimulusWhiskerData_03.png b/helpfiles/ExplicitStimulusWhiskerData_03.png index 72f6383..ecbb5df 100644 Binary files a/helpfiles/ExplicitStimulusWhiskerData_03.png and b/helpfiles/ExplicitStimulusWhiskerData_03.png differ diff --git a/helpfiles/ExplicitStimulusWhiskerData_04.png b/helpfiles/ExplicitStimulusWhiskerData_04.png index 12c58f6..64b806e 100644 Binary files a/helpfiles/ExplicitStimulusWhiskerData_04.png and b/helpfiles/ExplicitStimulusWhiskerData_04.png differ diff --git a/helpfiles/ExplicitStimulusWhiskerData_05.png b/helpfiles/ExplicitStimulusWhiskerData_05.png index 2f81749..e6048db 100644 Binary files a/helpfiles/ExplicitStimulusWhiskerData_05.png and b/helpfiles/ExplicitStimulusWhiskerData_05.png differ diff --git a/helpfiles/ExplicitStimulusWhiskerData_06.png b/helpfiles/ExplicitStimulusWhiskerData_06.png index 898d5a7..9ee1252 100644 Binary files a/helpfiles/ExplicitStimulusWhiskerData_06.png and b/helpfiles/ExplicitStimulusWhiskerData_06.png differ diff --git a/helpfiles/ExplicitStimulusWhiskerData_07.png b/helpfiles/ExplicitStimulusWhiskerData_07.png index 17870db..167cbf4 100644 Binary files a/helpfiles/ExplicitStimulusWhiskerData_07.png and b/helpfiles/ExplicitStimulusWhiskerData_07.png differ diff --git a/helpfiles/ExplicitStimulusWhiskerData_08.png b/helpfiles/ExplicitStimulusWhiskerData_08.png index 48ab58b..e8be9b1 100644 Binary files a/helpfiles/ExplicitStimulusWhiskerData_08.png and b/helpfiles/ExplicitStimulusWhiskerData_08.png differ diff --git a/helpfiles/ExplicitStimulusWhiskerData_09.png b/helpfiles/ExplicitStimulusWhiskerData_09.png new file mode 100644 index 0000000..66347d6 Binary files /dev/null and b/helpfiles/ExplicitStimulusWhiskerData_09.png differ diff --git a/helpfiles/FitResSummaryExamples.html b/helpfiles/FitResSummaryExamples.html index f604fc7..fbef614 100644 --- a/helpfiles/FitResSummaryExamples.html +++ b/helpfiles/FitResSummaryExamples.html @@ -1,71 +1,93 @@ + + + + + +FitResSummary Examples + + + + +

FitResSummary Examples

\ No newline at end of file +--> + + diff --git a/helpfiles/FitResult.html b/helpfiles/FitResult.html index 7810a96..dfc2f13 100644 --- a/helpfiles/FitResult.html +++ b/helpfiles/FitResult.html @@ -1,75 +1,112 @@ - - - - - FitResult
classdef FitResult < handle
-    % FITRESULT
-    % stores results of a fit using the Analysis object
-    % The results are for a single neuron over a range of configurations
-    % <a href="FitResultExamples.html">FitResult Examples</a>
-    % see also <a href="Analysis.html">Analysis</a>
-    % Reference page in Help browser
-    % <a href="FitResult.html">FitResult Reference</a>
+      -->
+FitResult
+
+
+
+
+
+
+
+
+
+classdef FitResult < handle
+% FITRESULT
+% stores results of a fit using the Analysis object
+% The results are for a single neuron over a range of configurations
+%
+% <a href="matlab:nstatOpenHelpPage('FitResultExamples.html')">FitResult Examples</a>
+%
+% see also <a href="matlab:nstatOpenHelpPage('Analysis.html')">Analysis</a>
+%
+% Reference page in Help browser
+% <a href="matlab:nstatOpenHelpPage('FitResult.html')">FitResult Reference</a>
+
+
+%
+% nSTAT v1 Copyright (C) 2012 Masschusetts Institute of Technology
+% Cajigas, I, Malik, WQ, Brown, EN
+% This program is free software; you can redistribute it and/or
+% modify it under the terms of the GNU General Public License as published
+% by the Free Software Foundation; either version 2 of the License, or
+% (at your option) any later version.
+%
+% This program is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+% See the GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with this program; if not, write to the Free Software Foundation,
+% Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
     properties
        numResults   %Number of results in this FitResult object
        lambda       %Lambda signal
@@ -77,9 +114,11 @@
        fitType      %Poisson or Binomial
 
        b            %coefficients for each fit
+
        dev          %deviance for each fit
        AIC          %Akaike's Information Criterion for each fit
        BIC          %Baysian Information Criterion for each fit
+       logLL        %Log Likelihood
        stats        %Relevant statistics for each fit
        configs      % the config collection for the different fits
        configNames  % names of the the differen fits
@@ -115,7 +154,7 @@
 
     methods
 
-        function fitObj=FitResult(spikeObj,covLabels,numHist,histObjects,ensHistObj,lambda,b, dev, stats,AIC,BIC,configColl,XvalData,XvalTime,distribution)
+        function fitObj=FitResult(spikeObj,covLabels,numHist,histObjects,ensHistObj,lambda,b, dev, stats,AIC,BIC,logLL, configColl,XvalData,XvalTime,distribution)
             % fitObj=FitResult(spikeObj,covLabels,numHist,histObjects,ensHistObj,lambda,b, dev, stats,AIC,BIC,configColl,XvalData,XvalTime)
             % Stores the results of multiple regressions for a single  neuron into a accessible structure.
             %
@@ -147,15 +186,42 @@
                 XvalData =[];
             end
 
-            if(isnumeric(spikeObj.name))
-                nNumber =spikeObj.name;
-            else
-                nNumber = str2double(spikeObj.name(~isletter(spikeObj.name)));
+            if(isa(spikeObj,'cell'))
+                for i=1:length(spikeObj)
+                    if(isnumeric(spikeObj{i}.name))
+                        nNumber(i) =spikeObj{i}.name;
+                    else
+                        nNumber(i) = str2double(spikeObj{i}.name(~isletter(spikeObj{i}.name)));
+                    end
+                    minTime(i)=spikeObj{i}.minTime;
+                    maxTime(i)=spikeObj{i}.maxTime;
+                end
+                nNumber = unique(nNumber);
+                minTime = unique(minTime);
+                maxTime = unique(maxTime);
+                if(length(nNumber)>1)
+                    error('Can only have a FitResults with spike trains from a single neuron');
+                end
+                if(length(minTime)>1 || length(maxTime)>1)
+                    error('Spike Trains are of different lengths');
+                end
+
+            elseif(isa(spikeObj,'nspikeTrain'))
+                if(isnumeric(spikeObj.name))
+                    nNumber =spikeObj.name;
+                else
+                    nNumber = str2double(spikeObj.name(~isletter(spikeObj.name)));
+                end
+                minTime=spikeObj.minTime;
+                maxTime=spikeObj.maxTime;
+
             end
+
             fitObj.neuronNumber = nNumber; %str2num(spikeObj.name);
             fitObj.neuralSpikeTrain = spikeObj;
-            fitObj.minTime = spikeObj.minTime;
-            fitObj.maxTime = spikeObj.maxTime;
+            fitObj.minTime = minTime;
+            fitObj.maxTime = maxTime;
+
             fitObj.numResults = 0;
             fitObj.configs = configColl;
             fitObj.configNames = configColl.getConfigNames;
@@ -166,7 +232,7 @@
             fitObj.numHist=numHist;
             fitObj.histObjects = histObjects;
             fitObj.ensHistObjects = ensHistObj;
-            fitObj.addParamsToFit(fitObj.neuronNumber,lambda,b, dev, stats,AIC,BIC,configColl);
+            fitObj.addParamsToFit(fitObj.neuronNumber,lambda,b, dev, stats,AIC,BIC,logLL,configColl);
             fitObj.Z        =[]; %rescaled spikes times - exponentially dist.
             fitObj.U        =[]; %rescaled spike times - uniformly dist.
             fitObj.X        =[]; %rescaled spike times - gaussian dist.
@@ -183,8 +249,19 @@
             fitObj.fitType = distribution;
 
         end
+        function fitObj = setNeuronName(fitObj,name)
+           fitObj.neuronNumber = name;
+        end
         function mFitRes = mergeResults(fitObj,newFitObj)
+            % mFitRes = mergeResults(fitObj,newFitObj)
+            % mFitRes contains the results from fitObj followed by the
+            % results of newFitObj in a single new FitResult object
+            %
+            % newFitObj can be of class 'FitResult' or a cell array of
+            % 'FitResult' objects. In the latter case, the results are
+            % apppended in the order that they appear in each cell array.
             if(isa(newFitObj,'FitResult'))
+%                 newFitObj.neuronNumber
                 if(fitObj.neuronNumber ==newFitObj.neuronNumber)
                     spikeObj = fitObj.neuralSpikeTrain;
                     covLabels = fitObj.covLabels(1:fitObj.numResults);
@@ -200,6 +277,7 @@
                     dev = [fitObj.dev newFitObj.dev];
                     AIC = [fitObj.AIC newFitObj.AIC];
                     BIC = [fitObj.BIC newFitObj.BIC];
+                    logLL = [fitObj.logLL newFitObj.logLL];
                     stats=fitObj.stats(1:fitObj.numResults);
                     stats((fitObj.numResults+1):(fitObj.numResults+newFitObj.numResults)) = newFitObj.stats(1:newFitObj.numResults);
                     lambda = fitObj.lambda.merge(newFitObj.lambda);
@@ -217,15 +295,38 @@
                     XvalTime = [fitObj.XvalTime newFitObj.XvalTime];
                     distribution=fitObj.fitType(1:fitObj.numResults);
                     distribution((fitObj.numResults+1):(fitObj.numResults+newFitObj.numResults)) = newFitObj.fitType(1:newFitObj.numResults);
-                    Z=[fitObj.Z newFitObj.Z];
-                    U=[fitObj.U newFitObj.U];
+                    tempZ = zeros(length(fitObj.Z),size(newFitObj.Z,2));
+                    tempU = zeros(length(fitObj.U),size(newFitObj.U,2));
+                    tempZ(1:length(newFitObj.Z),:) = newFitObj.Z;
+                    tempU(1:length(newFitObj.U),:) = newFitObj.U;
+                    Z=[fitObj.Z tempZ];
+                    U=[fitObj.U tempU];
                     [X,rhoSig,confBoundSig] = Analysis.computeInvGausTrans(Z);
 
                     M=fitObj.Residual.merge(newFitObj.Residual);
-                    xAxis    = [fitObj.KSStats.xAxis newFitObj.KSStats.xAxis];
-                    KSSorted = [fitObj.KSStats.KSSorted newFitObj.KSStats.KSSorted];
+
+                    origLength = size(fitObj.KSStats.xAxis,1);
+                    currLength = size(newFitObj.KSStats.xAxis,1);
+
+                    if(currLength~=origLength)
+                        %we use this because some times the time scales
+                        %dont match up.  In particular when spikeTrain is
+                        %segmented by steps or windows and the window sizes
+                        %are normalized to 1.
+                      newX = fitObj.KSStats.xAxis;
+                      oldX = newFitObj.KSStats.xAxis;
+                      oldY = newFitObj.KSStats.KSSorted;
+                      y = interp1(oldX,oldY,newX(:,1),'spline','extrap');
+                      xAxis    = [fitObj.KSStats.xAxis newX(:,1)];
+                      KSSorted = [fitObj.KSStats.KSSorted y];
+                    else
+
+                        xAxis    = [fitObj.KSStats.xAxis newFitObj.KSStats.xAxis];
+                        KSSorted = [fitObj.KSStats.KSSorted newFitObj.KSStats.KSSorted];
+
+                    end
                     ks_stat  = [fitObj.KSStats.ks_stat newFitObj.KSStats.ks_stat];
-                    mFitRes=FitResult(spikeObj,covLabels,numHist,histObjects,ensHistObjects,lambda,b, dev, stats,AIC,BIC,configColl,XvalData,XvalTime,distribution);
+                    mFitRes=FitResult(spikeObj,covLabels,numHist,histObjects,ensHistObjects,lambda,b, dev, stats,AIC,BIC,logLL,configColl,XvalData,XvalTime,distribution);
                     mFitRes.setKSStats(Z,U, xAxis, KSSorted, ks_stat);
                     mFitRes.setInvGausStats(X,rhoSig,confBoundSig);
                     mFitRes.setFitResidual(M);
@@ -245,7 +346,44 @@
             end
 
         end
-        function addParamsToFit(fitObj,neuronNum,lambda,b, dev, stats,AIC,BIC,configColl)
+        function subsetFit = getSubsetFitResult(fitObj,subfits)
+            %subfits is a row vector with numbers corresponding to the
+            %subfits
+            if(and(min(subfits)>0,max(subfits)<=fitObj.numResults))
+                spikeObj = fitObj.neuralSpikeTrain;
+                covLabels = fitObj.covLabels(subfits);
+                numHist = fitObj.numHist(subfits);
+                histObjects = fitObj.histObjects(subfits);
+                ensHistObj = fitObj.ensHistObjects(subfits);
+                lambda = fitObj.lambda.getSubSignal(subfits);
+                b = fitObj.b(subfits);
+                dev = fitObj.dev(subfits);
+                stats = fitObj.stats(subfits);
+                AIC = fitObj.AIC(subfits);
+                BIC = fitObj.BIC(subfits);
+                logLL = fitObj.logLL(subfits);
+                configColl = fitObj.configs.getSubsetConfigs(subfits);
+                XvalData  = fitObj.XvalData;
+                XvalTime  = fitObj.XvalTime;
+                distribution = fitObj.fitType;
+
+                subsetFit=FitResult(spikeObj,covLabels,numHist,histObjects,ensHistObj,lambda,b, dev, stats,AIC,BIC,logLL,configColl,XvalData,XvalTime,distribution);
+                Z = fitObj.Z(:,subfits);
+                U = fitObj.U(:,subfits);
+                X = fitObj.X(:,subfits);
+                xAxis= fitObj.KSStats.xAxis(:,subfits);
+                KSSorted=fitObj.KSStats.KSSorted(:,subfits);
+                ks_stat=fitObj.KSStats.ks_stat(subfits);
+                rhoSig=fitObj.invGausStats.rhoSig.getSubSignal(subfits);
+                confBoundSig=fitObj.invGausStats.confBoundSig;
+                M = fitObj.Residual.getSubSignal(subfits);
+
+                subsetFit.setKSStats(Z,U, xAxis, KSSorted, ks_stat);
+                subsetFit.setInvGausStats(X,rhoSig,confBoundSig);
+                subsetFit.setFitResidual(M);
+            end
+        end
+        function addParamsToFit(fitObj,neuronNum,lambda,b, dev, stats,AIC,BIC,logLL,configColl)
             % addParamsToFit(fitObj,neuronNum,lambda,b, dev, stats,AIC,BIC,configColl)
             % Add the specified parameters to the current FitResult object
             % only if the neuronNum matches the neuronNum of this object
@@ -266,28 +404,38 @@
 
               if(numNewResults==1)
                       fitObj.b{fitObj.numResults+1}    = b{1};
+
                       fitObj.dev(fitObj.numResults+1)  = dev;
                       fitObj.stats{fitObj.numResults+1}= stats{1};
                       if(nargin<7)
                           fitObj.AIC(fitObj.numResults+1)  = 2*length(b)+dev;
                           fitObj.BIC(fitObj.numResults+1)  = length(b)*log(length(newLambda.time))+dev;
+                          delta = 1/newLambda.sampleRate;
+                          fitObj.logLL(fitObj.numResults+1) = sum(y.*log(data*delta)+(1-y).*(1-newLambda.data*delta));
                       else
                           fitObj.AIC(fitObj.numResults+1)  = AIC;
                           fitObj.BIC(fitObj.numResults+1)  = BIC;
+                          fitObj.logLL(fitObj.numResults+1) = logLL;
                       end
 
                       fitObj.numCoeffs(fitObj.numResults+1) = length(b);
               else
                   for i=1:numNewResults
                       fitObj.b{fitObj.numResults+i}    = b{i};
+
                       fitObj.dev(fitObj.numResults+i)  = dev(i);
                       fitObj.stats{fitObj.numResults+i}= stats{i};
                       if(nargin<7)
                           fitObj.AIC(fitObj.numResults+i)  = 2*length(b{i})+dev(i);
                           fitObj.BIC(fitObj.numResults+i)  = length(b{i})*log(length(newLambda.time))+dev(i);
+                          delta=fitObj.neuralSpikeTrain.sampleRate;
+                          y=fitObj.neuralSpikeTrain.getSigRep.dataToMatrix;
+
+                          fitObj.logLL(fitObj.numResults+i)= sum(y.*log(newLambda.data*delta)+(1-y).*(1-newLambda.data*delta));
                       else
                           fitObj.AIC(fitObj.numResults+i)  = AIC(i);
                           fitObj.BIC(fitObj.numResults+i)  = BIC(i);
+                          fitObj.logLL(fitObj.numResults+i)= logLL(i);
                       end
                       fitObj.numCoeffs(fitObj.numResults+i) = length(b{i});
                   end
@@ -301,7 +449,7 @@
               fitObj.numResults = fitObj.numResults+numNewResults;
               dataLabels = cell(1,fitObj.numResults);
               for i=1:fitObj.numResults
-                 dataLabels{i} = strcat('\lambda_',num2str(i));
+                 dataLabels{i} = strcat('\lambda_{',num2str(i),'}');
               end
               fitObj.lambda.setDataLabels(dataLabels);
 
@@ -311,7 +459,7 @@
               error('Neuron number does not match');
           end
         end
-        function lambda = computeValLambda(fitObj)
+        function [lambda, logLL] = computeValLambda(fitObj)
             % lambda = computeValLambda(fitObj)
             % Returns a Covariate object lambda. This is the Conditional
             % intensity function evaluated using the validation data
@@ -320,17 +468,31 @@
                 lambdaData(:,i) = fitObj.evalLambda(i,fitObj.XvalData{i});
             end
             lambda=Covariate(fitObj.XvalTime{1},lambdaData,...
-                  '\Lambda(t)',fitObj.lambda.xlabelval,...
+                  '\lambda(t)',fitObj.lambda.xlabelval,...
                   fitObj.lambda.xunits,'Hz',fitObj.lambda.dataLabels);
+            delta = 1/lambda.sampleRate;
+            y=fitObj.neuralSpikeTrain.getSigRep.dataToMatrix;
+            logLL =sum(y.*log(lambda.data*delta)+(1-y).*(1-lambda.data*delta));
         end
 
         function mapCovLabelsToUniqueLabels(fitObj)
+            % mapCovLabelsToUniqueLabels(fitObj)
+            % Used internally by the FitResult class generate a matrix that
+            % maps how covariate labels of the fit object map to unique
+            % covariate labels. For example, multiple fits that have a
+            % constant baseline term will be assumed to refer to the same
+            % "baseline" term and not two separate ones
             flatMask = zeros(length(fitObj.uniqueCovLabels),length(fitObj.covLabels));
             for j=1:length(fitObj.covLabels)
                 currLabels = fitObj.covLabels{j};
                 index=zeros(1,length(currLabels));
                 for i=1:length(currLabels)
-                    index(i)=strmatch(currLabels{i}, fitObj.uniqueCovLabels, 'exact');
+                    idx = find(strcmp(currLabels{i}, fitObj.uniqueCovLabels),1,'first');
+                    if(isempty(idx))
+                        error('FitResult:UnknownCovariateLabel',...
+                            'Unable to map covariate label "%s" to unique labels.',currLabels{i});
+                    end
+                    index(i)=idx;
                 end
 
                 fitObj.indicesToUniqueLabels{j} = index;
@@ -429,7 +591,9 @@
 %                             if(i==1)
 %                                 runSum = b(1)*baseline;
 %                             else
-                                runSum = runSum+b(i)*newData{i-1};
+                                if(i<=length(b))
+                                    runSum = runSum+b(i)*newData{i};
+                                end
 %                             end
                         end
                         if(strcmp(fitObj.fitType{lambdaIndex},'poisson'))
@@ -533,7 +697,402 @@
            tempVal =sum(fitObj.flatMask,2);
            fitObj.plotParams.numResultsCoeffPresent =tempVal(index);
         end
-        function h=plotCoeffs(fitObj,handle,fitNum,plotProps,plotSignificance)
+
+        function [coeffIndex, epochId,numEpochs] = getCoeffIndex(fitObj,fitNum,sortByEpoch)
+          if(nargin<3 || isempty(sortByEpoch))
+             sortByEpoch=0;
+          end
+          if(nargin<2 || isempty(fitNum))
+              fitNum = 1:fitObj.numResults;
+          end
+          if(isempty(fitObj.plotParams))
+               fitObj.computePlotParams;
+          end
+          [histIndex, epochId] = fitObj.getHistIndex(fitNum,sortByEpoch);
+          allIndex = 1:length(fitObj.uniqueCovLabels);
+
+          nonHistIndex = setdiff(allIndex,histIndex);
+%           nonNANIndex = find(sum(~isnan(fitObj.plotParams.bAct(:,fitNum)),2)>=1);
+          nonNANIndex=  allIndex;
+          actCoeffIndex = nonHistIndex(ismember(nonHistIndex, nonNANIndex));
+          allCoeffTerms = fitObj.uniqueCovLabels(actCoeffIndex);
+%           coeffName = cell(size(allCoeffTerms));
+          epochStartInd=regexp(allCoeffTerms,'_\{\d*\}','start');
+          epochEndInd=regexp(allCoeffTerms,'_\{\d*\}','end');
+
+          allCoeffIndex = [];
+          nonEpochIndex=[];
+%           nonEmptyCoeffNameInd = [];
+          epochsExist =0;
+          for i=1:length(allCoeffTerms)
+              if(~isempty(allCoeffTerms{i}))
+                allCoeffIndex = [allCoeffIndex i];
+
+                 if(~isempty(epochStartInd{i}))
+%                      nonEmptyCoeffNameInd = [nonEmptyCoeffNameInd i];
+                     epochsExist=1;
+                     actStart = epochStartInd{i}+2;
+                     actEnd   = epochEndInd{i}-1;
+                     numEpoch(i) = str2num(allCoeffTerms{i}(actStart:actEnd));
+%                      coeffName{i} =  allCoeffTerms{i}(1:actStart-3);
+
+
+                 else
+                    nonEpochIndex = [nonEpochIndex i];
+                    numEpoch(i) = 0; % make terms that only appear once part of epoch 0.
+
+                 end
+              end
+
+          end
+
+%           coeffName = coeffName(nonEmptyCoeffNameInd);
+          if(epochsExist && ~sortByEpoch)
+              totalEpochs = unique(numEpoch);
+              coeffIndex = nonEpochIndex;
+              if(nargout>1)
+                  epochId=zeros(size(nonEpochIndex));
+              end
+              for i=1:length(totalEpochs)
+                  if(totalEpochs(i)~=0)
+                    coeffIndex = [coeffIndex, find(numEpoch==totalEpochs(i))];
+
+                     if(nargout>1)
+                        epochId = [epochId, totalEpochs(i)*ones(size(find(numEpoch==totalEpochs(i))))];
+                     end
+                  end
+              end
+              coeffIndex = actCoeffIndex(coeffIndex);
+          elseif(epochsExist && sortByEpoch)
+              coeffIndex = actCoeffIndex(allCoeffIndex);
+              if(nargout>1)
+                epochId = numEpoch;
+              end
+          else
+              coeffIndex = actCoeffIndex(allCoeffIndex);
+              if(nargout>1)
+                epochId = zeros(size(allCoeffIndex)); %no epochs exist so just create same index for all;
+              end
+          end
+
+
+%           nonNANIndex = find(sum(~isnan(fitObj.plotParams.bAct(:,fitNum)),2)>=1);
+          nonNANIndex = allIndex;
+          coeffIndex = coeffIndex(ismember(coeffIndex, nonNANIndex));
+
+          if(nargout>2)
+              numEpochs = length(unique(epochId));
+          end
+
+        end
+
+        function h=plotCoeffsWithoutHistory(fitObj,fitNum,sortByEpoch,plotSignificance)
+           if(nargin<4 || isempty(plotSignificance))
+               plotSignificance=1;
+           end
+           if(nargin<3 || isempty(sortByEpoch))
+              sortByEpoch = 0;
+           end
+           if(nargin<2 || isempty(fitNum))
+               fitNum = 1:fitObj.numResults;
+           end
+           if(isempty(fitObj.plotParams))
+               fitObj.computePlotParams;
+           end
+
+
+          coeffIndex = fitObj.getCoeffIndex(fitNum,sortByEpoch);
+          h=fitObj.plotCoeffs([],fitNum,[],plotSignificance,coeffIndex);
+
+
+
+        end
+
+        function [histIndex, epochId,numEpochs] = getHistIndex(fitObj,fitNum,sortByEpoch)
+            %if sortByEpoch==1 then we group all regression terms with the
+            %same name one next to each other by epoch (time interval).
+            %Otherwise, we show all epoch one terms, followed by all epoch
+            %2 terms, etc.
+           if(nargin<3 || isempty(sortByEpoch))
+            sortByEpoch = 0;
+           end
+          if(nargin<2 || isempty(fitNum))
+              fitNum = 1:fitObj.numResults;
+          end
+          if(isempty(fitObj.plotParams))
+               fitObj.computePlotParams;
+          end
+
+
+          allHistTerms = regexp(fitObj.uniqueCovLabels,'^[\w*');
+          epochStartInd=regexp(fitObj.uniqueCovLabels,'\]_\{\d*\}','start');
+          epochEndInd=regexp(fitObj.uniqueCovLabels,'\]_\{\d*\}','end');
+          allHistIndex = [];
+          epochsExist =0;
+          for i=1:length(allHistTerms)
+              if(~isempty(allHistTerms{i}))
+                allHistIndex = [allHistIndex i];
+                 if(~isempty(epochStartInd{i}))
+                     epochsExist=1;
+                     actStart = epochStartInd{i}+3;
+                     actEnd   = epochEndInd{i}-1;
+                     numEpoch(i) = str2num(fitObj.uniqueCovLabels{i}(actStart:actEnd));
+                 end
+              end
+
+          end
+
+          if(epochsExist && ~sortByEpoch)
+              totalEpochs = unique(numEpoch);
+              histIndex = [];
+              if(nargout>1)
+                  epochId=[];
+              end
+              for i=1:length(totalEpochs)
+                 histIndex = [histIndex, find(numEpoch==totalEpochs(i))];
+                 if(nargout>1)
+                    epochId = [epochId, totalEpochs(i)*ones(size(find(numEpoch==totalEpochs(i))))];
+                 end
+              end
+          elseif(epochsExist && sortByEpoch)
+              histIndex = allHistIndex;
+              if(nargout>1)
+                epochId = numEpoch;
+              end
+          else
+              histIndex = allHistIndex;
+              if(nargout>1)
+                epochId = zeros(size(allHistIndex)); %no epochs exist so just create same index for all;
+              end
+          end
+
+
+%           nonNANIndex = find(sum(~isnan(fitObj.plotParams.bAct(:,fitNum)),2)>=1);
+%           histIndex = histIndex(ismember(histIndex, nonNANIndex));
+
+          if(nargout>2)
+              numEpochs = length(unique(epochId));
+          end
+
+        end
+
+        function [coeffMat, labels, SEMat] = getCoeffs(fitObj, fitNum)
+             if(nargin<2 || isempty(fitNum))
+                fitNum =1:fitObj.numResults;
+            end
+            sortByEpoch = 0; % Make sure we have different time series if the history is divided into epochs;
+            [coeffIndex, epochId, numEpochs] = fitObj.getCoeffIndex(fitNum,sortByEpoch);
+            epochNums = unique(epochId);
+
+
+             coeffStrings = fitObj.uniqueCovLabels(coeffIndex);
+            baseStringEndIndex =regexp(coeffStrings,'_\{\d*\}','start');
+
+            for i=1:length(baseStringEndIndex)
+                if(~isempty(baseStringEndIndex{i}))
+                    baseStrings{i} = coeffStrings{i}(1:baseStringEndIndex{i}-1);
+                else
+                    baseStrings{i} = coeffStrings{i};
+                end
+            end
+            uniqueCoeffs = unique(baseStrings);
+
+            for i=1:length(uniqueCoeffs)
+               coeffStrIndex{i} = coeffIndex(strcmp(baseStrings,uniqueCoeffs{i}));
+               if(min(epochId)==0)
+                epochIndices{i} = epochId(strcmp(baseStrings,uniqueCoeffs{i}))+1;
+               else
+                epochIndices{i} = epochId(strcmp(baseStrings,uniqueCoeffs{i}));
+               end
+            end
+
+%
+%             for i=1:numEpochs
+%                 epochIndices{i} = find(epochId==epochNums(i));
+%                 epochLength(i) = length(epochIndices{i});
+%             end
+
+
+
+            coeffIndMat= nan(length(uniqueCoeffs),numEpochs);
+
+            labels = cell(size(coeffIndMat));
+            for i=1:length(uniqueCoeffs)
+               coeffIndMat(i,epochIndices{i}) = coeffStrIndex{i};
+               labels(i,epochIndices{i}) = fitObj.uniqueCovLabels(coeffStrIndex{i});
+            end
+
+
+%             for i=1:numEpochs
+%                coeffIndMat(1:epochLength(i),i) = coeffIndex(epochIndices{i});
+%                labels(1:epochLength(i),i) = fitObj.uniqueCovLabels(coeffIndMat(1:epochLength(i),i));
+%             end
+
+
+
+            if(length(fitNum)>1)
+                coeffMat = nan(size(coeffIndMat,1),size(coeffIndMat,2), length(fitNum));
+                SEMat    = nan(size(coeffIndMat,1),size(coeffIndMat,2), length(fitNum));
+                for i=1:length(fitNum)
+                    for j=1:length(uniqueCoeffs)
+                        bTemp=fitObj.plotParams.bAct(coeffStrIndex{j},i);
+                        seTemp=fitObj.plotParams.seAct(coeffStrIndex{j},i);
+                        coeffMat(j,epochIndices{j},i) = bTemp';
+                        SEMat(j,epochIndices{j},i) = seTemp';
+                    end
+                end
+            else
+                coeffMat = nan(size(coeffIndMat,1),size(coeffIndMat,2));
+                SEMat    = nan(size(coeffIndMat,1),size(coeffIndMat,2));
+                for j=1:length(uniqueCoeffs)
+                    bTemp=fitObj.plotParams.bAct(coeffStrIndex{j},fitNum);
+                    seTemp = fitObj.plotParams.seAct(coeffStrIndex{j},fitNum);
+                    coeffMat(j,epochIndices{j}) = bTemp';
+                    SEMat(j,epochIndices{j}) = seTemp';
+                end
+
+            end
+
+        end
+
+        function [histMat, labels, SEMat] = getHistCoeffs(fitObj,fitNum)
+              if(nargin<2 || isempty(fitNum))
+                fitNum =1:fitObj.numResults;
+            end
+            sortByEpoch = 0; % Make sure we have different time series if the history is divided into epochs;
+            [histIndex, epochId, numEpochs] = fitObj.getHistIndex(fitNum,sortByEpoch);
+            epochNums = unique(epochId);
+
+
+            histcoeffStrings = fitObj.uniqueCovLabels(histIndex);
+            baseStringEndIndex =regexp(histcoeffStrings,'_\{\d*\}','start');
+            baseStrings = cell(length(baseStringEndIndex),1);
+            for i=1:length(baseStringEndIndex)
+                if(~isempty(baseStringEndIndex{i}))
+                    baseStrings{i} = histcoeffStrings{i}(1:baseStringEndIndex{i}-1);
+                else
+                    baseStrings{i} = histcoeffStrings{i};
+                end
+            end
+            uniqueCoeffs = unique(baseStrings);
+
+            for i=1:length(uniqueCoeffs)
+               histcoeffStrIndex{i} = histIndex(strcmp(baseStrings,uniqueCoeffs{i}));
+               if(min(epochId)==0)
+                epochIndices{i} = epochId(strcmp(baseStrings,uniqueCoeffs{i}))+1;
+               else
+                epochIndices{i} = epochId(strcmp(baseStrings,uniqueCoeffs{i}));
+               end
+            end
+
+%
+%             for i=1:numEpochs
+%                 epochIndices{i} = find(epochId==epochNums(i));
+%                 epochLength(i) = length(epochIndices{i});
+%             end
+
+
+
+            histcoeffIndMat= nan(length(uniqueCoeffs),numEpochs);
+            labels = cell(size(histcoeffIndMat));
+%             SEMat = nan(length(uniqueCoeffs),numEpochs);
+            for i=1:length(uniqueCoeffs)
+               histcoeffIndMat(i,epochIndices{i}) = histcoeffStrIndex{i};
+               labels(i,epochIndices{i}) = fitObj.uniqueCovLabels(histcoeffStrIndex{i});
+%                SEMat(i,epochIndices{i}) = fitObj.se;
+            end
+
+
+%             for i=1:numEpochs
+%                coeffIndMat(1:epochLength(i),i) = coeffIndex(epochIndices{i});
+%                labels(1:epochLength(i),i) = fitObj.uniqueCovLabels(coeffIndMat(1:epochLength(i),i));
+%             end
+
+
+
+            if(length(fitNum)>1)
+                histMat = nan(size(histcoeffIndMat,1),size(histcoeffIndMat,2), length(fitNum));
+                SEMat   = nan(size(histcoeffIndMat,1),size(histcoeffIndMat,2), length(fitNum));
+                for i=fitNum
+                    for j=1:length(uniqueCoeffs)
+                        bTemp=fitObj.plotParams.bAct(histcoeffStrIndex{j},i);
+                        seTemp = fitObj.plotParams.seAct(histcoeffStrIndex{j},i);
+                        histMat(j,epochIndices{j},i) = bTemp';
+                        SEMat(j,epochIndices{j},i) = seTemp';
+                    end
+                end
+            else
+                histMat = nan(size(histcoeffIndMat,1),size(histcoeffIndMat,2));
+                SEMat   = nan(size(histcoeffIndMat,1),size(histcoeffIndMat,2));
+                for j=1:length(uniqueCoeffs)
+                    bTemp=fitObj.plotParams.bAct(histcoeffStrIndex{j},fitNum);
+                    seTemp=fitObj.plotParams.seAct(histcoeffStrIndex{j},fitNum);
+                    histMat(j,epochIndices{j}) = bTemp';
+                    SEMat(j,epochIndices{j}) = seTemp';
+                end
+
+            end
+
+
+%
+%             if(nargin<2 || isempty(fitNum))
+%                 fitNum =1:fitObj.numResults;
+%             end
+%             sortByEpoch = 0; % Make sure we have different time series if the history is divided into epochs;
+%             [histIndex, epochId, numEpochs] = fitObj.getHistIndex(fitNum,sortByEpoch);
+%             epochNums = unique(epochId);
+%             for i=1:numEpochs
+%                 epochIndices{i} = find(epochId==epochNums(i));
+%                 epochLength(i) = length(epochIndices{i});
+%             end
+%
+%             histIndMat= nan(max(epochLength),numEpochs);
+%             labels = cell(size(histIndMat));
+%             for i=1:numEpochs
+%                histIndMat(1:epochLength(i),i) = histIndex(epochIndices{i});
+%                labels(1:epochLength(i),i) = fitObj.uniqueCovLabels(histIndMat(1:epochLength(i),i));
+%             end
+%
+%
+%
+%             if(length(fitNum)>1)
+%                 histMat = nan(size(histIndMat,1),size(histIndMat,2), length(fitNum));
+%                 for i=1:length(fitNum)
+%                     for j=1:numEpochs
+%                         bTemp=fitObj.plotParams.bAct(epochIndices{j},i);
+%                         histMat(1:epochLength(j),j,i) = bTemp;
+%                     end
+%                 end
+%             else
+%                 histMat = nan(size(histIndMat,1),size(histIndMat,2));
+%
+%                 for j=1:numEpochs
+%                     bTemp=fitObj.plotParams.bAct(epochIndices{j},fitNum);
+%                     histMat(1:epochLength(j),j) = bTemp;
+%                 end
+%
+%             end
+
+        end
+        function h=plotHistCoeffs(fitObj,fitNum,sortByEpoch,plotSignificance)
+          if(nargin<4 || isempty(plotSignificance))
+            plotSignificance=1;
+          end
+          if(nargin<3 || isempty(sortByEpoch))
+             sortByEpoch=0;
+          end
+          if(nargin<2 || isempty(fitNum))
+              fitNum = 1:fitObj.numResults;
+          end
+          if(isempty(fitObj.plotParams))
+               fitObj.computePlotParams;
+          end
+          histIndex = fitObj.getHistIndex(fitNum,sortByEpoch);
+          h=fitObj.plotCoeffs([],fitNum,[],plotSignificance,histIndex);
+        end
+
+
+        function h=plotCoeffs(fitObj,handle,fitNum,plotProps,plotSignificance,subIndex)
            % h=plotCoeffs(fitObj,handle,fitNum,plotProps,plotSignificance)
            % plots the GLM coefficients for each fit along with the
            % confidence intervals.
@@ -544,7 +1103,8 @@
            %                   parameters that are statistically different
            %                   from zero with alpha=5%.
 
-           if(nargin<5)
+
+           if(nargin<5 || isempty(plotSignificance))
                plotSignificance = 1;
            end
 
@@ -561,21 +1121,43 @@
            end
 
            if(isempty(fitObj.plotParams))
-               fitObj.computePlotParams(fitNum);
+               fitObj.computePlotParams;
+           end
+
+           if(nargin<6 || isempty(subIndex))
+              subIndex = [fitObj.getHistIndex, fitObj.getCoeffIndex];
            end
 
-           bAct = fitObj.getPlotParams.bAct;
-           seAct= fitObj.getPlotParams.seAct;
-           sigIndex=fitObj.getPlotParams.sigIndex;
+           bAct = fitObj.getPlotParams.bAct(subIndex,fitNum);
+           seAct= fitObj.getPlotParams.seAct(subIndex,fitNum);
+           sigIndex=fitObj.getPlotParams.sigIndex(subIndex,fitNum);
 
            if(~isempty(plotProps))
                for i=1:length(fitNum)
-                    h=errorbar(handle,1:length(index),bAct,seAct,plotProps{i}); hold on;
+                    h(i)=errorbar(handle,1:length(subIndex),bAct(:,i),seAct(:,i),plotProps{i}); hold on;
+                    set(h(i), 'LineStyle', 'none', 'Marker', '.');%,...
+%                         'Linewidth',1,'Marker','o','MarkerSize',6);
+                    currColor = get(h(i),'Color');
+                    set(h(i),'MarkerEdgeColor',currColor,'MarkerFaceColor',currColor);
+%                     hE= get(h(i),'Children');
+%                     errorbarXData = get(hE(2),'XData');
+%                     errorbarXData(4:9:end) = errorbarXData(1:9:end) - 0.2;
+%                     errorbarXData(7:9:end) = errorbarXData(1:9:end) - 0.2;
+%                     errorbarXData(5:9:end) = errorbarXData(1:9:end) + 0.2;
+%                     errorbarXData(8:9:end) = errorbarXData(1:9:end) + 0.2;
+%                     set(hE(2), 'XData', errorbarXData);
                end
            else
                Xaxis=repmat(1:length(bAct(:,1)),[length(bAct(1,:)) 1]);
                h=errorbar(handle,Xaxis',bAct,seAct,'.');%strcat('.',FitResult.colors{mod(i-1,length(FitResult.colors))+1}));
-           end
+               set(h, 'LineStyle', 'none', 'Marker', '.');%,...
+
+               for n=1:length(h)
+                   currColor = get(h(n),'Color');
+                   set(h(n),'MarkerEdgeColor',currColor,'MarkerFaceColor',currColor);
+
+               end
+            end
 
             hold on;
 
@@ -584,24 +1166,40 @@
                v=axis;
                vdiff = .8*v(4);
 
-               for i=fitNum
-                  plot(handle,find(sigIndex(:,i)==1),vdiff*ones(length(find(sigIndex(:,i)==1)),1)-i*.1,strcat('.',FitResult.colors{mod(i-1,length(FitResult.colors))+1})); hold on;
+               for i=1:length(fitNum)
+                  plot(handle,find(sigIndex(:,i)==1),vdiff*ones(length(find(sigIndex(:,i)==1)),1)-i*.1,strcat('*',FitResult.colors{mod(i-1,length(FitResult.colors))+1})); hold on;
                end
             end
-           ylabel('Fit Coefficients','Interpreter','latex');
-           xtickLabels = fitObj.getPlotParams.xLabels;
+           ylabel('GLM Fit Coefficients','Interpreter','none');
+           xtickLabels = fitObj.getPlotParams.xLabels(subIndex);
            xticks = 1:(length(xtickLabels));
 
            set(handle,'xtick',xticks,'xtickLabel',xtickLabels,'FontSize',6);
-           if(max(fitObj.numCoeffs)>1)
-            xticklabel_rotate([],90,[],'Fontsize',6);
+%            axis tight;
+           if(max(fitObj.numCoeffs)>=1)
+            xticklabel_rotate([],90,[],'Fontsize',10);
            end
 %            hT=rotateticklabel(gca,-90);
-           legend(handle,fitObj.lambda.dataLabels,'Location','Best');
-           %axis tight;
+           h_legend=legend(handle,fitObj.lambda.dataLabels(fitNum),'Location','NorthEast');
+           set(h_legend,'FontSize',14)
+            pos = get(h_legend,'position');
+            set(h_legend, 'position',[pos(1)+.05 pos(2) pos(3:4)]);
+
+%            axis tight;
+            title({'GLM Coefficients with 95% CIs (* p<0.05)'},'FontWeight','bold',...
+            'FontSize',11,...
+            'FontName','Arial');
+            set(gca,'FontName', 'Arial' );
+            set(gca, ...
+              'TickLength'  , [.02 .02] , ...
+              'YGrid'       , 'on'      , ...
+              'LineWidth'   , 1         );
+            hx=get(gca,'XLabel');  hy=get(gca,'YLabel');
+            set([hx hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
+
 
         end
-        function plotResults(fitObj)
+        function h=plotResults(fitObj)
             % plotResults(fitObj)
             % Generates KS plot, auto-correlation function of the inverse
             % gaussian transformed rescaled ISIs, the sequential
@@ -609,46 +1207,67 @@
             % rescaled ISIs (zj vs. zj-1), the GLM regression coefficients,
             % and the Point Process Residual.
                 scrsz = get(0,'ScreenSize');
-                figure('Position',[scrsz(3)*.1 scrsz(4)*.1 scrsz(3)*.8 scrsz(4)*.8]);
+                h=figure('OuterPosition',[scrsz(3)*.01 scrsz(4)*.04 scrsz(3)*.98 scrsz(4)*.95]);
 
                 subplot(2,4,[1 2]); fitObj.KSPlot; %make the plot
-                text(.45, .95,strcat('Neuron:',num2str(fitObj.neuronNumber)));
+                ht=text(.45, .95,strcat('Neuron:',num2str(fitObj.neuronNumber)));
+                set(ht,'FontName', 'Arial','FontWeight','bold','FontSize',10);
+
                 subplot(2,4,3); fitObj.plotInvGausTrans;
                 subplot(2,4,4); fitObj.plotSeqCorr;
                 subplot(2,4,[7 8]); fitObj.plotResidual;
                 subplot(2,4,[5 6]); fitObj.plotCoeffs;
         end
-        function handle = KSPlot(fitObj)
+        function handle = KSPlot(fitObj,fitNum)
             % handle = KSPlot(fitObj)
             % computes the K-S plot for each of the the candidate rate
             % functions in this FitResult object. These candidate rate
             % functions are numbered according to the order in which they
             % were added to the FitResult.
+            if(nargin<2)
+                fitNum=1:fitObj.numResults;
+            end
             h=gcf;
             %h=[];
             figure(h);
         %     size(xAxis)
         %     size(KSSorted)
             N = length(fitObj.KSStats.KSSorted);
-            xaxis = fitObj.KSStats.xAxis(:,1);
-            handle=plot(fitObj.KSStats.xAxis,fitObj.KSStats.KSSorted, xaxis,xaxis, 'k-.',xaxis, xaxis+1.36/sqrt(N), 'r', xaxis,xaxis-1.36/sqrt(N), 'r' );
-
-            %set(gca,'xtick',[],'ytick',[],'ztick', [])
-            axis( [0 1 0 1] );
-            dataLabels = cell(1,fitObj.lambda.dimension);
-            for i=1:fitObj.lambda.dimension
-                dataLabels{i} = fitObj.lambda.dataLabels{i};
+            if(~isempty(fitObj.KSStats.xAxis))
+                xaxis = fitObj.KSStats.xAxis(:,1);
+                % Plot the CIs
+                plot(xaxis,xaxis, 'k-.'); hold on;
+                plot(xaxis, xaxis+1.36/sqrt(N), 'r','Linewidth',1);
+                plot(xaxis,xaxis-1.36/sqrt(N), 'r','Linewidth',1 );
+                handle=plot(fitObj.KSStats.xAxis(:,fitNum),fitObj.KSStats.KSSorted(:,fitNum),'Linewidth',2);
+
+                %set(gca,'xtick',[],'ytick',[],'ztick', [])
+                axis( [0 1 0 1] );
+%             dataLabels = cell(1,fitObj.lambda.dimension);
+%             for i=1:fitObj.lambda.dimension
+                dataLabels = fitObj.lambda.dataLabels(fitNum);
+%             end
+                h_legend=legend(handle,dataLabels,'Location','SouthEast');
+                set(h_legend,'FontSize',14)
+            end
+            hx=xlabel('Ideal Uniform CDF');
+            hy=ylabel('Empirical CDF');
+            title({'KS Plot of Rescaled ISIs'; 'with 95% Confidence Intervals'},'FontWeight','bold','FontSize',11,'FontName','Arial');
+            set([hx, hy],'FontName', 'Arial','FontWeight','bold','FontSize',12);
+
+            set(gca, ...
+              'TickLength'  , [.02 .02] , ...
+              'YTick'       , 0:.2:1, ...
+              'XTick'       , 0:.2:1, ...
+              'LineWidth'   , 1         );
             end
-            legend(dataLabels,'Location','Best');
-            xlabel('Uniform CDF');
-            ylabel('Empirical CDF of Rescaled ISIs');
-            title('KS Plot with 95% Confidence Intervals');
-
-
-        end
 
         function structure = toStructure(fitObj)
-            %
+            % structure =  toStructure(fitObj)
+            % Converts FitResult object to a matlab structure than can then
+            % be saved. The structure is compatible with the FitResult
+            % static method FitResult.fromStructure(structure) that returns
+            % the object corresponding structure passed in.
             fnames = fieldnames(fitObj);
 
             for i=1:length(fnames)
@@ -701,8 +1320,8 @@
             pval=zeros(1,fitObj.numResults);
             dataLabels = fitObj.lambda.dataLabels;
             for i=1:fitObj.numResults
-               handle = plot(fitObj.Z(1:end-1,i),fitObj.Z(2:end,i),strcat('.',Analysis.colors{mod(i-1,length(Analysis.colors))+1})); hold on;
-               [rhoTemp,p]= corrcoef(fitObj.Z(1:end-1,i),fitObj.Z(2:end,i));%handle=scatterhist(fitResults.Z(1:end-1,i),fitResults.Z(2:end,i))
+               handle = plot(fitObj.U(1:end-1,i),fitObj.U(2:end,i),strcat('.',Analysis.colors{mod(i-1,length(Analysis.colors))+1})); hold on;
+               [rhoTemp,p]= corrcoef(fitObj.U(1:end-1,i),fitObj.U(2:end,i));%handle=scatterhist(fitResults.Z(1:end-1,i),fitResults.Z(2:end,i))
 
                [~,columns]=size(rhoTemp);
                 if(columns>1)
@@ -718,17 +1337,51 @@
             end
 
 
-            legend(dataLabels,'Location','Best');
-            ylabel('z_{j+1}'); xlabel('z_j');
-            axis tight;
+            h_legend=legend(dataLabels,'Location','NorthEast');
+            set(h_legend,'FontSize',14)
+            pos = get(h_legend,'position');
+            if(~isempty(pos))
+                 set(h_legend, 'position',[pos(1)+.05 pos(2) pos(3:4)]);
+            end
+
+            hy=ylabel('u_{j+1}'); hx=xlabel('u_j');
+            set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
+
+            axis([0 1 0 1]);
+            title({'Sequential Correlation of'; 'Rescaled ISIs'},'FontWeight','bold',...
+            'FontSize',11,...
+            'FontName','Arial');
+
+
+            set(gca, ...
+              'TickLength'  , [.02 .02] , ...
+              'YTick'       , 0:.25:1, ...
+              'XTick'       , 0:.25:1, ...
+              'LineWidth'   , 1         );
+
+
+
         end
         function handle = plotInvGausTrans(fitObj)
+            % handle = plotInvGausTrans(fitObj)
+            % Plots the Auto-correlation function of the X_j's where:
+            % Z_j: rescaled ISI from the Time Rescaling Theorem.
+            %      Exponential Rate 1 under true conditional intensity
+            %      function.
+            % U_j: 1-exp(-Z_j). Uniform on the interval [0,1) if Z_j's
+            %      are exponential rate 1.
+            %
+            % X_j: norminv(U_j,0,1). Gaussian mean 0, stdev 1 if U_j's are
+            %      U([0,1))
+            %
+
             %[rows,colm] = size(fitObj.X);
             %index=find(fitObj.invGausStats.lags==1);
             %lags=fitObj.invGausStats.lags;
-            rhoSig=fitObj.invGausStats.rhoSig;
+            [fitObj.X,rhoSig,confBoundSig] = Analysis.computeInvGausTrans(fitObj.Z);
+%             rhoSig=fitObj.invGausStats.rhoSig;
             n=length(fitObj.X);
-            confBoundSig = fitObj.invGausStats.confBoundSig;
+%             confBoundSig = fitObj.invGausStats.confBoundSig;
             handle=[];
 %              for i=1:colm
 %                     %i
@@ -737,19 +1390,51 @@
 %                     hold on;
 %                     %labelArray{i} = ['Fit ' num2str(i)];
 %              end
-
-            rhoSig.plot;
-            legend(fitObj.lambda.dataLabels,'Location','Best');
+            if(~isempty(rhoSig))
+                rhoSig.plot;
+            end
+            h_legend=legend(fitObj.lambda.dataLabels,'Location','NorthEast');
+            set(h_legend,'FontSize',14)
+            pos = get(h_legend,'position');
+            if(~isempty(pos))
+             set(h_legend, 'position',[pos(1)+.05 pos(2) pos(3:4)]);
+            end
             %legend(h,labelArray);
-            hold on; confBoundSig.plot;
-
+            hold on;
+            if(~isempty(confBoundSig))
+                confBoundSig.plot;
+            end
+            title({'Autocorrelation Function';'of Rescaled ISIs'; 'with 95% CIs'},'FontWeight','bold',...
+            'FontSize',11,...
+            'FontName','Arial');
+            hx=get(gca,'XLabel');  hy=get(gca,'YLabel');
+            set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
+            set(gca, ...
+              'TickLength'  , [.02 .02] , ...
+              'LineWidth'   , 1         );
+            v=axis;
+            maxY = max(abs(v(3:4)))*(1.1); %add 10%
+            axis([v(1:2) -maxY maxY]);
         end
         function handle = plotResidual(fitObj)
             % handle = plotResidual(fitObj)
             % Plots the Point Process Residual
             handle=fitObj.Residual.plot;
             legend off;
-            legend(fitObj.Residual.dataLabels,'Location','Best');
+            h_legend=legend(fitObj.lambda.dataLabels,'Location','NorthEast');
+            set(h_legend,'FontSize',14)
+            pos = get(h_legend,'position');
+%             set(h_legend, 'position',[.91 .41 pos(3:4)]);
+            set(h_legend, 'position',[pos(1)+.05 pos(2) pos(3:4)]);
+            title('Point Process Residual','FontWeight','bold',...
+            'FontSize',11,...
+            'FontName','Arial');
+            xlabel('time [s]','Interpreter','none');
+            hx=get(gca,'XLabel');  hy=get(gca,'YLabel');
+            set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
+            v=axis;
+            maxY = max(abs(v(3:4)))*(1.1); %add 10%
+            axis([v(1:2) -maxY maxY]);
         end
 
 
@@ -766,9 +1451,23 @@
             fitObj.U        =U;
             fitObj.KSStats.xAxis    =xAxis;
             fitObj.KSStats.KSSorted =KSSorted;
+
+
+            for i=1:size(xAxis,2);
+                [differentDists(i),pVal(i),ks_stat(i)]=kstest2(fitObj.KSStats.xAxis(:,i) ,fitObj.KSStats.KSSorted(:,i));
+            end
+            if(~exist('differentDists'))
+                differentDists=1;
+            end
+            if(~exist('pVal'))
+                pVal=1;
+            end
+
             fitObj.KSStats.ks_stat  =ks_stat;
-            N = length(fitObj.KSStats.KSSorted);
-            fitObj.KSStats.withinConfInt = ks_stat<1.36/sqrt(N);
+%             N = length(fitObj.KSStats.KSSorted);
+%             fitObj.KSStats.withinConfInt = ks_stat<1.36/sqrt(N);
+            fitObj.KSStats.withinConfInt = ~differentDists;
+            fitObj.KSStats.pValue = pVal;
         end
         function setInvGausStats(fitObj, X,rhoSig,confBoundSig)
             % setInvGausStats(fitObj,X,rhoSig,confBoundSig)
@@ -785,6 +1484,39 @@
            fitObj.Residual = M;
         end
 
+        function [paramVals, paramSE, paramSigIndex] = getParam(fitObj,paramNames,fitNum)
+           % output is a matrix of length equal to the total number of
+           % paramNames
+           % and one column for each fit
+
+            if(nargin<3)
+                fitNum = 1:fitObj.numResults;
+            end
+           if(isempty(fitObj.plotParams))
+               fitObj.computePlotParams;
+           end
+           paramVals = zeros(length(paramNames),length(fitNum));
+
+           if(nargout>1)
+            paramSE = zeros(length(paramNames),length(fitNum));
+           end
+
+           if(nargout>2)
+            paramSigIndex = zeros(length(paramNames),length(fitNum));
+           end
+
+           for i=1:length(paramNames)
+                paramIndex=find(strcmp(paramNames(i),fitObj.uniqueCovLabels));
+                paramVals(i,:) = fitObj.plotParams.bAct(paramIndex,fitNum);
+                if(nargout>1)
+                    paramSE(i,:) = fitObj.plotParams.seAct(paramIndex,fitNum);
+                end
+                if(nargout>2)
+                    paramSigIndex(i,:) = fitObj.plotParams.sigIndex(paramIndex,fitNum);
+                end
+           end
+
+        end
 
 
     end
@@ -792,8 +1524,19 @@
     methods (Static)
 
         function fitObj = fromStructure(structure)
+            % fitObj = fromStructure(structure)
+            % Returns a FitResult object from the input structure.
+            % This is used to be able to save and restore FitResult
             if(isa(structure,'struct'))
-                spikeObj=nspikeTrain.fromStructure(structure.neuralSpikeTrain);
+                if(isa(structure.neuralSpikeTrain,'cell'))
+                    spikeObj = cell(1,length(structure.neuralSpikeTrain));
+                   for k=1:length(structure.neuralSpikeTrain)
+                       spikeObj{k} = nspikeTrain.fromStructure(structure.neuralSpikeTrain{k});
+
+                   end
+                else
+                    spikeObj=nspikeTrain.fromStructure(structure.neuralSpikeTrain);
+                end
                 lambda=Covariate.fromStructure(structure.lambda);
                 rhoSig=SignalObj.signalFromStruct(structure.invGausStats.rhoSig);
                 confBoundSig = SignalObj.signalFromStruct(structure.invGausStats.confBoundSig);
@@ -803,10 +1546,40 @@
                     ensHistObject{i} = History.fromStructure(structure.ensHistObjects{i});
                 end
                 configColl = ConfigColl.fromStructure(structure.configs);
-                fitObj=FitResult(spikeObj,structure.covLabels,structure.numHist,histObjects,ensHistObject,lambda,structure.b, structure.dev, structure.stats,structure.AIC,structure.BIC,configColl,structure.XvalData,structure.XvalTime,structure.fitType);
+
+                % Support legacy saved structures that predate select fields.
+                logLL = NaN(1,max(1,structure.numResults));
+                if isfield(structure,'logLL')
+                    logLL = structure.logLL;
+                    if isscalar(logLL) && structure.numResults > 1
+                        logLL = repmat(logLL,1,structure.numResults);
+                    end
+                end
+
+                XvalData = [];
+                if isfield(structure,'XvalData')
+                    XvalData = structure.XvalData;
+                end
+
+                XvalTime = [];
+                if isfield(structure,'XvalTime')
+                    XvalTime = structure.XvalTime;
+                end
+
+                fitType = '';
+                if isfield(structure,'fitType')
+                    fitType = structure.fitType;
+                elseif isfield(structure,'distribution')
+                    fitType = structure.distribution;
+                end
+
+%                 fitObj=FitResult(spikeObj,covLabels,numHist,histObjects,ensHistObj,lambda,b, dev, stats,AIC,BIC,logLL, configColl,XvalData,XvalTime,distribution)
+                fitObj=FitResult(spikeObj,structure.covLabels,structure.numHist,histObjects,ensHistObject,lambda,structure.b, structure.dev, structure.stats,structure.AIC,structure.BIC,logLL,configColl,XvalData,XvalTime,fitType);
                 fitObj.setKSStats(structure.Z,structure.U, structure.KSStats.xAxis, structure.KSStats.KSSorted, structure.KSStats.ks_stat);
                 fitObj.setInvGausStats(structure.X,rhoSig,confBoundSig);
                 fitObj.setFitResidual(M);
+                fitObj.setNeuronName(structure.neuronNumber);
+
             elseif(isa(structure,'cell')) %cell array of FitResult objects
                 fitObj = cell(size(structure));
                for i=1:length(structure)
@@ -817,15 +1590,21 @@
         end
 
         function structCell = CellArrayToStructure(fitResObjCell)
+            % structCell = CellArrayToStructure(fitResObjCell)
+            % For every FitResult structure in the a cell array, it calls
+            % FitResults.fromStructure
+
            if(isa(fitResObjCell,'FitResult'))
                structCell = fitResObjCell.toStructure;
-           elseif(isa(fitResObjCell,'cell'))
+           elseif(isa(fitResObjCell,'cell')&&~isempty(fitResObjCell))
                if(isa(fitResObjCell{1},'FitResult'))
                    structCell = cell(size(fitResObjCell));
                   for i=1:length(fitResObjCell)
                      structCell{i} = fitResObjCell{i}.toStructure;
                   end
                end
+           else
+            structCell={};
            end
 
         end
@@ -910,7 +1689,7 @@
 
     % if no XTickLabel AND no XTick are defined use the current XTickLabel
     %if nargin < 3 & (~exist('XTick') | isempty(XTick)),
-    if (nargin < 3 || isempty(varargin{1})) & (~exist('XTick') | isempty(XTick)),
+    if (nargin < 3 || isempty(varargin{1})) && (~exist('XTick') || isempty(XTick)),
         xTickLabels = get(gca,'XTickLabel')  ; % use current XTickLabel
         if ~iscell(xTickLabels)
             % remove trailing spaces if exist (typical with auto generated XTickLabel)
@@ -994,7 +1773,17 @@
     set(get(gca,'ylabel'),'units','pixel')          ;
 
     origpos = get(gca,'position')                   ;
-    textsizes = cell2mat(get(hText,'extent'))       ;
+
+    % Modified by Iahn Cajigas
+    % 3/4/2011 to allow for a single
+    % xTickLabel to work properly
+    temphText = get(hText,'extent');
+    if(isa(temphText,'cell'))
+        textsizes = cell2mat(temphText)       ;
+    else
+        textsizes = temphText;
+    end
+
     longest =  max(textsizes(:,4))                  ;
 
     laborigext = get(get(gca,'xlabel'),'extent')    ;
@@ -1049,6 +1838,7 @@
 
 end
 function [uniqueLabels, indexIntoOriginal, restoreIndex] = getUniqueLabels(covLabels)
+% Given a set of covLabels, returns a subset of labels that are unique
             offset = 0;
             for i=1:length(covLabels)
                 currLabels = covLabels{i};
@@ -1057,21 +1847,45 @@
             end
             [uniqueLabels, indexIntoOriginal, restoreIndex] = unique(allLabels);
 end
-
Input argument "spikeObj" is undefined.
-
-Error in ==> FitResult>FitResult.FitResult at 86
-            if(isnumeric(spikeObj.name))
-
\ No newline at end of file +--> + + diff --git a/helpfiles/FitResult.m b/helpfiles/FitResult.m deleted file mode 100644 index f5321cd..0000000 --- a/helpfiles/FitResult.m +++ /dev/null @@ -1,1745 +0,0 @@ -classdef FitResult < handle -% FITRESULT -% stores results of a fit using the Analysis object -% The results are for a single neuron over a range of configurations -% -% FitResult Examples -% -% see also Analysis -% -% Reference page in Help browser -% FitResult Reference - - -% -% nSTAT v1 Copyright (C) 2012 Masschusetts Institute of Technology -% Cajigas, I, Malik, WQ, Brown, EN -% This program is free software; you can redistribute it and/or -% modify it under the terms of the GNU General Public License as published -% by the Free Software Foundation; either version 2 of the License, or -% (at your option) any later version. -% -% This program is distributed in the hope that it will be useful, -% but WITHOUT ANY WARRANTY; without even the implied warranty of -% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -% See the GNU General Public License for more details. -% -% You should have received a copy of the GNU General Public License -% along with this program; if not, write to the Free Software Foundation, -% Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - - properties - numResults %Number of results in this FitResult object - lambda %Lambda signal - numCoeffs %Number of coefficients for each fitResult - fitType %Poisson or Binomial - - b %coefficients for each fit - - dev %deviance for each fit - AIC %Akaike's Information Criterion for each fit - BIC %Baysian Information Criterion for each fit - logLL %Log Likelihood - stats %Relevant statistics for each fit - configs % the config collection for the different fits - configNames % names of the the differen fits - neuronNumber % the number of the neuron the data comes from - neuralSpikeTrain % the spike data - covLabels - uniqueCovLabels - indicesToUniqueLabels - numHist % Number of history terms (used for indexing into the regression coefficients) for each fit - histObjects % History object for self firing - ensHistObjects % History object to be applied to the neuron's neigbors to compute ensemble effect - flatMask - Z % Rescaled spike times from the Time-Rescaling theorem, exponential rate 1 - U % Transformed z's -> uniform in [0,1] - X % Transformed u's -> gaussian in [-inf, inf] - Residual %fit residual -% xAxis -% KSSorted -% ks_stat - invGausStats - KSStats %Kolmogorov Smirnov Statistics - plotParams - XvalData % cell array of raw data used for validation - XvalTime % cell array of time vectors for each element of XvalData - validation % a FitResult object with the validation data - minTime % The minTime from the spikeTrain (not necessarily the analysis) - maxTime % The maxTime for the spikeTrain (not necessarily the analysis) - end - properties (Constant,Hidden) - colors={'b','g','r','c','m','y','k'}; - end - - - methods - - function fitObj=FitResult(spikeObj,covLabels,numHist,histObjects,ensHistObj,lambda,b, dev, stats,AIC,BIC,logLL, configColl,XvalData,XvalTime,distribution) - % fitObj=FitResult(spikeObj,covLabels,numHist,histObjects,ensHistObj,lambda,b, dev, stats,AIC,BIC,configColl,XvalData,XvalTime) - % Stores the results of multiple regressions for a single neuron into a accessible structure. - % - % spikeObj: The spike train for the neuron whose results are - % being stored. - % covLabels: A 2-d cell array, the jth row has all the labels for the covariates used in the jth fit - % numHist: The number of history terms in each of the N fits. - % histObjects: The History object for each of the N fits - % ensHistObj: The History object for used to compute the ensemble history effect. - % lambda: The conditional intensity function evaluated usin the - % data. Each dimension of lambda corresponds to the a different - % GLM Fit. - % b: N-component cell array containing the GLM regression - % coefficient of each of the fits. The jth component has all - % the regression coefficients for the jth trial. - % dev: vector of Deviances for each the GLM fits. - % stats: Cell array of the stats parameters for each GLM fit; - % AIC: vector of Akaike's information criteria for each the GLM fits. - % BIC: vector of Bayes Information criteria for each the GLM fits. - % configColl: configCollection object used to generate this - % results - % XvalData: Data to be used for validation. - % XvalTime: Time vector for the data. - - if(nargin< 14) - XvalTime =[]; - end - if(nargin<13) - XvalData =[]; - end - - if(isa(spikeObj,'cell')) - for i=1:length(spikeObj) - if(isnumeric(spikeObj{i}.name)) - nNumber(i) =spikeObj{i}.name; - else - nNumber(i) = str2double(spikeObj{i}.name(~isletter(spikeObj{i}.name))); - end - minTime(i)=spikeObj{i}.minTime; - maxTime(i)=spikeObj{i}.maxTime; - end - nNumber = unique(nNumber); - minTime = unique(minTime); - maxTime = unique(maxTime); - if(length(nNumber)>1) - error('Can only have a FitResults with spike trains from a single neuron'); - end - if(length(minTime)>1 || length(maxTime)>1) - error('Spike Trains are of different lengths'); - end - - elseif(isa(spikeObj,'nspikeTrain')) - if(isnumeric(spikeObj.name)) - nNumber =spikeObj.name; - else - nNumber = str2double(spikeObj.name(~isletter(spikeObj.name))); - end - minTime=spikeObj.minTime; - maxTime=spikeObj.maxTime; - - end - - fitObj.neuronNumber = nNumber; %str2num(spikeObj.name); - fitObj.neuralSpikeTrain = spikeObj; - fitObj.minTime = minTime; - fitObj.maxTime = maxTime; - - fitObj.numResults = 0; - fitObj.configs = configColl; - fitObj.configNames = configColl.getConfigNames; - fitObj.covLabels=covLabels; - fitObj.uniqueCovLabels= getUniqueLabels(covLabels); - - fitObj.mapCovLabelsToUniqueLabels; - fitObj.numHist=numHist; - fitObj.histObjects = histObjects; - fitObj.ensHistObjects = ensHistObj; - fitObj.addParamsToFit(fitObj.neuronNumber,lambda,b, dev, stats,AIC,BIC,logLL,configColl); - fitObj.Z =[]; %rescaled spikes times - exponentially dist. - fitObj.U =[]; %rescaled spike times - uniformly dist. - fitObj.X =[]; %rescaled spike times - gaussian dist. - fitObj.Residual =[]; %fit residual for PP - fitObj.KSStats.xAxis =[]; - fitObj.KSStats.KSSorted =[]; - fitObj.KSStats.ks_stat =[]; - fitObj.invGausStats.rhoSig=[]; - fitObj.invGausStats.confBoundSig=[]; - fitObj.plotParams = []; - fitObj.XvalData = XvalData; - fitObj.XvalTime = XvalTime; - - fitObj.fitType = distribution; - - end - function fitObj = setNeuronName(fitObj,name) - fitObj.neuronNumber = name; - end - function mFitRes = mergeResults(fitObj,newFitObj) - % mFitRes = mergeResults(fitObj,newFitObj) - % mFitRes contains the results from fitObj followed by the - % results of newFitObj in a single new FitResult object - % - % newFitObj can be of class 'FitResult' or a cell array of - % 'FitResult' objects. In the latter case, the results are - % apppended in the order that they appear in each cell array. - if(isa(newFitObj,'FitResult')) -% newFitObj.neuronNumber - if(fitObj.neuronNumber ==newFitObj.neuronNumber) - spikeObj = fitObj.neuralSpikeTrain; - covLabels = fitObj.covLabels(1:fitObj.numResults); - covLabels((fitObj.numResults+1):(fitObj.numResults+newFitObj.numResults)) = newFitObj.covLabels(1:newFitObj.numResults); - numHist = fitObj.numHist(1:fitObj.numResults); - numHist((fitObj.numResults+1):(fitObj.numResults+newFitObj.numResults)) = newFitObj.numHist(1:newFitObj.numResults); - histObjects=fitObj.histObjects(1:fitObj.numResults); - histObjects((fitObj.numResults+1):(fitObj.numResults+newFitObj.numResults)) = newFitObj.histObjects(1:newFitObj.numResults); - ensHistObjects=fitObj.ensHistObjects(1:fitObj.numResults); - ensHistObjects((fitObj.numResults+1):(fitObj.numResults+newFitObj.numResults)) = newFitObj.ensHistObjects(1:newFitObj.numResults); - b=fitObj.b(1:fitObj.numResults); - b((fitObj.numResults+1):(fitObj.numResults+newFitObj.numResults)) = newFitObj.b(1:newFitObj.numResults); - dev = [fitObj.dev newFitObj.dev]; - AIC = [fitObj.AIC newFitObj.AIC]; - BIC = [fitObj.BIC newFitObj.BIC]; - logLL = [fitObj.logLL newFit.logLL]; - stats=fitObj.stats(1:fitObj.numResults); - stats((fitObj.numResults+1):(fitObj.numResults+newFitObj.numResults)) = newFitObj.stats(1:newFitObj.numResults); - lambda = fitObj.lambda.merge(newFitObj.lambda); - - for i=1:fitObj.numResults - config{i}=fitObj.configs.getConfig(i); - end - offset=fitObj.numResults; - for i=1:newFitObj.numResults - config{i+offset}=newFitObj.configs.getConfig(i); - end - configColl= ConfigColl(config); - - XvalData = [fitObj.XvalData newFitObj.XvalData]; - XvalTime = [fitObj.XvalTime newFitObj.XvalTime]; - distribution=fitObj.fitType(1:fitObj.numResults); - distribution((fitObj.numResults+1):(fitObj.numResults+newFitObj.numResults)) = newFitObj.fitType(1:newFitObj.numResults); - tempZ = zeros(length(fitObj.Z),size(newFitObj.Z,2)); - tempU = zeros(length(fitObj.U),size(newFitObj.U,2)); - tempZ(1:length(newFitObj.Z),:) = newFitObj.Z; - tempU(1:length(newFitObj.U),:) = newFitObj.U; - Z=[fitObj.Z tempZ]; - U=[fitObj.U tempU]; - [X,rhoSig,confBoundSig] = Analysis.computeInvGausTrans(Z); - - M=fitObj.Residual.merge(newFitObj.Residual); - - origLength = size(fitObj.KSStats.xAxis,1); - currLength = size(newFitObj.KSStats.xAxis,1); - - if(currLength~=origLength) - %we use this because some times the time scales - %dont match up. In particular when spikeTrain is - %segmented by steps or windows and the window sizes - %are normalized to 1. - newX = fitObj.KSStats.xAxis; - oldX = newFitObj.KSStats.xAxis; - oldY = newFitObj.KSStats.KSSorted; - y = interp1(oldX,oldY,newX(:,1),'spline','extrap'); - xAxis = [fitObj.KSStats.xAxis newX(:,1)]; - KSSorted = [fitObj.KSStats.KSSorted y]; - else - - xAxis = [fitObj.KSStats.xAxis newFitObj.KSStats.xAxis]; - KSSorted = [fitObj.KSStats.KSSorted newFitObj.KSStats.KSSorted]; - - end - ks_stat = [fitObj.KSStats.ks_stat newFitObj.KSStats.ks_stat]; - mFitRes=FitResult(spikeObj,covLabels,numHist,histObjects,ensHistObjects,lambda,b, dev, stats,AIC,BIC,logLL,configColl,XvalData,XvalTime,distribution); - mFitRes.setKSStats(Z,U, xAxis, KSSorted, ks_stat); - mFitRes.setInvGausStats(X,rhoSig,confBoundSig); - mFitRes.setFitResidual(M); - - - elseif(isa(newFitObj,'cell')) - if(isa(newFitObj{1},'FitResult')) - for i=1:length(newFitObj) - if(i==1) - mFitRes = fitObj.mergeResults(newFitObj{i}); - else - mFitRes = mFitRes.mergeResults(newFitObj{i}); - end - end - end - end - end - - end - function subsetFit = getSubsetFitResult(fitObj,subfits) - %subfits is a row vector with numbers corresponding to the - %subfits - if(and(min(subfits)>0,max(subfits)<=fitObj.numResults)) - spikeObj = fitObj.neuralSpikeTrain; - covLabels = fitObj.covLabels(subfits); - numHist = fitObj.numHist(subfits); - histObjects = fitObj.histObjects(subfits); - ensHistObj = fitObj.ensHistObjects(subfits); - lambda = fitObj.lambda.getSubSignal(subfits); - b = fitObj.b(subfits); - dev = fitObj.dev(subfits); - stats = fitObj.stats(subfits); - AIC = fitObj.AIC(subfits); - BIC = fitObj.BIC(subfits); - logLL = fitObj.logLL(subfits); - configColl = fitObj.configs.getSubsetConfigs(subfits); - XvalData = fitObj.XvalData; - XvalTime = fitObj.XvalTime; - distribution = fitObj.fitType; - - subsetFit=FitResult(spikeObj,covLabels,numHist,histObjects,ensHistObj,lambda,b, dev, stats,AIC,BIC,logLL,configColl,XvalData,XvalTime,distribution); - Z = fitObj.Z(:,subfits); - U = fitObj.U(:,subfits); - X = fitObj.X(:,subfits); - xAxis= fitObj.KSStats.xAxis(:,subfits); - KSSorted=fitObj.KSStats.KSSorted(:,subfits); - ks_stat=fitObj.KSStats.ks_stat(subfits); - rhoSig=fitObj.invGausStats.rhoSig.getSubSignal(subfits); - confBoundSig=fitObj.invGausStats.confBoundSig; - M = fitObj.Residual.getSubSignal(subfits); - - subsetFit.setKSStats(Z,U, xAxis, KSSorted, ks_stat); - subsetFit.setInvGausStats(X,rhoSig,confBoundSig); - subsetFit.setFitResidual(M); - end - end - function addParamsToFit(fitObj,neuronNum,lambda,b, dev, stats,AIC,BIC,logLL,configColl) - % addParamsToFit(fitObj,neuronNum,lambda,b, dev, stats,AIC,BIC,configColl) - % Add the specified parameters to the current FitResult object - % only if the neuronNum matches the neuronNum of this object - if(fitObj.neuronNumber==neuronNum) - if(isa(lambda,'cell')) - newLambda=lambda{1}; - for i=2:length(lambda) - newLambda = newLambda.merge(lambda{i}); - end - elseif(isa(lambda,'Covariate')||isa(lambda,'SignalObj')) - newLambda = lambda; - end - - numNewResults = newLambda.dimension;%number of new elements - if(nargin<8) - configColl = cell(1,numNewResults); - end - - if(numNewResults==1) - fitObj.b{fitObj.numResults+1} = b{1}; - - fitObj.dev(fitObj.numResults+1) = dev; - fitObj.stats{fitObj.numResults+1}= stats{1}; - if(nargin<7) - fitObj.AIC(fitObj.numResults+1) = 2*length(b)+dev; - fitObj.BIC(fitObj.numResults+1) = length(b)*log(length(newLambda.time))+dev; - delta = 1/newLambda.sampleRate; - fitObj.logLL(fitObj.numResults+1) = sum(y.*log(data*delta)+(1-y).*(1-newLambda.data*delta)); - else - fitObj.AIC(fitObj.numResults+1) = AIC; - fitObj.BIC(fitObj.numResults+1) = BIC; - fitObj.logLL(fitObj.numResults+1) = logLL; - end - - fitObj.numCoeffs(fitObj.numResults+1) = length(b); - else - for i=1:numNewResults - fitObj.b{fitObj.numResults+i} = b{i}; - - fitObj.dev(fitObj.numResults+i) = dev(i); - fitObj.stats{fitObj.numResults+i}= stats{i}; - if(nargin<7) - fitObj.AIC(fitObj.numResults+i) = 2*length(b{i})+dev(i); - fitObj.BIC(fitObj.numResults+i) = length(b{i})*log(length(newLambda.time))+dev(i); - delta=fitObj.neuralSpikeTrain.sampleRate; - y=fitObj.neuralSpikeTrain.getSigRep.dataToMatrix; - - fitObj.logLL(fitObj.numResults+i)= sum(y.*log(newLambda.data*delta)+(1-y).*(1-newLambda.data*delta)); - else - fitObj.AIC(fitObj.numResults+i) = AIC(i); - fitObj.BIC(fitObj.numResults+i) = BIC(i); - fitObj.logLL(fitObj.numResults+i)= logLL(i); - end - fitObj.numCoeffs(fitObj.numResults+i) = length(b{i}); - end - end - if(fitObj.numResults ==0) - fitObj.lambda = newLambda; - else - fitObj.lambda = fitObj.lambda.merge(newLambda); %new lambda - end - - fitObj.numResults = fitObj.numResults+numNewResults; - dataLabels = cell(1,fitObj.numResults); - for i=1:fitObj.numResults - dataLabels{i} = strcat('\lambda_{',num2str(i),'}'); - end - fitObj.lambda.setDataLabels(dataLabels); - - fitObj.configs.addConfig(configColl); - fitObj.configNames = fitObj.configs.getConfigNames; - else - error('Neuron number does not match'); - end - end - function [lambda, logLL] = computeValLambda(fitObj) - % lambda = computeValLambda(fitObj) - % Returns a Covariate object lambda. This is the Conditional - % intensity function evaluated using the validation data - lambdaData = zeros(length(fitObj.XvalTime{1}),fitObj.numResults); - for i=1:fitObj.numResults - lambdaData(:,i) = fitObj.evalLambda(i,fitObj.XvalData{i}); - end - lambda=Covariate(fitObj.XvalTime{1},lambdaData,... - '\lambda(t)',fitObj.lambda.xlabelval,... - fitObj.lambda.xunits,'Hz',fitObj.lambda.dataLabels); - delta = 1/lambda.sampleRate; - y=fitObj.neuralSpikeTrain.getSigRep.dataToMatrix; - logLL =sum(y.*log(lambda.data*delta)+(1-y).*(1-lambda.data*delta)); - end - - function mapCovLabelsToUniqueLabels(fitObj) - % mapCovLabelsToUniqueLabels(fitObj) - % Used internally by the FitResult class generate a matrix that - % maps how covariate labels of the fit object map to unique - % covariate labels. For example, multiple fits that have a - % constant baseline term will be assumed to refer to the same - % "baseline" term and not two separate ones - flatMask = zeros(length(fitObj.uniqueCovLabels),length(fitObj.covLabels)); - for j=1:length(fitObj.covLabels) - currLabels = fitObj.covLabels{j}; - index=zeros(1,length(currLabels)); - for i=1:length(currLabels) - index(i)=strmatch(currLabels{i}, fitObj.uniqueCovLabels, 'exact'); - end - - fitObj.indicesToUniqueLabels{j} = index; - flatMask(index,j) = 1; - end - fitObj.flatMask = flatMask; - end - function p=getPlotParams(fitObj) - % p=getPlotParams(fitObj) - if(isempty(fitObj.plotParams)) - fitObj.computePlotParams; - end - p=fitObj.plotParams; - end - function plotValidation(fitObj) - % plotValidation(fitObj) - % calls plotResults on the validation FitResult object if - % validation data is present. Note that the GLM coefficients - % are not recomputed and therefore the same as those obtained - % from the training data. - if(~isempty(fitObj.validation)) - fitObj.validation.plotResults; - else - display('Validation Data not available to plot'); - end - end - function answer = isValDataPresent(fitObj) - % answer = isValDataPresent(fitObj) - % returns 1 if validation data is present. This method is used - % to determine if validation data is available to compute the - % validation results. - answer = 0; - if(~isempty(fitObj.XvalTime) && ~isempty(fitObj.XvalData)) - for i=1:length(fitObj.XvalTime) - currTime = fitObj.XvalTime{i}; - if(~isempty(currTime)) - if(currTime(end)-currTime(1)>0) - answer =1; - break; - end - end - end - - end - - end - function lambdaData = evalLambda(fitObj,lambdaIndex,newData) - % lambdaData = evalLambda(fitObj,lambdaIndex,newData) - % lambdaIndex: the index of the corresponding lambda to be - % evaluated with the new data. - % newData: matrix of covariates in same order as fits without - % constant term in first column -% if(isa(newData,'double')) -% [~,columns] = size(newData); -% tempData = cell(1,columns); -% for i=1:columns -% tempData{i} = newData(:,i); -% end -% newData = tempData; -% end - - if(lambdaIndex>0 && lambdaIndex <= fitObj.numResults) - b=fitObj.b{lambdaIndex}; %coefficient matrix - if(isempty(newData)) - [rows,~] = size(newData); - baseline=ones(rows,1); - lambdaData = exp(b(1)*baseline); - else - if(isa(newData,'double')) %matrix, 1 column per coefficient - baseline=ones(length(newData),1); - [~,columns] = size(newData); - - if(length(b)>=1) - lambdaData = exp(newData*b(1:end)); - if(strcmp(fitObj.fitType{lambdaIndex},'poisson')) -% lambdaData = exp(newData*b(1:end)); -% lambdaData = exp(b(1) + newData*b(2:end)); - else -% lambdaData = exp(b(1) + newData*b(2:end)); - lambdaData = lambdaData./(1+lambdaData); - end -% else -% if(strcmp(fitObj.fitType{lambdaIndex},'poisson')) -% lambdaData = exp(b(1)*baseline); -% -% else -% lambdaData = exp(b(1)*baseline); -% lambdaData = lambdaData./(1+lambdaData); -% end - end - lambdaData = lambdaData*fitObj.neuralSpikeTrain.sampleRate; - elseif(isa(newData,'cell')) % a cell array, each element is matrix of values for each coeff -% baseline=ones(size(newData{1})); %design matrix - runSum=0; - for i=1:(length(newData)) %-fitObj.numHist(lambdaIndex)) -% if(i==1) -% runSum = b(1)*baseline; -% else - if(i<=length(b)) - runSum = runSum+b(i)*newData{i}; - end -% end - end - if(strcmp(fitObj.fitType{lambdaIndex},'poisson')) - lambdaData = exp(runSum); - lambdaData = lambdaData*fitObj.neuralSpikeTrain.sampleRate; - else - lambdaData = exp(runSum); - lambdaData = lambdaData./(1+lambdaData); - lambdaData = lambdaData*fitObj.neuralSpikeTrain.sampleRate; - end - else - error('New data must be cell or a matrix'); - end - - end - else - error('Index into fit params is incorrect'); - end - - end -% function handle = plotHist(fitObj,fitNum) -% % handle = plotHist(fitObj,fitNum) -% % plots the history terms used in this FitResult object -% % if fitNum is not specified then fitNum=1:numResults -% if(nargin<2 || isempty(fitNum)) -% fitNum = 1:fitObj.numResults; -% end -% -% for j=fitNum -% if(j>0 && j <= fitObj.numResults) -% b=fitObj.b{j}; %coefficient matrix -% startHistIndex = length(b)-fitObj.numHist(j)+1; -% if(startHistIndex=windowTimes(i),t<=windowTimes(i+1)); -% histEffect(index)=exp(-bHist(i))-1; %To offset zero coeffs -% end -% end -% else -% t=[0; 0.00001]; -% histEffect =[0;0]; -% end -% if(j==fitNum(1)) -% hSig = SignalObj(t,histEffect,'History','time','s','',fitObj.lambda.dataLabels{j}); -% else -% hSig = hSig.merge(SignalObj(t,histEffect,'History Effect','time','s','',fitObj.lambda.dataLabels{j})); -% end -% end -% end -% N=floor(length(hSig.time)./70); B=ones(1,N)/N; A=1; -% handle=hSig.filtfilt(B,A).plot; -% end - function computePlotParams(fitObj,fitNum) - if(nargin<2) - fitNum = 1:fitObj.numResults; - end - index=find(sum(fitObj.flatMask,2)>0);%1:length(fitObj.flatMask(:,1)); - %Only use the labels that appear in at least one fit - %Otherwise that parameter was not present for any of the - %regressions and just takes up plot real-estate - - sigIndex=zeros(length(index),length(fitNum)); - bAct = nan(length(index),length(fitNum)); - seAct= nan(length(index),length(fitNum)); - - for i=fitNum - %this indexing is to avoid extremely large se's from - %affecting plots - criteria = find(fitObj.stats{i}.se'<100); - %indicesForFit = find(fitObj.flatMask(index,i)==1); - indicesForFit = fitObj.indicesToUniqueLabels{i}; - bVals = fitObj.b{i}(criteria); - bAct(indicesForFit(criteria),i) = bVals; %sorted according to uniqueLabels - seVals = fitObj.stats{i}.se(criteria)'; - seAct(indicesForFit(criteria),i)= seVals; %sorted according to uniqueLabels; - temp = sign([bAct(:,i)-seAct(:,i) bAct(:,i)+seAct(:,i)]); - productOfSigns = temp(:,1).*temp(:,2); %should be positive - sIndex=and(productOfSigns>0,seAct(:,i)~=0); - sigIndex(:,i)=sIndex; - end - fitObj.plotParams.bAct = bAct; - fitObj.plotParams.seAct= seAct; - fitObj.plotParams.sigIndex = sigIndex; - fitObj.plotParams.xLabels = cell(length(index),1); - fitObj.plotParams.xLabels = fitObj.uniqueCovLabels; - -% for i=1:(length(index)) -% if(i==1) -% fitObj.plotParams.xLabels{i} = 'baseline'; -% %text(i, 0,'baseline','interpreter','latex'); -% else -% fitObj.plotParams.xLabels{i} = fitObj.covLabels{index(i)-1}; -% %text(i, 0,fitObj.covLabels{index(i)-1},'interpreter','latex'); -% end -% end - tempVal =sum(fitObj.flatMask,2); - fitObj.plotParams.numResultsCoeffPresent =tempVal(index); - end - - function [coeffIndex, epochId,numEpochs] = getCoeffIndex(fitObj,fitNum,sortByEpoch) - if(nargin<3 || isempty(sortByEpoch)) - sortByEpoch=0; - end - if(nargin<2 || isempty(fitNum)) - fitNum = 1:fitObj.numResults; - end - if(isempty(fitObj.plotParams)) - fitObj.computePlotParams; - end - [histIndex, epochId] = fitObj.getHistIndex(fitNum,sortByEpoch); - allIndex = 1:length(fitObj.uniqueCovLabels); - - nonHistIndex = setdiff(allIndex,histIndex); -% nonNANIndex = find(sum(~isnan(fitObj.plotParams.bAct(:,fitNum)),2)>=1); - nonNANIndex= allIndex; - actCoeffIndex = nonHistIndex(ismember(nonHistIndex, nonNANIndex)); - allCoeffTerms = fitObj.uniqueCovLabels(actCoeffIndex); -% coeffName = cell(size(allCoeffTerms)); - epochStartInd=regexp(allCoeffTerms,'_\{\d*\}','start'); - epochEndInd=regexp(allCoeffTerms,'_\{\d*\}','end'); - - allCoeffIndex = []; - nonEpochIndex=[]; -% nonEmptyCoeffNameInd = []; - epochsExist =0; - for i=1:length(allCoeffTerms) - if(~isempty(allCoeffTerms{i})) - allCoeffIndex = [allCoeffIndex i]; - - if(~isempty(epochStartInd{i})) -% nonEmptyCoeffNameInd = [nonEmptyCoeffNameInd i]; - epochsExist=1; - actStart = epochStartInd{i}+2; - actEnd = epochEndInd{i}-1; - numEpoch(i) = str2num(allCoeffTerms{i}(actStart:actEnd)); -% coeffName{i} = allCoeffTerms{i}(1:actStart-3); - - - else - nonEpochIndex = [nonEpochIndex i]; - numEpoch(i) = 0; % make terms that only appear once part of epoch 0. - - end - end - - end - -% coeffName = coeffName(nonEmptyCoeffNameInd); - if(epochsExist && ~sortByEpoch) - totalEpochs = unique(numEpoch); - coeffIndex = nonEpochIndex; - if(nargout>1) - epochId=zeros(size(nonEpochIndex)); - end - for i=1:length(totalEpochs) - if(totalEpochs(i)~=0) - coeffIndex = [coeffIndex, find(numEpoch==totalEpochs(i))]; - - if(nargout>1) - epochId = [epochId, totalEpochs(i)*ones(size(find(numEpoch==totalEpochs(i))))]; - end - end - end - coeffIndex = actCoeffIndex(coeffIndex); - elseif(epochsExist && sortByEpoch) - coeffIndex = actCoeffIndex(allCoeffIndex); - if(nargout>1) - epochId = numEpoch; - end - else - coeffIndex = actCoeffIndex(allCoeffIndex); - if(nargout>1) - epochId = zeros(size(allCoeffIndex)); %no epochs exist so just create same index for all; - end - end - - -% nonNANIndex = find(sum(~isnan(fitObj.plotParams.bAct(:,fitNum)),2)>=1); - nonNANIndex = allIndex; - coeffIndex = coeffIndex(ismember(coeffIndex, nonNANIndex)); - - if(nargout>2) - numEpochs = length(unique(epochId)); - end - - end - - function h=plotCoeffsWithoutHistory(fitObj,fitNum,sortByEpoch,plotSignificance) - if(nargin<4 || isempty(plotSignificance)) - plotSignificance=1; - end - if(nargin<3 || isempty(sortByEpoch)) - sortByEpoch = 0; - end - if(nargin<2 || isempty(fitNum)) - fitNum = 1:fitObj.numResults; - end - if(isempty(fitObj.plotParams)) - fitObj.computePlotParams; - end - - - coeffIndex = fitObj.getCoeffIndex(fitNum,sortByEpoch); - h=fitObj.plotCoeffs([],fitNum,[],plotSignificance,coeffIndex); - - - - end - - function [histIndex, epochId,numEpochs] = getHistIndex(fitObj,fitNum,sortByEpoch) - %if sortByEpoch==1 then we group all regression terms with the - %same name one next to each other by epoch (time interval). - %Otherwise, we show all epoch one terms, followed by all epoch - %2 terms, etc. - if(nargin<3 || isempty(sortByEpoch)) - sortByEpoch = 0; - end - if(nargin<2 || isempty(fitNum)) - fitNum = 1:fitObj.numResults; - end - if(isempty(fitObj.plotParams)) - fitObj.computePlotParams; - end - - - allHistTerms = regexp(fitObj.uniqueCovLabels,'^[\w*'); - epochStartInd=regexp(fitObj.uniqueCovLabels,'\]_\{\d*\}','start'); - epochEndInd=regexp(fitObj.uniqueCovLabels,'\]_\{\d*\}','end'); - allHistIndex = []; - epochsExist =0; - for i=1:length(allHistTerms) - if(~isempty(allHistTerms{i})) - allHistIndex = [allHistIndex i]; - if(~isempty(epochStartInd{i})) - epochsExist=1; - actStart = epochStartInd{i}+3; - actEnd = epochEndInd{i}-1; - numEpoch(i) = str2num(fitObj.uniqueCovLabels{i}(actStart:actEnd)); - end - end - - end - - if(epochsExist && ~sortByEpoch) - totalEpochs = unique(numEpoch); - histIndex = []; - if(nargout>1) - epochId=[]; - end - for i=1:length(totalEpochs) - histIndex = [histIndex, find(numEpoch==totalEpochs(i))]; - if(nargout>1) - epochId = [epochId, totalEpochs(i)*ones(size(find(numEpoch==totalEpochs(i))))]; - end - end - elseif(epochsExist && sortByEpoch) - histIndex = allHistIndex; - if(nargout>1) - epochId = numEpoch; - end - else - histIndex = allHistIndex; - if(nargout>1) - epochId = zeros(size(allHistIndex)); %no epochs exist so just create same index for all; - end - end - - -% nonNANIndex = find(sum(~isnan(fitObj.plotParams.bAct(:,fitNum)),2)>=1); -% histIndex = histIndex(ismember(histIndex, nonNANIndex)); - - if(nargout>2) - numEpochs = length(unique(epochId)); - end - - end - - function [coeffMat, labels, SEMat] = getCoeffs(fitObj, fitNum) - if(nargin<2 || isempty(fitNum)) - fitNum =1:fitObj.numResults; - end - sortByEpoch = 0; % Make sure we have different time series if the history is divided into epochs; - [coeffIndex, epochId, numEpochs] = fitObj.getCoeffIndex(fitNum,sortByEpoch); - epochNums = unique(epochId); - - - coeffStrings = fitObj.uniqueCovLabels(coeffIndex); - baseStringEndIndex =regexp(coeffStrings,'_\{\d*\}','start'); - - for i=1:length(baseStringEndIndex) - if(~isempty(baseStringEndIndex{i})) - baseStrings{i} = coeffStrings{i}(1:baseStringEndIndex{i}-1); - else - baseStrings{i} = coeffStrings{i}; - end - end - uniqueCoeffs = unique(baseStrings); - - for i=1:length(uniqueCoeffs) - coeffStrIndex{i} = coeffIndex(strcmp(baseStrings,uniqueCoeffs{i})); - if(min(epochId)==0) - epochIndices{i} = epochId(strcmp(baseStrings,uniqueCoeffs{i}))+1; - else - epochIndices{i} = epochId(strcmp(baseStrings,uniqueCoeffs{i})); - end - end - -% -% for i=1:numEpochs -% epochIndices{i} = find(epochId==epochNums(i)); -% epochLength(i) = length(epochIndices{i}); -% end - - - - coeffIndMat= nan(length(uniqueCoeffs),numEpochs); - - labels = cell(size(coeffIndMat)); - for i=1:length(uniqueCoeffs) - coeffIndMat(i,epochIndices{i}) = coeffStrIndex{i}; - labels(i,epochIndices{i}) = fitObj.uniqueCovLabels(coeffStrIndex{i}); - end - - -% for i=1:numEpochs -% coeffIndMat(1:epochLength(i),i) = coeffIndex(epochIndices{i}); -% labels(1:epochLength(i),i) = fitObj.uniqueCovLabels(coeffIndMat(1:epochLength(i),i)); -% end - - - - if(length(fitNum)>1) - coeffMat = nan(size(coeffIndMat,1),size(coeffIndMat,2), length(fitNum)); - SEMat = nan(size(coeffIndMat,1),size(coeffIndMat,2), length(fitNum)); - for i=1:length(fitNum) - for j=1:length(uniqueCoeffs) - bTemp=fitObj.plotParams.bAct(coeffStrIndex{j},i); - seTemp=fitObj.plotParams.seAct(coeffStrIndex{j},i); - coeffMat(j,epochIndices{j},i) = bTemp'; - SEMat(j,epochIndices{j},i) = seTemp'; - end - end - else - coeffMat = nan(size(coeffIndMat,1),size(coeffIndMat,2)); - SEMat = nan(size(coeffIndMat,1),size(coeffIndMat,2)); - for j=1:length(uniqueCoeffs) - bTemp=fitObj.plotParams.bAct(coeffStrIndex{j},fitNum); - seTemp = fitObj.plotParams.seAct(coeffStrIndex{j},fitNum); - coeffMat(j,epochIndices{j}) = bTemp'; - SEMat(j,epochIndices{j}) = seTemp'; - end - - end - - end - - function [histMat, labels, SEMat] = getHistCoeffs(fitObj,fitNum) - if(nargin<2 || isempty(fitNum)) - fitNum =1:fitObj.numResults; - end - sortByEpoch = 0; % Make sure we have different time series if the history is divided into epochs; - [histIndex, epochId, numEpochs] = fitObj.getHistIndex(fitNum,sortByEpoch); - epochNums = unique(epochId); - - - histcoeffStrings = fitObj.uniqueCovLabels(histIndex); - baseStringEndIndex =regexp(histcoeffStrings,'_\{\d*\}','start'); - baseStrings = cell(length(baseStringEndIndex),1); - for i=1:length(baseStringEndIndex) - if(~isempty(baseStringEndIndex{i})) - baseStrings{i} = histcoeffStrings{i}(1:baseStringEndIndex{i}-1); - else - baseStrings{i} = histcoeffStrings{i}; - end - end - uniqueCoeffs = unique(baseStrings); - - for i=1:length(uniqueCoeffs) - histcoeffStrIndex{i} = histIndex(strcmp(baseStrings,uniqueCoeffs{i})); - if(min(epochId)==0) - epochIndices{i} = epochId(strcmp(baseStrings,uniqueCoeffs{i}))+1; - else - epochIndices{i} = epochId(strcmp(baseStrings,uniqueCoeffs{i})); - end - end - -% -% for i=1:numEpochs -% epochIndices{i} = find(epochId==epochNums(i)); -% epochLength(i) = length(epochIndices{i}); -% end - - - - histcoeffIndMat= nan(length(uniqueCoeffs),numEpochs); - labels = cell(size(histcoeffIndMat)); -% SEMat = nan(length(uniqueCoeffs),numEpochs); - for i=1:length(uniqueCoeffs) - histcoeffIndMat(i,epochIndices{i}) = histcoeffStrIndex{i}; - labels(i,epochIndices{i}) = fitObj.uniqueCovLabels(histcoeffStrIndex{i}); -% SEMat(i,epochIndices{i}) = fitObj.se; - end - - -% for i=1:numEpochs -% coeffIndMat(1:epochLength(i),i) = coeffIndex(epochIndices{i}); -% labels(1:epochLength(i),i) = fitObj.uniqueCovLabels(coeffIndMat(1:epochLength(i),i)); -% end - - - - if(length(fitNum)>1) - histMat = nan(size(histcoeffIndMat,1),size(histcoeffIndMat,2), length(fitNum)); - SEMat = nan(size(histcoeffIndMat,1),size(histcoeffIndMat,2), length(fitNum)); - for i=fitNum - for j=1:length(uniqueCoeffs) - bTemp=fitObj.plotParams.bAct(histcoeffStrIndex{j},i); - seTemp = fitObj.plotParams.seAct(histcoeffStrIndex{j},i); - histMat(j,epochIndices{j},i) = bTemp'; - SEMat(j,epochIndices{j},i) = seTemp'; - end - end - else - histMat = nan(size(histcoeffIndMat,1),size(histcoeffIndMat,2)); - SEMat = nan(size(histcoeffIndMat,1),size(histcoeffIndMat,2)); - for j=1:length(uniqueCoeffs) - bTemp=fitObj.plotParams.bAct(histcoeffStrIndex{j},fitNum); - seTemp=fitObj.plotParams.seAct(histcoeffStrIndex{j},fitNum); - histMat(j,epochIndices{j}) = bTemp'; - SEMat(j,epochIndices{j}) = seTemp'; - end - - end - - -% -% if(nargin<2 || isempty(fitNum)) -% fitNum =1:fitObj.numResults; -% end -% sortByEpoch = 0; % Make sure we have different time series if the history is divided into epochs; -% [histIndex, epochId, numEpochs] = fitObj.getHistIndex(fitNum,sortByEpoch); -% epochNums = unique(epochId); -% for i=1:numEpochs -% epochIndices{i} = find(epochId==epochNums(i)); -% epochLength(i) = length(epochIndices{i}); -% end -% -% histIndMat= nan(max(epochLength),numEpochs); -% labels = cell(size(histIndMat)); -% for i=1:numEpochs -% histIndMat(1:epochLength(i),i) = histIndex(epochIndices{i}); -% labels(1:epochLength(i),i) = fitObj.uniqueCovLabels(histIndMat(1:epochLength(i),i)); -% end -% -% -% -% if(length(fitNum)>1) -% histMat = nan(size(histIndMat,1),size(histIndMat,2), length(fitNum)); -% for i=1:length(fitNum) -% for j=1:numEpochs -% bTemp=fitObj.plotParams.bAct(epochIndices{j},i); -% histMat(1:epochLength(j),j,i) = bTemp; -% end -% end -% else -% histMat = nan(size(histIndMat,1),size(histIndMat,2)); -% -% for j=1:numEpochs -% bTemp=fitObj.plotParams.bAct(epochIndices{j},fitNum); -% histMat(1:epochLength(j),j) = bTemp; -% end -% -% end - - end - function h=plotHistCoeffs(fitObj,fitNum,sortByEpoch,plotSignificance) - if(nargin<4 || isempty(plotSignificance)) - plotSignificance=1; - end - if(nargin<3 || isempty(sortByEpoch)) - sortByEpoch=0; - end - if(nargin<2 || isempty(fitNum)) - fitNum = 1:fitObj.numResults; - end - if(isempty(fitObj.plotParams)) - fitObj.computePlotParams; - end - histIndex = fitObj.getHistIndex(fitNum,sortByEpoch); - h=fitObj.plotCoeffs([],fitNum,[],plotSignificance,histIndex); - end - - - function h=plotCoeffs(fitObj,handle,fitNum,plotProps,plotSignificance,subIndex) - % h=plotCoeffs(fitObj,handle,fitNum,plotProps,plotSignificance) - % plots the GLM coefficients for each fit along with the - % confidence intervals. - % fitNum: number of the fit to plot. If not specified, all are - % plotted. - % plotProps: properties to use for the making the plot - % plotSignificance: If 1 then an asterix (*) is place above - % parameters that are statistically different - % from zero with alpha=5%. - - - if(nargin<5 || isempty(plotSignificance)) - plotSignificance = 1; - end - - if(nargin<4 || isempty(plotProps)) - plotProps = []; - end - - if(nargin<3 || isempty(fitNum)) - fitNum = 1:fitObj.numResults; - end - - if(nargin<2 || isempty(handle)) - handle=gca; - end - - if(isempty(fitObj.plotParams)) - fitObj.computePlotParams; - end - - if(nargin<6 || isempty(subIndex)) - subIndex = [fitObj.getHistIndex, fitObj.getCoeffIndex]; - end - - bAct = fitObj.getPlotParams.bAct(subIndex,fitNum); - seAct= fitObj.getPlotParams.seAct(subIndex,fitNum); - sigIndex=fitObj.getPlotParams.sigIndex(subIndex,fitNum); - - if(~isempty(plotProps)) - for i=1:length(fitNum) - h(i)=errorbar(handle,1:length(subIndex),bAct(:,i),seAct(:,i),plotProps{i}); hold on; - set(h(i), 'LineStyle', 'none', 'Marker', '.');%,... -% 'Linewidth',1,'Marker','o','MarkerSize',6); - currColor = get(h(i),'Color'); - set(h(i),'MarkerEdgeColor',currColor,'MarkerFaceColor',currColor); -% hE= get(h(i),'Children'); -% errorbarXData = get(hE(2),'XData'); -% errorbarXData(4:9:end) = errorbarXData(1:9:end) - 0.2; -% errorbarXData(7:9:end) = errorbarXData(1:9:end) - 0.2; -% errorbarXData(5:9:end) = errorbarXData(1:9:end) + 0.2; -% errorbarXData(8:9:end) = errorbarXData(1:9:end) + 0.2; -% set(hE(2), 'XData', errorbarXData); - end - else - Xaxis=repmat(1:length(bAct(:,1)),[length(bAct(1,:)) 1]); - h=errorbar(handle,Xaxis',bAct,seAct,'.');%strcat('.',FitResult.colors{mod(i-1,length(FitResult.colors))+1})); - set(h, 'LineStyle', 'none', 'Marker', '.');%,... - - for n=1:length(h) - currColor = get(h(n),'Color'); - set(h(n),'MarkerEdgeColor',currColor,'MarkerFaceColor',currColor); - - end - end - - hold on; - - - if(plotSignificance==1) - v=axis; - vdiff = .8*v(4); - - for i=1:length(fitNum) - plot(handle,find(sigIndex(:,i)==1),vdiff*ones(length(find(sigIndex(:,i)==1)),1)-i*.1,strcat('*',FitResult.colors{mod(i-1,length(FitResult.colors))+1})); hold on; - end - end - ylabel('GLM Fit Coefficients','Interpreter','none'); - xtickLabels = fitObj.getPlotParams.xLabels(subIndex); - xticks = 1:(length(xtickLabels)); - - set(handle,'xtick',xticks,'xtickLabel',xtickLabels,'FontSize',6); -% axis tight; - if(max(fitObj.numCoeffs)>=1) - xticklabel_rotate([],90,[],'Fontsize',10); - end -% hT=rotateticklabel(gca,-90); - h_legend=legend(handle,fitObj.lambda.dataLabels(fitNum),'Location','NorthEast'); - set(h_legend,'FontSize',14) - pos = get(h_legend,'position'); - set(h_legend, 'position',[pos(1)+.05 pos(2) pos(3:4)]); - -% axis tight; - title({'GLM Coefficients with 95% CIs (* p<0.05)'},'FontWeight','bold',... - 'FontSize',11,... - 'FontName','Arial'); - set(gca,'FontName', 'Arial' ); - set(gca, ... - 'TickLength' , [.02 .02] , ... - 'YGrid' , 'on' , ... - 'LineWidth' , 1 ); - hx=get(gca,'XLabel'); hy=get(gca,'YLabel'); - set([hx hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold'); - - - end - function h=plotResults(fitObj) - % plotResults(fitObj) - % Generates KS plot, auto-correlation function of the inverse - % gaussian transformed rescaled ISIs, the sequential - % correlation coefficient between neigboring pairs of the - % rescaled ISIs (zj vs. zj-1), the GLM regression coefficients, - % and the Point Process Residual. - scrsz = get(0,'ScreenSize'); - h=figure('OuterPosition',[scrsz(3)*.01 scrsz(4)*.04 scrsz(3)*.98 scrsz(4)*.95]); - - subplot(2,4,[1 2]); fitObj.KSPlot; %make the plot - ht=text(.45, .95,strcat('Neuron:',num2str(fitObj.neuronNumber))); - set(ht,'FontName', 'Arial','FontWeight','bold','FontSize',10); - - subplot(2,4,3); fitObj.plotInvGausTrans; - subplot(2,4,4); fitObj.plotSeqCorr; - subplot(2,4,[7 8]); fitObj.plotResidual; - subplot(2,4,[5 6]); fitObj.plotCoeffs; - end - function handle = KSPlot(fitObj,fitNum) - % handle = KSPlot(fitObj) - % computes the K-S plot for each of the the candidate rate - % functions in this FitResult object. These candidate rate - % functions are numbered according to the order in which they - % were added to the FitResult. - if(nargin<2) - fitNum=1:fitObj.numResults; - end - h=gcf; - %h=[]; - figure(h); - % size(xAxis) - % size(KSSorted) - N = length(fitObj.KSStats.KSSorted); - if(~isempty(fitObj.KSStats.xAxis)) - xaxis = fitObj.KSStats.xAxis(:,1); - % Plot the CIs - plot(xaxis,xaxis, 'k-.'); hold on; - plot(xaxis, xaxis+1.36/sqrt(N), 'r','Linewidth',1); - plot(xaxis,xaxis-1.36/sqrt(N), 'r','Linewidth',1 ); - handle=plot(fitObj.KSStats.xAxis(:,fitNum),fitObj.KSStats.KSSorted(:,fitNum),'Linewidth',2); - - %set(gca,'xtick',[],'ytick',[],'ztick', []) - axis( [0 1 0 1] ); -% dataLabels = cell(1,fitObj.lambda.dimension); -% for i=1:fitObj.lambda.dimension - dataLabels = fitObj.lambda.dataLabels(fitNum); -% end - h_legend=legend(handle,dataLabels,'Location','SouthEast'); - set(h_legend,'FontSize',14) - end - hx=xlabel('Ideal Uniform CDF'); - hy=ylabel('Empirical CDF'); - title({'KS Plot of Rescaled ISIs'; 'with 95% Confidence Intervals'},'FontWeight','bold','FontSize',11,'FontName','Arial'); - set([hx, hy],'FontName', 'Arial','FontWeight','bold','FontSize',12); - - set(gca, ... - 'TickLength' , [.02 .02] , ... - 'YTick' , 0:.2:1, ... - 'XTick' , 0:.2:1, ... - 'LineWidth' , 1 ); - end - - function structure = toStructure(fitObj) - % structure = toStructure(fitObj) - % Converts FitResult object to a matlab structure than can then - % be saved. The structure is compatible with the FitResult - % static method FitResult.fromStructure(structure) that returns - % the object corresponding structure passed in. - fnames = fieldnames(fitObj); - - for i=1:length(fnames) - - currObj = fitObj.(fnames{i}); - if(strcmp(fnames{i},'histObjects')||strcmp(fnames{i},'ensHistObjects')) - for j=1:fitObj.numResults - tempObj = fitObj.(fnames{i}){j}; - if(~isempty(tempObj)) - structure.(fnames{i}){j} = tempObj.toStructure; - else - structure.(fnames{i}){j} = tempObj; - end - end - elseif(strcmp(fnames{i},'invGausStats')) - tempNames = fieldnames(fitObj.(fnames{i})); - for j=1:length(tempNames) - tempObj = currObj.(tempNames{j}); - if(~isempty(tempObj)) - structure.(fnames{i}).(tempNames{j})= tempObj.dataToStructure; - else - structure.(fnames{i}).(tempNames{j})= tempObj; - end - - end - - else - - if(isa(currObj,'double')||isa(currObj,'cell')) - structure.(fnames{i}) = currObj; - elseif(isa(currObj,'Covariate') ||isa(currObj,'ConfigColl')||isa(currObj,'nspikeTrain')) - structure.(fnames{i}) = currObj.toStructure; - elseif(isa(currObj,'SignalObj')) - structure.(fnames{i}) = currObj.dataToStructure; - elseif(isa(currObj,'struct')) - structure.(fnames{i}) = currObj; - end - end - end - - end - - - function handle = plotSeqCorr(fitObj) - % handle = plotSeqCorr(fitObj) - % plot zj+1 against zj - - %colors = {'.b','.g','.r','.c','.m','.y','.k'}; - rho=zeros(1,fitObj.numResults); - pval=zeros(1,fitObj.numResults); - dataLabels = fitObj.lambda.dataLabels; - for i=1:fitObj.numResults - handle = plot(fitObj.U(1:end-1,i),fitObj.U(2:end,i),strcat('.',Analysis.colors{mod(i-1,length(Analysis.colors))+1})); hold on; - [rhoTemp,p]= corrcoef(fitObj.U(1:end-1,i),fitObj.U(2:end,i));%handle=scatterhist(fitResults.Z(1:end-1,i),fitResults.Z(2:end,i)) - - [~,columns]=size(rhoTemp); - if(columns>1) - rho(i) = rhoTemp(1,2); - pval(i)= p(1,2); - else - rho(i) = rhoTemp; - pval(i)= p; - end - dataLabels{i} = strcat(dataLabels{i},', \rho=',num2str(rho(i),'%0.2g'),' (p=',num2str(pval(i),'%0.2g'),')'); - %get(h,'AlphaData'); - %set(h,'FaceAlpha',0.2,'EdgeAlpha',0.8,'EdgeColor',color{i}); - end - - - h_legend=legend(dataLabels,'Location','NorthEast'); - set(h_legend,'FontSize',14) - pos = get(h_legend,'position'); - if(~isempty(pos)) - set(h_legend, 'position',[pos(1)+.05 pos(2) pos(3:4)]); - end - - hy=ylabel('u_{j+1}'); hx=xlabel('u_j'); - set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold'); - - axis([0 1 0 1]); - title({'Sequential Correlation of'; 'Rescaled ISIs'},'FontWeight','bold',... - 'FontSize',11,... - 'FontName','Arial'); - - - set(gca, ... - 'TickLength' , [.02 .02] , ... - 'YTick' , 0:.25:1, ... - 'XTick' , 0:.25:1, ... - 'LineWidth' , 1 ); - - - - end - function handle = plotInvGausTrans(fitObj) - % handle = plotInvGausTrans(fitObj) - % Plots the Auto-correlation function of the X_j's where: - % Z_j: rescaled ISI from the Time Rescaling Theorem. - % Exponential Rate 1 under true conditional intensity - % function. - % U_j: 1-exp(-Z_j). Uniform on the interval [0,1) if Z_j's - % are exponential rate 1. - % - % X_j: norminv(U_j,0,1). Gaussian mean 0, stdev 1 if U_j's are - % U([0,1)) - % - - %[rows,colm] = size(fitObj.X); - %index=find(fitObj.invGausStats.lags==1); - %lags=fitObj.invGausStats.lags; - [fitObj.X,rhoSig,confBoundSig] = Analysis.computeInvGausTrans(fitObj.Z); -% rhoSig=fitObj.invGausStats.rhoSig; - n=length(fitObj.X); -% confBoundSig = fitObj.invGausStats.confBoundSig; - handle=[]; -% for i=1:colm -% %i -% htemp=plot(lags',rho(:,i),strcat('.',FitResults.colors{mod(i-1,length(Analysis.colors))+1})); -% handle=[handle,htemp]; -% hold on; -% %labelArray{i} = ['Fit ' num2str(i)]; -% end - if(~isempty(rhoSig)) - rhoSig.plot; - end - h_legend=legend(fitObj.lambda.dataLabels,'Location','NorthEast'); - set(h_legend,'FontSize',14) - pos = get(h_legend,'position'); - if(~isempty(pos)) - set(h_legend, 'position',[pos(1)+.05 pos(2) pos(3:4)]); - end - %legend(h,labelArray); - hold on; - if(~isempty(confBoundSig)) - confBoundSig.plot; - end - title({'Autocorrelation Function';'of Rescaled ISIs'; 'with 95% CIs'},'FontWeight','bold',... - 'FontSize',11,... - 'FontName','Arial'); - hx=get(gca,'XLabel'); hy=get(gca,'YLabel'); - set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold'); - set(gca, ... - 'TickLength' , [.02 .02] , ... - 'LineWidth' , 1 ); - v=axis; - maxY = max(abs(v(3:4)))*(1.1); %add 10% - axis([v(1:2) -maxY maxY]); - end - function handle = plotResidual(fitObj) - % handle = plotResidual(fitObj) - % Plots the Point Process Residual - handle=fitObj.Residual.plot; - legend off; - h_legend=legend(fitObj.lambda.dataLabels,'Location','NorthEast'); - set(h_legend,'FontSize',14) - pos = get(h_legend,'position'); -% set(h_legend, 'position',[.91 .41 pos(3:4)]); - set(h_legend, 'position',[pos(1)+.05 pos(2) pos(3:4)]); - title('Point Process Residual','FontWeight','bold',... - 'FontSize',11,... - 'FontName','Arial'); - xlabel('time [s]','Interpreter','none'); - hx=get(gca,'XLabel'); hy=get(gca,'YLabel'); - set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold'); - v=axis; - maxY = max(abs(v(3:4)))*(1.1); %add 10% - axis([v(1:2) -maxY maxY]); - end - - - function setKSStats(fitObj, Z, U, xAxis, KSSorted, ks_stat) - % setKSStats(fitObj, Z, xAxis, KSSorted, ks_stat) - % Allows KS statistics to be set after object creation - % Z: Rescaled ISIs from the Time Rescaling Theorem - % xAxis: xAxis of the KS plot - % KSSorted: the sorted values of Uj=1-exp(-zj) - % ks_stat: the maximum deviation from the 45 degree line for - % all of the fits. - % - fitObj.Z =Z; - fitObj.U =U; - fitObj.KSStats.xAxis =xAxis; - fitObj.KSStats.KSSorted =KSSorted; - - - for i=1:size(xAxis,2); - [differentDists(i),pVal(i),ks_stat(i)]=kstest2(fitObj.KSStats.xAxis(:,i) ,fitObj.KSStats.KSSorted(:,i)); - end - if(~exist('differentDists')) - differentDists=1; - end - if(~exist('pVal')) - pVal=1; - end - - fitObj.KSStats.ks_stat =ks_stat; -% N = length(fitObj.KSStats.KSSorted); -% fitObj.KSStats.withinConfInt = ks_stat<1.36/sqrt(N); - fitObj.KSStats.withinConfInt = ~differentDists; - fitObj.KSStats.pValue = pVal; - end - function setInvGausStats(fitObj, X,rhoSig,confBoundSig) - % setInvGausStats(fitObj,X,rhoSig,confBoundSig) - % Sets the inverse gaussian transformed rescaled ISIs and the - % confidence bounds after the object has been created. - %fitObj.U=U; - fitObj.X=X; - fitObj.invGausStats.rhoSig=rhoSig; - fitObj.invGausStats.confBoundSig=confBoundSig; - end - function setFitResidual(fitObj,M) - % setFitResidual(fitObj,M). - % Adds the point process residual to the FitResult object - fitObj.Residual = M; - end - - function [paramVals, paramSE, paramSigIndex] = getParam(fitObj,paramNames,fitNum) - % output is a matrix of length equal to the total number of - % paramNames - % and one column for each fit - - if(nargin<3) - fitNum = 1:fitObj.numResults; - end - if(isempty(fitObj.plotParams)) - fitObj.computePlotParams; - end - paramVals = zeros(length(paramNames),length(fitNum)); - - if(nargout>1) - paramSE = zeros(length(paramNames),length(fitNum)); - end - - if(nargout>2) - paramSigIndex = zeros(length(paramNames),length(fitNum)); - end - - for i=1:length(paramNames) - paramIndex=find(strcmp(paramNames(i),fitObj.uniqueCovLabels)); - paramVals(i,:) = fitObj.plotParams.bAct(paramIndex,fitNum); - if(nargout>1) - paramSE(i,:) = fitObj.plotParams.seAct(paramIndex,fitNum); - end - if(nargout>2) - paramSigIndex(i,:) = fitObj.plotParams.sigIndex(paramIndex,fitNum); - end - end - - end - - - end - - methods (Static) - - function fitObj = fromStructure(structure) - % fitObj = fromStructure(structure) - % Returns a FitResult object from the input structure. - % This is used to be able to save and restore FitResult - if(isa(structure,'struct')) - if(isa(structure.neuralSpikeTrain,'cell')) - spikeObj = cell(1,length(structure.neuralSpikeTrain)); - for k=1:length(structure.neuralSpikeTrain) - spikeObj{k} = nspikeTrain.fromStructure(structure.neuralSpikeTrain{k}); - - end - else - spikeObj=nspikeTrain.fromStructure(structure.neuralSpikeTrain); - end - lambda=Covariate.fromStructure(structure.lambda); - rhoSig=SignalObj.signalFromStruct(structure.invGausStats.rhoSig); - confBoundSig = SignalObj.signalFromStruct(structure.invGausStats.confBoundSig); - M = Covariate.fromStructure(structure.Residual); - for i=1:structure.numResults - histObjects{i} = History.fromStructure(structure.histObjects{i}); - ensHistObject{i} = History.fromStructure(structure.ensHistObjects{i}); - end - configColl = ConfigColl.fromStructure(structure.configs); - if(isfield(structure,'logLL')) - logLL = structure.logLL; - else - logLL = zeros(size(structure.AIC)); - end - fitObj=FitResult(spikeObj,structure.covLabels,structure.numHist,histObjects,ensHistObject,lambda,structure.b, structure.dev, structure.stats,structure.AIC,structure.BIC,logLL,configColl,structure.XvalData,structure.XvalTime,structure.fitType); - fitObj.setKSStats(structure.Z,structure.U, structure.KSStats.xAxis, structure.KSStats.KSSorted, structure.KSStats.ks_stat); - fitObj.setInvGausStats(structure.X,rhoSig,confBoundSig); - fitObj.setFitResidual(M); - fitObj.setNeuronName(structure.neuronNumber); - - elseif(isa(structure,'cell')) %cell array of FitResult objects - fitObj = cell(size(structure)); - for i=1:length(structure) - fitObj{i} = FitResult.fromStructure(structure{i}); - end - end - - end - - function structCell = CellArrayToStructure(fitResObjCell) - % structCell = CellArrayToStructure(fitResObjCell) - % For every FitResult structure in the a cell array, it calls - % FitResults.fromStructure - - if(isa(fitResObjCell,'FitResult')) - structCell = fitResObjCell.toStructure; - elseif(isa(fitResObjCell,'cell')&&~isempty(fitResObjCell)) - if(isa(fitResObjCell{1},'FitResult')) - structCell = cell(size(fitResObjCell)); - for i=1:length(fitResObjCell) - structCell{i} = fitResObjCell{i}.toStructure; - end - end - else - structCell={}; - end - - end - - end - -end - -%Helper functions -function hText = xticklabel_rotate(XTick,rot,varargin) - %hText = xticklabel_rotate(XTick,rot,XTickLabel,varargin) Rotate XTickLabel - % - % Syntax: xticklabel_rotate - % - % Input: - % {opt} XTick - vector array of XTick positions & values (numeric) - % uses current XTick values or XTickLabel cell array by - % default (if empty) - % {opt} rot - angle of rotation in degrees, 90° by default - % {opt} XTickLabel - cell array of label strings - % {opt} [var] - "Property-value" pairs passed to text generator - % ex: 'interpreter','none' - % 'Color','m','Fontweight','bold' - % - % Output: hText - handle vector to text labels - % - % Example 1: Rotate existing XTickLabels at their current position by 90° - % xticklabel_rotate - % - % Example 2: Rotate existing XTickLabels at their current position by 45° and change - % font size - % xticklabel_rotate([],45,[],'Fontsize',14) - % - % Example 3: Set the positions of the XTicks and rotate them 90° - % figure; plot([1960:2004],randn(45,1)); xlim([1960 2004]); - % xticklabel_rotate([1960:2:2004]); - % - % Example 4: Use text labels at XTick positions rotated 45° without tex interpreter - % xticklabel_rotate(XTick,45,NameFields,'interpreter','none'); - % - % Example 5: Use text labels rotated 90° at current positions - % xticklabel_rotate([],90,NameFields); - % - % Note : you can not re-run xticklabel_rotate on the same graph. - % - % - - - % This is a modified version of xticklabel_rotate90 by Denis Gilbert - % Modifications include Text labels (in the form of cell array) - % Arbitrary angle rotation - % Output of text handles - % Resizing of axes and title/xlabel/ylabel positions to maintain same overall size - % and keep text on plot - % (handles small window resizing after, but not well due to proportional placement with - % fixed font size. To fix this would require a serious resize function) - % Uses current XTick by default - % Uses current XTickLabel is different from XTick values (meaning has been already defined) - - % Brian FG Katz - % bfgkatz@hotmail.com - % 23-05-03 - % Modified 03-11-06 after user comment - % Allow for exisiting XTickLabel cell array - - % Other m-files required: cell2mat - % Subfunctions: none - % MAT-files required: none - % - % See also: xticklabel_rotate90, TEXT, SET - - % Based on xticklabel_rotate90 - % Author: Denis Gilbert, Ph.D., physical oceanography - % Maurice Lamontagne Institute, Dept. of Fisheries and Oceans Canada - % email: gilbertd@dfo-mpo.gc.ca Web: http://www.qc.dfo-mpo.gc.ca/iml/ - % February 1998; Last revision: 24-Mar-2003 - - % check to see if xticklabel_rotate has already been here (no other reason for this to happen) - if isempty(get(gca,'XTickLabel')), - error('xticklabel_rotate : can not process, either xticklabel_rotate has already been run or XTickLabel field has been erased') ; - end - - % if no XTickLabel AND no XTick are defined use the current XTickLabel - %if nargin < 3 & (~exist('XTick') | isempty(XTick)), - if (nargin < 3 || isempty(varargin{1})) && (~exist('XTick') || isempty(XTick)), - xTickLabels = get(gca,'XTickLabel') ; % use current XTickLabel - if ~iscell(xTickLabels) - % remove trailing spaces if exist (typical with auto generated XTickLabel) - temp1 = num2cell(xTickLabels,2) ; - for loop = 1:length(temp1), - temp1{loop} = deblank(temp1{loop}) ; - end - xTickLabels = temp1 ; - end - varargin = varargin(2:length(varargin)); - end - - % if no XTick is defined use the current XTick - if (~exist('XTick') | isempty(XTick)), - XTick = get(gca,'XTick') ; % use current XTick - end - - %Make XTick a column vector - XTick = XTick(:); - - if ~exist('xTickLabels'), - % Define the xtickLabels - % If XtickLabel is passed as a cell array then use the text - if (length(varargin)>0) & (iscell(varargin{1})), - xTickLabels = varargin{1}; - varargin = varargin(2:length(varargin)); - else - xTickLabels = num2str(XTick); - end - end - - if length(XTick) ~= length(xTickLabels), - error('xticklabel_rotate : must have same number of elements in "XTick" and "XTickLabel"') ; - end - - %Set the Xtick locations and set XTicklabel to an empty string - set(gca,'XTick',XTick,'XTickLabel','') - - if nargin < 2, - rot = 90 ; - end - - % Determine the location of the labels based on the position - % of the xlabel - hxLabel = get(gca,'XLabel'); % Handle to xlabel - xLabelString = get(hxLabel,'String'); - - % if ~isempty(xLabelString) - % warning('You may need to manually reset the XLABEL vertical position') - % end - - set(hxLabel,'Units','data'); - xLabelPosition = get(hxLabel,'Position'); - y = xLabelPosition(2); - - %CODE below was modified following suggestions from Urs Schwarz - y=repmat(y,size(XTick,1),1); - % retrieve current axis' fontsize - fs = get(gca,'fontsize'); - - % Place the new xTickLabels by creating TEXT objects - hText = text(XTick, y, xTickLabels,'fontsize',fs); - - % Rotate the text objects by ROT degrees - set(hText,'Rotation',rot,'HorizontalAlignment','right',varargin{:}) - - % Adjust the size of the axis to accomodate for longest label (like if they are text ones) - % This approach keeps the top of the graph at the same place and tries to keep xlabel at the same place - % This approach keeps the right side of the graph at the same place - - set(get(gca,'xlabel'),'units','data') ; - labxorigpos_data = get(get(gca,'xlabel'),'position') ; - set(get(gca,'ylabel'),'units','data') ; - labyorigpos_data = get(get(gca,'ylabel'),'position') ; - set(get(gca,'title'),'units','data') ; - labtorigpos_data = get(get(gca,'title'),'position') ; - - set(gca,'units','pixel') ; - set(hText,'units','pixel') ; - set(get(gca,'xlabel'),'units','pixel') ; - set(get(gca,'ylabel'),'units','pixel') ; - - origpos = get(gca,'position') ; - - % Modified by Iahn Cajigas - % 3/4/2011 to allow for a single - % xTickLabel to work properly - temphText = get(hText,'extent'); - if(isa(temphText,'cell')) - textsizes = cell2mat(temphText) ; - else - textsizes = temphText; - end - - longest = max(textsizes(:,4)) ; - - laborigext = get(get(gca,'xlabel'),'extent') ; - laborigpos = get(get(gca,'xlabel'),'position') ; - - - labyorigext = get(get(gca,'ylabel'),'extent') ; - labyorigpos = get(get(gca,'ylabel'),'position') ; - leftlabdist = labyorigpos(1) + labyorigext(1) ; - - % assume first entry is the farthest left - leftpos = get(hText(1),'position') ; - leftext = get(hText(1),'extent') ; - leftdist = leftpos(1) + leftext(1) ; - if leftdist > 0, leftdist = 0 ; end % only correct for off screen problems - - botdist = origpos(2) + laborigpos(2) ; - newpos = [origpos(1)-leftdist longest+botdist origpos(3)+leftdist origpos(4)-longest+origpos(2)-botdist] ; - set(gca,'position',newpos) ; - - % readjust position of nex labels after resize of plot - set(hText,'units','data') ; - for loop= 1:length(hText), - set(hText(loop),'position',[XTick(loop), y(loop)]) ; - end - - - % adjust position of xlabel and ylabel - laborigpos = get(get(gca,'xlabel'),'position') ; - set(get(gca,'xlabel'),'position',[laborigpos(1) laborigpos(2)-longest 0]) ; - - % switch to data coord and fix it all - set(get(gca,'ylabel'),'units','data') ; - set(get(gca,'ylabel'),'position',labyorigpos_data) ; - set(get(gca,'title'),'position',labtorigpos_data) ; - - set(get(gca,'xlabel'),'units','data') ; - labxorigpos_data_new = get(get(gca,'xlabel'),'position') ; - set(get(gca,'xlabel'),'position',[labxorigpos_data(1) labxorigpos_data_new(2)]) ; - - - % Reset all units to normalized to allow future resizing - set(get(gca,'xlabel'),'units','normalized') ; - set(get(gca,'ylabel'),'units','normalized') ; - set(get(gca,'title'),'units','normalized') ; - set(hText,'units','normalized') ; - set(gca,'units','normalized') ; - - if nargout < 1, - clear hText - end - -end -function [uniqueLabels, indexIntoOriginal, restoreIndex] = getUniqueLabels(covLabels) -% Given a set of covLabels, returns a subset of labels that are unique - offset = 0; - for i=1:length(covLabels) - currLabels = covLabels{i}; - allLabels((1:length(currLabels))+offset) = currLabels; - offset=length(allLabels); - end - [uniqueLabels, indexIntoOriginal, restoreIndex] = unique(allLabels); -end - - - \ No newline at end of file diff --git a/helpfiles/FitResultExamples.html b/helpfiles/FitResultExamples.html index 4b9728b..031b899 100644 --- a/helpfiles/FitResultExamples.html +++ b/helpfiles/FitResultExamples.html @@ -1,85 +1,93 @@ - - + - - - - - FitResult Examples - - - - -
-

FitResult Examples

- -
- - - \ No newline at end of file + + diff --git a/helpfiles/FitResultReference.html b/helpfiles/FitResultReference.html new file mode 100644 index 0000000..2887349 --- /dev/null +++ b/helpfiles/FitResultReference.html @@ -0,0 +1,128 @@ + + + + + +FitResult Reference + + + + + + + +
+

FitResult Reference

+

The `FitResult` class stores model fitting outputs generated by `Analysis.RunAnalysisForNeuron` and `Analysis.RunAnalysisForAllNeurons`.

+

This reference page is generated from the canonical runtime class:

+
+ +
+

Related pages:

+ + +
+ + + diff --git a/helpfiles/FitResultReference.m b/helpfiles/FitResultReference.m new file mode 100644 index 0000000..199b682 --- /dev/null +++ b/helpfiles/FitResultReference.m @@ -0,0 +1,13 @@ +%% FitResult Reference +% The `FitResult` class stores model fitting outputs generated by +% `Analysis.RunAnalysisForNeuron` and `Analysis.RunAnalysisForAllNeurons`. +% +% This reference page is generated from the canonical runtime class: +% +% * <../FitResult.m FitResult.m> +% +% Related pages: +% +% * +% * +% * diff --git a/helpfiles/HippocampalPlaceCellExample.html b/helpfiles/HippocampalPlaceCellExample.html index ab1f4ff..f2397d3 100644 --- a/helpfiles/HippocampalPlaceCellExample.html +++ b/helpfiles/HippocampalPlaceCellExample.html @@ -1,13 +1,18 @@ - - - - - HIPPOCAMPAL PLACE CELL - RECEPTIVE FIELD ESTIMATION

HIPPOCAMPAL PLACE CELL - RECEPTIVE FIELD ESTIMATION

Estimation of receptive fields of neurons is a very common data analysis problem in neuroscience. Here we use the nSTAT software to perform an estimation of the receptive fields of hippocampal place cells using a bivariate Gaussian model and Zernike polynomials. The number of zernike polynomials is based on "An Analysis of Hippocampal Spatio-Temporal Representations Using a Bayesian Algorithm for Neural Spike Train Decoding" Barbieri et. al 2005. The data used herein in was provided by Dr. Ricardo Barbieri on 2/28/2011.

Author: Iahn Cajigas

Date: 3/1/2011

Contents

close all
-

Example Data

The x and y coordinates of a freely foraging rat in a circular environment (70cm in diameter and 30cm high walls) and a fixed visual cue. The x and y coordinates at the time when a spike was observed are marked in red. The position coordinates have been normalized to be between -1 and 1 to allow to simplify the analysis.

    load(strcat('PlaceCellDataAnimal1.mat'));
+  
+
+
+
+

HIPPOCAMPAL PLACE CELL - RECEPTIVE FIELD ESTIMATION

+ +

Estimation of receptive fields of neurons is a very common data analysis problem in neuroscience. Here we use the nSTAT software to perform an estimation of the receptive fields of hippocampal place cells using a bivariate Gaussian model and Zernike polynomials. The number of zernike polynomials is based on "An Analysis of Hippocampal Spatio-Temporal Representations Using a Bayesian Algorithm for Neural Spike Train Decoding" Barbieri et. al 2005. The data used herein in was provided by Dr. Ricardo Barbieri on 2/28/2011.

+

+Author: Iahn Cajigas

+

+Date: 3/1/2011

+ +

Contents

+ +
close all
+[~,~,~,~,placeCellDataDir] = getPaperDataDirs();
+
+

Example Data

+

The x and y coordinates of a freely foraging rat in a circular environment (70cm in diameter and 30cm high walls) and a fixed visual cue. The x and y coordinates at the time when a spike was observed are marked in red. The position coordinates have been normalized to be between -1 and 1 to allow to simplify the analysis.

+
    load(fullfile(placeCellDataDir,'PlaceCellDataAnimal1.mat'));
     exampleCell = 25;
     figure(1);
     plot(x,y,'b',neuron{exampleCell}.xN,neuron{exampleCell}.yN,'r.');
     xlabel('x'); ylabel('y');
     title(['Animal#1, Cell#' num2str(exampleCell)]);
-

Analyze All Cells

 numAnimals =2;
+
+

Analyze All Cells

+
 numAnimals =2;
 for n=1:numAnimals
     % load the data
     clear x y neuron time nst tc tcc z;
-    load(strcat('PlaceCellDataAnimal',num2str(n),'.mat'));
+    load(fullfile(placeCellDataDir,['PlaceCellDataAnimal' num2str(n) '.mat']));
 
     % Create the spikeTrains for each cell
     for i=1:length(neuron)
@@ -139,13 +181,20 @@
 %     filename = ['PlaceCellAnimal' num2str(n) 'Results'];
 %     save(filename,'resStruct');
 end
-

View Summary Statistics

Note the Zernike Polynomials yield better fits in terms of decreased KS Statistics (less deviation from the 45 degree line), reduced AIC and reduced BIC across the majority of cells and for both animals

for n=1:numAnimals
-    resData=load(strcat('PlaceCellAnimal',num2str(n),'Results.mat'));
+
+

View Summary Statistics

+

Note the Zernike Polynomials yield better fits in terms of decreased KS Statistics (less deviation from the 45 degree line), reduced AIC and reduced BIC across the majority of cells and for both animals

+
+for n=1:numAnimals
+    resData=load(fullfile(fileparts(placeCellDataDir),['PlaceCellAnimal' num2str(n) 'Results.mat']));
     results = FitResult.fromStructure(resData.resStruct);
     Summary = FitResSummary(results);
     Summary.plotSummary;
 end
-

Visualize the results

% Define a grid
+
+

Visualize the results

+
+% Define a grid
 [x_new,y_new]=meshgrid(-1:.01:1); %define new x and y
 y_new = flipud(y_new); x_new = fliplr(x_new);
 [theta_new,r_new] = cart2pol(x_new,y_new);
@@ -177,8 +226,8 @@
 for n=1:numAnimals
 
     clear lambdaGaussian lambdaZernike;
-    load(strcat('PlaceCellDataAnimal',num2str(n),'.mat'));
-    resData=load(strcat('PlaceCellAnimal',num2str(n),'Results.mat'));
+    load(fullfile(placeCellDataDir,['PlaceCellDataAnimal' num2str(n) '.mat']));
+    resData=load(fullfile(fileparts(placeCellDataDir),['PlaceCellAnimal' num2str(n) 'Results.mat']));
     results = FitResult.fromStructure(resData.resStruct);
 
     for i=1:length(neuron)
@@ -256,8 +305,8 @@
 
 
     clear lambdaGaussian lambdaZernike;
-    load(strcat('PlaceCellDataAnimal1.mat'));
-    resData=load(strcat('PlaceCellAnimal1Results.mat'));
+    load(fullfile(placeCellDataDir,'PlaceCellDataAnimal1.mat'));
+    resData=load(fullfile(fileparts(placeCellDataDir),'PlaceCellAnimal1Results.mat'));
     results = FitResult.fromStructure(resData.resStruct);
 
     for i=1:length(neuron)
@@ -288,7 +337,14 @@
     axis tight square;
     xlabel('x position'); ylabel('y position');
     title(['Animal#1, Cell#' num2str(exampleCell)]);
-
\ No newline at end of file +--> + + diff --git a/helpfiles/HippocampalPlaceCellExample.m b/helpfiles/HippocampalPlaceCellExample.m index 91fd04a..598f19b 100644 --- a/helpfiles/HippocampalPlaceCellExample.m +++ b/helpfiles/HippocampalPlaceCellExample.m @@ -9,15 +9,16 @@ % *Author*: Iahn Cajigas % % *Date*: 3/1/2011 -%% -close all - -%% Example Data -% The x and y coordinates of a freely foraging rat in a circular environment (70cm in diameter and 30cm high walls) and a fixed visual cue. -% The x and y coordinates at the time when a spike was observed are marked -% in red. The position coordinates have been normalized to be between -1 -% and 1 to allow to simplify the analysis. - load(strcat('PlaceCellDataAnimal1.mat')); +%% +close all +[~,~,~,~,placeCellDataDir] = getPaperDataDirs(); + +%% Example Data +% The x and y coordinates of a freely foraging rat in a circular environment (70cm in diameter and 30cm high walls) and a fixed visual cue. +% The x and y coordinates at the time when a spike was observed are marked +% in red. The position coordinates have been normalized to be between -1 +% and 1 to allow to simplify the analysis. + load(fullfile(placeCellDataDir,'PlaceCellDataAnimal1.mat')); exampleCell = 25; figure(1); plot(x,y,'b',neuron{exampleCell}.xN,neuron{exampleCell}.yN,'r.'); @@ -26,10 +27,10 @@ %% Analyze All Cells numAnimals =2; -for n=1:numAnimals - % load the data - clear x y neuron time nst tc tcc z; - load(strcat('PlaceCellDataAnimal',num2str(n),'.mat')); +for n=1:numAnimals + % load the data + clear x y neuron time nst tc tcc z; + load(fullfile(placeCellDataDir,['PlaceCellDataAnimal' num2str(n) '.mat'])); % Create the spikeTrains for each cell for i=1:length(neuron) @@ -97,8 +98,8 @@ % Note the Zernike Polynomials yield better fits in terms of decreased KS % Statistics (less deviation from the 45 degree line), reduced AIC and % reduced BIC across the majority of cells and for both animals -for n=1:numAnimals - resData=load(strcat('PlaceCellAnimal',num2str(n),'Results.mat')); +for n=1:numAnimals + resData=load(fullfile(fileparts(placeCellDataDir),['PlaceCellAnimal' num2str(n) 'Results.mat'])); results = FitResult.fromStructure(resData.resStruct); Summary = FitResSummary(results); Summary.plotSummary; @@ -135,12 +136,12 @@ -for n=1:numAnimals - - clear lambdaGaussian lambdaZernike; - load(strcat('PlaceCellDataAnimal',num2str(n),'.mat')); - resData=load(strcat('PlaceCellAnimal',num2str(n),'Results.mat')); - results = FitResult.fromStructure(resData.resStruct); +for n=1:numAnimals + + clear lambdaGaussian lambdaZernike; + load(fullfile(placeCellDataDir,['PlaceCellDataAnimal' num2str(n) '.mat'])); + resData=load(fullfile(fileparts(placeCellDataDir),['PlaceCellAnimal' num2str(n) 'Results.mat'])); + results = FitResult.fromStructure(resData.resStruct); for i=1:length(neuron) % Evaluate our fits using the new data and the estimated parameters @@ -217,8 +218,8 @@ clear lambdaGaussian lambdaZernike; - load(strcat('PlaceCellDataAnimal1.mat')); - resData=load(strcat('PlaceCellAnimal1Results.mat')); + load(fullfile(placeCellDataDir,'PlaceCellDataAnimal1.mat')); + resData=load(fullfile(fileparts(placeCellDataDir),'PlaceCellAnimal1Results.mat')); results = FitResult.fromStructure(resData.resStruct); for i=1:length(neuron) diff --git a/helpfiles/HippocampalPlaceCellExample.png b/helpfiles/HippocampalPlaceCellExample.png index 9294edd..48376b1 100644 Binary files a/helpfiles/HippocampalPlaceCellExample.png and b/helpfiles/HippocampalPlaceCellExample.png differ diff --git a/helpfiles/HippocampalPlaceCellExample_01.png b/helpfiles/HippocampalPlaceCellExample_01.png index 0039f3c..8c03d56 100644 Binary files a/helpfiles/HippocampalPlaceCellExample_01.png and b/helpfiles/HippocampalPlaceCellExample_01.png differ diff --git a/helpfiles/HippocampalPlaceCellExample_02.png b/helpfiles/HippocampalPlaceCellExample_02.png index c487eeb..5fbb2a4 100644 Binary files a/helpfiles/HippocampalPlaceCellExample_02.png and b/helpfiles/HippocampalPlaceCellExample_02.png differ diff --git a/helpfiles/HippocampalPlaceCellExample_03.png b/helpfiles/HippocampalPlaceCellExample_03.png index 85946da..0f310ad 100644 Binary files a/helpfiles/HippocampalPlaceCellExample_03.png and b/helpfiles/HippocampalPlaceCellExample_03.png differ diff --git a/helpfiles/HippocampalPlaceCellExample_04.png b/helpfiles/HippocampalPlaceCellExample_04.png index 85409db..a8b5118 100644 Binary files a/helpfiles/HippocampalPlaceCellExample_04.png and b/helpfiles/HippocampalPlaceCellExample_04.png differ diff --git a/helpfiles/HippocampalPlaceCellExample_05.png b/helpfiles/HippocampalPlaceCellExample_05.png index cd48a6e..38ba80e 100644 Binary files a/helpfiles/HippocampalPlaceCellExample_05.png and b/helpfiles/HippocampalPlaceCellExample_05.png differ diff --git a/helpfiles/HippocampalPlaceCellExample_06.png b/helpfiles/HippocampalPlaceCellExample_06.png index 8aff8d2..b295619 100644 Binary files a/helpfiles/HippocampalPlaceCellExample_06.png and b/helpfiles/HippocampalPlaceCellExample_06.png differ diff --git a/helpfiles/HippocampalPlaceCellExample_07.png b/helpfiles/HippocampalPlaceCellExample_07.png index e21f25a..f1a40a2 100644 Binary files a/helpfiles/HippocampalPlaceCellExample_07.png and b/helpfiles/HippocampalPlaceCellExample_07.png differ diff --git a/helpfiles/HippocampalPlaceCellExample_08.png b/helpfiles/HippocampalPlaceCellExample_08.png index b9d7a7a..3130984 100644 Binary files a/helpfiles/HippocampalPlaceCellExample_08.png and b/helpfiles/HippocampalPlaceCellExample_08.png differ diff --git a/helpfiles/HippocampalPlaceCellExample_09.png b/helpfiles/HippocampalPlaceCellExample_09.png index 7a8f075..524efb3 100644 Binary files a/helpfiles/HippocampalPlaceCellExample_09.png and b/helpfiles/HippocampalPlaceCellExample_09.png differ diff --git a/helpfiles/HippocampalPlaceCellExample_10.png b/helpfiles/HippocampalPlaceCellExample_10.png new file mode 100644 index 0000000..73f9828 Binary files /dev/null and b/helpfiles/HippocampalPlaceCellExample_10.png differ diff --git a/helpfiles/HippocampalPlaceCellExample_11.png b/helpfiles/HippocampalPlaceCellExample_11.png new file mode 100644 index 0000000..134cbd4 Binary files /dev/null and b/helpfiles/HippocampalPlaceCellExample_11.png differ diff --git a/helpfiles/HistoryExamples.html b/helpfiles/HistoryExamples.html index c765d87..9e83c04 100644 --- a/helpfiles/HistoryExamples.html +++ b/helpfiles/HistoryExamples.html @@ -1,13 +1,18 @@ - - - - - HistoryExamples

Contents

Test History

Generete a nspikeTrain and define a set of history windows of interest. We desire windows from 1-2ms, 2-3ms, 3-5ms, and 5ms-10ms. The history object with this windows in created below and then the

spikeTimes = sort(rand(1,100))*1;
+  
+
+
+
+

Contents

+ +

Test History

+

Generete a nspikeTrain and define a set of history windows of interest. We desire windows from 1-2ms, 2-3ms, 3-5ms, and 5ms-10ms. The history object with this windows in created below and then the

+
spikeTimes = sort(rand(1,100))*1;
 nst        = nspikeTrain(spikeTimes,'n1',.001);
 windowTimes = [.001 .002 .004];
 h=History(windowTimes);
-

/

The firing activity within each window is computed by calling the computeHistory method on a nspikeTrain, nstColl, or a cell array of nspikeTrains

histn1=h.computeHistory(nst);
+
+

/

+

The firing activity within each window is computed by calling the computeHistory method on a nspikeTrain, nstColl, or a cell array of nspikeTrains

+
histn1=h.computeHistory(nst);
 figure; subplot(3,1,1); h.plot; ylabel('History Windows');
 subplot(3,1,2); histn1.plot;    ylabel('History Covariate for nst');
 figure; nst.plot;               ylabel('Neural Spike Train');
-

Example 2: History covariates for a collection of Neural Spikes (nstColl)

It is possible to compute history covariates for all the nspikeTrains in a nstColl simultaneously.

Generate data and create a nstColl

clear nst;
+
+

Example 2: History covariates for a collection of Neural Spikes (nstColl)

+

It is possible to compute history covariates for all the nspikeTrains in a nstColl simultaneously.

+

Generate data and create a nstColl

+
clear nst;
 for i=1:1
     spikeTimes = sort(rand(1,100))*1;
     nst{i}=nspikeTrain(spikeTimes,'',.001);
@@ -83,9 +117,18 @@
 
 windowTimes = [.001 .002 .01];
 h=History(windowTimes);
-

generate a CovColl (collection of covariates) by applying the computing the history of the entire nstColl

histColl = h.computeHistory(spikeColl);
+
+

generate a CovColl (collection of covariates) by applying the computing the history of the entire nstColl

+
histColl = h.computeHistory(spikeColl);
 figure; histColl.plot;
-
\ No newline at end of file +--> + + diff --git a/helpfiles/HistoryExamples.png b/helpfiles/HistoryExamples.png index 0b01958..ab7a065 100644 Binary files a/helpfiles/HistoryExamples.png and b/helpfiles/HistoryExamples.png differ diff --git a/helpfiles/HistoryExamples_01.png b/helpfiles/HistoryExamples_01.png index bcd3d8f..3ac2014 100644 Binary files a/helpfiles/HistoryExamples_01.png and b/helpfiles/HistoryExamples_01.png differ diff --git a/helpfiles/HistoryExamples_02.png b/helpfiles/HistoryExamples_02.png index 5b185b7..1160c27 100644 Binary files a/helpfiles/HistoryExamples_02.png and b/helpfiles/HistoryExamples_02.png differ diff --git a/helpfiles/HistoryExamples_03.png b/helpfiles/HistoryExamples_03.png index 1d19ebf..848fe9f 100644 Binary files a/helpfiles/HistoryExamples_03.png and b/helpfiles/HistoryExamples_03.png differ diff --git a/helpfiles/HistoryExamples_04.png b/helpfiles/HistoryExamples_04.png new file mode 100644 index 0000000..b7afc30 Binary files /dev/null and b/helpfiles/HistoryExamples_04.png differ diff --git a/helpfiles/HybridFilterExample.html b/helpfiles/HybridFilterExample.html index 53d0e9c..0574846 100644 --- a/helpfiles/HybridFilterExample.html +++ b/helpfiles/HybridFilterExample.html @@ -1,13 +1,18 @@ - - - - - Hybrid Point Process Filter Example

Hybrid Point Process Filter Example

This example is based on an implementation of the Hybrid Point Process filter described in General-purpose filter design for neural prosthetic devices by Srinivasan L, Eden UT, Mitter SK, Brown EN in J Neurophysiol. 2007 Oct, 98(4):2456-75.

Contents

Problem Statement

Suppose that a process of interest can be modeled as consisting of several discrete states where the evolution of the system under each state can be modeled as a linear state space model. The observations of both the state and the continuous dynamics are not direct, but rather observed through how the continuous and discrete states affect the firing of a population of neurons. The goal of the hybrid filter is to estimate both the continuous dynamics and the underlying system state from only the neural population firing (point process observations).

To illustrate the use of this filter, we consider a reaching task. We assume two underlying system states s=1="Not Moving"=NM and s=2="Moving"=M. Under the "Not Moving" the position of the arm remain constant, whereas in the "Moving" state, the position and velocities evolved based on the arm acceleration that is modeled as a gaussian white noise process.

Under both the "Moving" and "Not Moving" states, the arm evolution state vector is

$${\bf{x}} = {[x,y,{v_x},{v_y},{a_x},{a_y}]^T}$$

Generated Simulated Arm Reach

clear all;
+  
+
+
+
+

Hybrid Point Process Filter Example

+ +

This example is based on an implementation of the Hybrid Point Process filter described in General-purpose filter design for neural prosthetic devices by Srinivasan L, Eden UT, Mitter SK, Brown EN in J Neurophysiol. 2007 Oct, 98(4):2456-75.

+ +

Contents

+ +

Problem Statement

+

Suppose that a process of interest can be modeled as consisting of several discrete states where the evolution of the system under each state can be modeled as a linear state space model. The observations of both the state and the continuous dynamics are not direct, but rather observed through how the continuous and discrete states affect the firing of a population of neurons. The goal of the hybrid filter is to estimate both the continuous dynamics and the underlying system state from only the neural population firing (point process observations).

+

To illustrate the use of this filter, we consider a reaching task. We assume two underlying system states s=1="Not Moving"=NM and s=2="Moving"=M. Under the "Not Moving" the position of the arm remain constant, whereas in the "Moving" state, the position and velocities evolved based on the arm acceleration that is modeled as a gaussian white noise process.

+

Under both the "Moving" and "Not Moving" states, the arm evolution state vector is

+

+$${\bf{x}} = {[x,y,{v_x},{v_y},{a_x},{a_y}]^T}$$

+

Generated Simulated Arm Reach

+
clear all;
 close all;
 delta=0.001;
 Tmax=2;
@@ -134,7 +170,9 @@
     end
 
 end
-
%save paperHybridFilterExample time Tmax delta mstate X p_ij ind A Q Px0
+
+
+%save paperHybridFilterExample time Tmax delta mstate X p_ij ind A Q Px0
 load paperHybridFilterExample;
 Q{1}=minCovVal*eye(2,2);
 numCells=40;
@@ -219,7 +257,11 @@
 set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
 
 % close all;
-

Simulate Neural Firing

We simulate a population of neurons that fire in response to the movement velocity (x and y coorinates)

%Use the data to estimate the process noise for the moving case and
+
+

Simulate Neural Firing

+

We simulate a population of neurons that fire in response to the movement velocity (x and y coorinates)

+
+%Use the data to estimate the process noise for the moving case and
 %non-moving case
 
 nonMovingInd = intersect(find(X(5,:)==0),find(X(6,:)==0));
@@ -445,7 +487,14 @@
     hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
     set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
     title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
\ No newline at end of file +--> + + diff --git a/helpfiles/HybridFilterExample.png b/helpfiles/HybridFilterExample.png index b536aaf..675018b 100644 Binary files a/helpfiles/HybridFilterExample.png and b/helpfiles/HybridFilterExample.png differ diff --git a/helpfiles/HybridFilterExample_01.png b/helpfiles/HybridFilterExample_01.png index c4301ca..3c2837c 100644 Binary files a/helpfiles/HybridFilterExample_01.png and b/helpfiles/HybridFilterExample_01.png differ diff --git a/helpfiles/HybridFilterExample_02.png b/helpfiles/HybridFilterExample_02.png index 3e1faab..2c2be6a 100644 Binary files a/helpfiles/HybridFilterExample_02.png and b/helpfiles/HybridFilterExample_02.png differ diff --git a/helpfiles/HybridFilterExample_eq12496553100641641814.png b/helpfiles/HybridFilterExample_eq12496553100641641814.png new file mode 100644 index 0000000..ca060bb Binary files /dev/null and b/helpfiles/HybridFilterExample_eq12496553100641641814.png differ diff --git a/helpfiles/NetworkTutorial.html b/helpfiles/NetworkTutorial.html index 519893b..5ae1b1f 100644 --- a/helpfiles/NetworkTutorial.html +++ b/helpfiles/NetworkTutorial.html @@ -1,13 +1,18 @@ - - - - - NetworkTutorial

Contents

% Author: Iahn Cajigas
+  
+
+
+
+

Contents

+ +
+% Author: Iahn Cajigas
 % Date: 2/10/2014
-

Point Process Network Simulation

In order to understand how the point process GLM framework can be used to estimate the network connectivity within a population of neurons, we simulate a network of 2 neurons.

This block diagram specifies a conditional intensity function of the form

$$lambda_{i} \cdot \Delta = logistic(\mu_{i} + H*\Delta N_{i}[n] +
S*u_{stim}[n] + E*\Delta N_{k}[n]$$

where, $\hbox{\fontsize{14}{16}\selectfont\(logistic(x)=e^{x}/{1+e^{x}}\)}$. Note that * is the convolution opertator.

2 Neuron Network

clear all;
+
+

Point Process Network Simulation

+

In order to understand how the point process GLM framework can be used to estimate the network connectivity within a population of neurons, we simulate a network of 2 neurons.

+

+

+

+

+

This block diagram specifies a conditional intensity function of the form

+

+$$lambda_{i} \cdot \Delta = logistic(\mu_{i} + H*\Delta N_{i}[n] +
S*u_{stim}[n] + E*\Delta N_{k}[n]$$

+

where, $\hbox{\fontsize{14}{16}\selectfont\(logistic(x)=e^{x}/{1+e^{x}}\)}$. Note that * is the convolution opertator.

+

2 Neuron Network

+
clear all;
 close all;
 Ts=.001;            %Sample Time
 tMin=0; tMax=50;    %Simulation duration
 t=tMin:Ts:tMax;
 numNeurons=2;
-

Baseline firing rate of the neurons being modeled

mu{1}=-3;
+
+

Baseline firing rate of the neurons being modeled

+
mu{1}=-3;
 mu{2}=-3;
-

History Effect

Captures how the firing of a neuron at modulates its probability of firing. Captures effects such as the refractory period and bursting. We use the same firing history for both neurons in this example. Note that the firing activity at time n leads to strong inhibition at time n+1 (refractory period) and that this effect becomes smaller over the next two time periods.

$$1*h[n]=-4*\Delta N[n-1]-2*\Delta N[n-2] -1*\Delta N[n-3]$$

Note that the one sample delay in same cell firing is included in the simulink model.

H{1}=tf([-4 -2 -1],[1],Ts,'Variable','z^-1');
+
+

History Effect

+

Captures how the firing of a neuron at modulates its probability of firing. Captures effects such as the refractory period and bursting. We use the same firing history for both neurons in this example. Note that the firing activity at time n leads to strong inhibition at time n+1 (refractory period) and that this effect becomes smaller over the next two time periods.

+

+$$1*h[n]=-4*\Delta N[n-1]-2*\Delta N[n-2] -1*\Delta N[n-3]$$

+

Note that the one sample delay in same cell firing is included in the simulink model.

+
H{1}=tf([-4 -2 -1],[1],Ts,'Variable','z^-1');
 H{2}=tf([-4 -2 -1],[1],Ts,'Variable','z^-1');
-

Stimulus Effect

$$1*s_{1}[n]=1*u_{stim}[n]$$

$$1*s_{2}[n]=-1*u_{stim}[n]$$

Neuron 1 is positively modulated by the stimulus

S{1}=tf([1],1,Ts,'Variable','z^-1');
+
+

Stimulus Effect

+

+$$1*s_{1}[n]=1*u_{stim}[n]$$

+

+$$1*s_{2}[n]=-1*u_{stim}[n]$$

+

Neuron 1 is positively modulated by the stimulus

+
S{1}=tf([1],1,Ts,'Variable','z^-1');
 % Neuron 1 is negatively modulated by the stimulus
 S{2}=tf([-1],1,Ts,'Variable','z^-1');
-

Ensemble Effect

Captures the effect of how neighboring neuron firing modulates the firing of a given neuron.

$$1*e_{1}[n]=1*\Delta N_{2}[n-1]$$

$$1*e_{2}[n]=-4*\Delta N_{1}[n-1]$$

Note that the one sample delay in firing of the neighbor cell is included in the simulink model.

%Neuron 2 firing positively modulates Neuron 1
+
+

Ensemble Effect

+

Captures the effect of how neighboring neuron firing modulates the firing of a given neuron.

+

+$$1*e_{1}[n]=1*\Delta N_{2}[n-1]$$

+

+$$1*e_{2}[n]=-4*\Delta N_{1}[n-1]$$

+

Note that the one sample delay in firing of the neighbor cell is included in the simulink model.

+
+%Neuron 2 firing positively modulates Neuron 1
 E{1}=tf([1],1,Ts,'Variable','z^-1');
 %Neuron 1 firing has strong inhibitory effect on neuron 2.
 E{2}=tf([-4],1,Ts,'Variable','z^-1');
-

Stimulus

We use a simple sine wave here but we may want to explore other types of inputs to see if they affect the recovery of the network parameters.

f=1;                      %Stimulus frequency [Hz]
+
+

Stimulus

+

We use a simple sine wave here but we may want to explore other types of inputs to see if they affect the recovery of the network parameters.

+
f=1;                      %Stimulus frequency [Hz]
 u = sin(2*pi*f*t)';       %Make this neuron modulated by a sine wave
 stim=Covariate(t',u,'Stimulus','time','s','Voltage',{'sin'});
 
@@ -99,7 +185,10 @@
 assignin('base','E2',E{2});
 assignin('base','mu2',mu{2});
 options = simget;
-

Simulate the Network

Uses a binomial model for the conditional intensity function nSTAT supports poisson model too but this simulink model simulates the firing using a binomial model

fitType = 'binomial';
+
+

Simulate the Network

+

Uses a binomial model for the conditional intensity function nSTAT supports poisson model too but this simulink model simulates the firing using a binomial model

+
fitType = 'binomial';
 if(strcmp(fitType,'binomial'))
     Algorithm = 'BNLRCG';
 else
@@ -124,13 +213,11 @@
 figure;
 subplot(2,1,1); sC.plot;    v=axis; axis([0 tMax/10 v(3) v(4)]);
 subplot(2,1,2); stim.plot;  v=axis; axis([0 tMax/10 v(3) v(4)]);
-
Warning: The model 'SimulatedNetwork2' has the 'Configuration Parameters' >
-'Data Import/Export' > 'Signal logging format' parameter set to 'ModelDataLogs'.
-The signal logging save format 'ModelDataLogs' will be removed in a future
-release.  To take advantage of new functionality, update models that use
-'ModelDataLogs' signal logging format to use the 'Dataset' format.  For help
-with resolving this and other upgrade issues, use the Simulink Upgrade Advisor. 
-

GLM Model Fitting Setup

In this section, we create the appropriate structures to fit several GLM models to the data generated above.

% Create a constant covariate representing the mean firing rate $$\mu_{i}$
+
+

GLM Model Fitting Setup

+

In this section, we create the appropriate structures to fit several GLM models to the data generated above.

+
+% Create a constant covariate representing the mean firing rate $$\mu_{i}$
 baseline=Covariate(t',ones(length(t),1),'Baseline','time','s','',{'mu'});
 
 spikeColl = sC; %Use the generated data as our collection of spikes
@@ -138,7 +225,9 @@
 cc=CovColl({stim,baseline});
 trial = Trial(spikeColl,cc); sampleRate = 1/Ts; %Create trial
 % trial.setTrialPartition([0 tMax/2 tMax]);
-

GLM Model Fitting and Results

clear c;
+
+

GLM Model Fitting and Results

+
clear c;
 % We know the history effect goes back 3 lag orders
 selfHist = [0:1:3]*Ts;
 % only have an effect at the 1ms lag. This captures the effect of the
@@ -204,10 +293,19 @@
 imagesc(network1ms,CLIM);
 set(gca,'XTick',index,'YTick',index);
 title('Estimated 1ms');
-
Analyzing Configuration #1: Neuron #1,2
+
+
Analyzing Configuration #1: Neuron #1,2
 Analyzing Configuration #2: Neuron #1,2
 Analyzing Configuration #3: Neuron #1,2
-

Note: by default all neurons are considered to be potential neighbors. If this is not the case, you can call trial.setNeighbors(neighborArray) where neighborArray is a matrix that in the ith row has ones in the columns of those neurons considered to be potential neighbors and zeros otherwise. By default neighborArray has 0 only on the diagonal, so that the ith neuron cannot be its own neighbor, and 1 ones elsewhere.

\ No newline at end of file +--> + + diff --git a/helpfiles/NetworkTutorial.png b/helpfiles/NetworkTutorial.png index 3e7d12c..1afefc0 100644 Binary files a/helpfiles/NetworkTutorial.png and b/helpfiles/NetworkTutorial.png differ diff --git a/helpfiles/NetworkTutorial_01.png b/helpfiles/NetworkTutorial_01.png index 7338c72..180e983 100644 Binary files a/helpfiles/NetworkTutorial_01.png and b/helpfiles/NetworkTutorial_01.png differ diff --git a/helpfiles/NetworkTutorial_02.png b/helpfiles/NetworkTutorial_02.png index 3e7b6ba..9309102 100644 Binary files a/helpfiles/NetworkTutorial_02.png and b/helpfiles/NetworkTutorial_02.png differ diff --git a/helpfiles/NetworkTutorial_03.png b/helpfiles/NetworkTutorial_03.png index 3944075..1d500d2 100644 Binary files a/helpfiles/NetworkTutorial_03.png and b/helpfiles/NetworkTutorial_03.png differ diff --git a/helpfiles/NetworkTutorial_04.png b/helpfiles/NetworkTutorial_04.png index de88841..8e49f4f 100644 Binary files a/helpfiles/NetworkTutorial_04.png and b/helpfiles/NetworkTutorial_04.png differ diff --git a/helpfiles/NetworkTutorial_05.png b/helpfiles/NetworkTutorial_05.png new file mode 100644 index 0000000..d1aa82b Binary files /dev/null and b/helpfiles/NetworkTutorial_05.png differ diff --git a/helpfiles/NetworkTutorial_eq00934746157675957628.png b/helpfiles/NetworkTutorial_eq00934746157675957628.png new file mode 100644 index 0000000..bb05426 Binary files /dev/null and b/helpfiles/NetworkTutorial_eq00934746157675957628.png differ diff --git a/helpfiles/NetworkTutorial_eq01980944866737042252.png b/helpfiles/NetworkTutorial_eq01980944866737042252.png new file mode 100644 index 0000000..4c7421c Binary files /dev/null and b/helpfiles/NetworkTutorial_eq01980944866737042252.png differ diff --git a/helpfiles/NetworkTutorial_eq03377344100068725995.png b/helpfiles/NetworkTutorial_eq03377344100068725995.png new file mode 100644 index 0000000..7a6f552 Binary files /dev/null and b/helpfiles/NetworkTutorial_eq03377344100068725995.png differ diff --git a/helpfiles/NetworkTutorial_eq03916883050122551270.png b/helpfiles/NetworkTutorial_eq03916883050122551270.png new file mode 100644 index 0000000..8b6c41f Binary files /dev/null and b/helpfiles/NetworkTutorial_eq03916883050122551270.png differ diff --git a/helpfiles/NetworkTutorial_eq09130146389578799533.png b/helpfiles/NetworkTutorial_eq09130146389578799533.png new file mode 100644 index 0000000..c7cab13 Binary files /dev/null and b/helpfiles/NetworkTutorial_eq09130146389578799533.png differ diff --git a/helpfiles/NetworkTutorial_eq12373433218180091647.png b/helpfiles/NetworkTutorial_eq12373433218180091647.png new file mode 100644 index 0000000..e311363 Binary files /dev/null and b/helpfiles/NetworkTutorial_eq12373433218180091647.png differ diff --git a/helpfiles/NetworkTutorial_eq13194312176468615019.png b/helpfiles/NetworkTutorial_eq13194312176468615019.png new file mode 100644 index 0000000..b7cd670 Binary files /dev/null and b/helpfiles/NetworkTutorial_eq13194312176468615019.png differ diff --git a/helpfiles/NeuralSpikeAnalysis_top.html b/helpfiles/NeuralSpikeAnalysis_top.html index d29df95..f818b4b 100644 --- a/helpfiles/NeuralSpikeAnalysis_top.html +++ b/helpfiles/NeuralSpikeAnalysis_top.html @@ -9,7 +9,7 @@ Neural Spike Train Analysis Toolbox (nSTAT) - +

General Point Process Simulation

In this demo, we show how sample-paths of a point process (PP) can be generated from specification of its conditional intensity function (CIF). We then use the generated PP data to validate the outputs of the Neural Spike Analysis Toolbox.

Contents

Point Process Sample Path Generation

That both the stimulus effect and ensemble effects can be made into multi-input/multi-output transfer functions to account for more than 1 stimulus effect or multiple neighboring neuron effects. To do this, simply define $$E$ or $$S$ to be a row vector of LTI transfer functions. Make sure than the number of dimensions of the input matches the number of transfer functions specified in the row vector.

This block diagram specifies a conditional intensity function of the form

$$\lambda_{i} \cdot \Delta = exp(\mu_{i} + H*\Delta N_{i}[n] + S*u_{stim}[n] + E*\Delta N_{k}[n])/(1+exp(\mu_{i} + H*\Delta N_{i}[n] + S*u_{stim}[n] + E*\Delta N_{k}[n]))$$

close all;
+      -->
+General Point Process Simulation
+
+
+
+
+
+
+
+
+

General Point Process Simulation

+ +

In this demo, we show how sample-paths of a point process (PP) can be generated from specification of its conditional intensity function (CIF). We then use the generated PP data to validate the outputs of the Neural Spike Analysis Toolbox.

+ +

Contents

+ +

Point Process Sample Path Generation

+

That both the stimulus effect and ensemble effects can be made into multi-input/multi-output transfer functions to account for more than 1 stimulus effect or multiple neighboring neuron effects. To do this, simply define $E$ or $S$ to be a row vector of LTI transfer functions. Make sure than the number of dimensions of the input matches the number of transfer functions specified in the row vector.

+
Error in state of SceneNode.
+String scalar or character vector must have valid interpreter syntax: 
+$E$ or $
+
+

+

+

This block diagram specifies a conditional intensity function of the form

+

+$$\lambda_{i} \cdot \Delta = exp(\mu_{i} + H*\Delta N_{i}[n] + S*u_{stim}[n] + E*\Delta N_{k}[n])/(1+exp(\mu_{i} + H*\Delta N_{i}[n] + S*u_{stim}[n] + E*\Delta N_{k}[n]))$$

+
close all;
 Ts=.001;            %Sample Time
 tMin=0; tMax=50;    %Simulation duration
 t=tMin:Ts:tMax;
 
 mu=-3;              %Baseline firing rate of the neurons being modeled
-

History Effect

$$1*h[n]=-1*\Delta N[n-1]-2*\Delta N[n-2] -4*\Delta N[n-3]$$

H=tf([-1 -2 -4],[1],Ts,'Variable','z^-1');
-

Stimulus Effect

$$1*s[n]=1*u_{stim}[n]$$

S=tf([1],1,Ts,'Variable','z^-1');
-

Ensemble Effect

$$1*e[n]=0*\Delta N_{k}[n]$$

E=tf([0],1,Ts,'Variable','z^-1');
-
f=1;                    %Stimulus frequency
+
+

History Effect

+

+$$1*h[n]=-1*\Delta N[n-1]-2*\Delta N[n-2] -4*\Delta N[n-3]$$

+
H=tf([-1 -2 -4],[1],Ts,'Variable','z^-1');
+
+

Stimulus Effect

+

+$$1*s[n]=1*u_{stim}[n]$$

+
S=tf([1],1,Ts,'Variable','z^-1');
+
+

Ensemble Effect

+

+$$1*e[n]=0*\Delta N_{k}[n]$$

+
E=tf([0],1,Ts,'Variable','z^-1');
+
+
f=1;                    %Stimulus frequency
 u = sin(2*pi*f*t)';       %Make this neuron modulated by a sine wave
 e = zeros(length(t),1);   %No Ensemble input
 
@@ -83,22 +154,36 @@
 figure;
 subplot(2,1,1); sC.plot;    v=axis; axis([0 tMax/10 v(3) v(4)]);
 subplot(2,1,2); stim.plot;  v=axis; axis([0 tMax/10 v(3) v(4)]);
-

GLM Model Fitting Setup

In this section, we create the appropriate structures to fit several GLM models to the data generated above.

% Create a constant covariate representing the mean firing rate $$\mu_{i}$
+
+

GLM Model Fitting Setup

+

In this section, we create the appropriate structures to fit several GLM models to the data generated above.

+
+% Create a constant covariate representing the mean firing rate $$\mu_{i}$
 baseline=Covariate(t',ones(length(t),1),'Baseline','time','s','',{'mu'});
 
 
 spikeColl = sC;               %Use the generated data as our collection of spikes
 cc=CovColl({stim,baseline});  %Use stimulation and baseline as possible covariates
 trial = Trial(spikeColl,cc); sampleRate = 1/Ts; %Create trial
-

GLM Model Fitting and Results

clear c;
+
+

GLM Model Fitting and Results

+
clear c;
 selfHist = [0:0.001:0.003]; %We know the history effect goes back 3 lag orders
-

Fit only a mean firing rate

c{1} = TrialConfig({{'Baseline','mu'}},sampleRate,[],[]);
+
+

Fit only a mean firing rate

+
c{1} = TrialConfig({{'Baseline','mu'}},sampleRate,[],[]);
 c{1}.setName('Baseline');
-

Fit a mean firing rate + the stimulus term

c{2} = TrialConfig({{'Baseline','mu'},{'Stimulus','sin'}},sampleRate,[],[]);
+
+

Fit a mean firing rate + the stimulus term

+
c{2} = TrialConfig({{'Baseline','mu'},{'Stimulus','sin'}},sampleRate,[],[]);
 c{2}.setName('Stim');
-

Fit a mean firing rate, self-history, and stimulus --- Same as true model

c{3} = TrialConfig({{'Baseline','mu'},{'Stimulus','sin'}},sampleRate,selfHist,[]);
+
+

Fit a mean firing rate, self-history, and stimulus --- Same as true model

+
c{3} = TrialConfig({{'Baseline','mu'},{'Stimulus','sin'}},sampleRate,selfHist,[]);
 c{3}.setName('Stim+Hist');
-

Place all configurations together and run analysis for each neuron

cfgColl= ConfigColl(c);
+
+

Place all configurations together and run analysis for each neuron

+
cfgColl= ConfigColl(c);
 if(strcmp(fitType,'binomial'))
     Algorithm = 'BNLRCG';   % BNLRCG - faster Truncated, L-2 Regularized,
                             % Binomial Logistic Regression with Conjugate
@@ -108,14 +193,25 @@
                             % or Poisson CIFs
 end
 results = Analysis.RunAnalysisForAllNeurons(trial,cfgColl,0,Algorithm);
-
Analyzing Configuration #1: Neuron #1,2,3,4,5
+
+
Analyzing Configuration #1: Neuron #1,2,3,4,5
 Analyzing Configuration #2: Neuron #1,2,3,4,5
 Analyzing Configuration #3: Neuron #1,2,3,4,5
-

Results for sample neuron

results{1}.plotResults;
-

Results for across all sample paths

Summary = FitResSummary(results);
+
+

Results for sample neuron

+
results{1}.plotResults;
+
+

Results for across all sample paths

+
Summary = FitResSummary(results);
 Summary.plotSummary;
-
\ No newline at end of file +--> + + diff --git a/helpfiles/PPSimExample.png b/helpfiles/PPSimExample.png index ba45854..9e7d096 100644 Binary files a/helpfiles/PPSimExample.png and b/helpfiles/PPSimExample.png differ diff --git a/helpfiles/PPSimExample_01.png b/helpfiles/PPSimExample_01.png index 0979ba4..70851e4 100644 Binary files a/helpfiles/PPSimExample_01.png and b/helpfiles/PPSimExample_01.png differ diff --git a/helpfiles/PPSimExample_02.png b/helpfiles/PPSimExample_02.png index c98eaf5..26d9c51 100644 Binary files a/helpfiles/PPSimExample_02.png and b/helpfiles/PPSimExample_02.png differ diff --git a/helpfiles/PPSimExample_03.png b/helpfiles/PPSimExample_03.png index 60b7801..375b1c6 100644 Binary files a/helpfiles/PPSimExample_03.png and b/helpfiles/PPSimExample_03.png differ diff --git a/helpfiles/PPSimExample_04.png b/helpfiles/PPSimExample_04.png new file mode 100644 index 0000000..26facc3 Binary files /dev/null and b/helpfiles/PPSimExample_04.png differ diff --git a/helpfiles/PPSimExample_eq06096954086272423927.png b/helpfiles/PPSimExample_eq06096954086272423927.png new file mode 100644 index 0000000..13743e3 Binary files /dev/null and b/helpfiles/PPSimExample_eq06096954086272423927.png differ diff --git a/helpfiles/PPSimExample_eq13375943156936646927.png b/helpfiles/PPSimExample_eq13375943156936646927.png new file mode 100644 index 0000000..f0e2e08 Binary files /dev/null and b/helpfiles/PPSimExample_eq13375943156936646927.png differ diff --git a/helpfiles/PPSimExample_eq16379821955407226942.png b/helpfiles/PPSimExample_eq16379821955407226942.png new file mode 100644 index 0000000..87b3b86 Binary files /dev/null and b/helpfiles/PPSimExample_eq16379821955407226942.png differ diff --git a/helpfiles/PPSimExample_eq17733795504284027482.png b/helpfiles/PPSimExample_eq17733795504284027482.png new file mode 100644 index 0000000..77c6556 Binary files /dev/null and b/helpfiles/PPSimExample_eq17733795504284027482.png differ diff --git a/helpfiles/PPThinning.html b/helpfiles/PPThinning.html index 5a15c1c..885c890 100644 --- a/helpfiles/PPThinning.html +++ b/helpfiles/PPThinning.html @@ -1,13 +1,18 @@ - - - - - Simulate PP via thinning

Simulate PP via thinning

Given a conditional intensity function, we generate a point process consistent with this CIF.

Contents

Basic Example

close all;
+  
+
+
+
+

Simulate PP via thinning

+ +

Given a conditional intensity function, we generate a point process consistent with this CIF.

+ +

Contents

+ +

Basic Example

+
close all;
 delta = 0.001;
 Tmax = 100;
 time = 0:delta:Tmax;
@@ -91,7 +121,9 @@
 
 % keep spike if lambda ratio is greater than random number
 tSpikesThin  = tSpikes(lambdaRatio>=u2);
-

Compare Constant rate process vs. thinned process

figure(1);
+
+

Compare Constant rate process vs. thinned process

+
figure(1);
 n1 = nspikeTrain(tSpikes);
 n2 = nspikeTrain(tSpikesThin);
 subplot(2,2,1); n1.plot; plot(tSpikes,ones(size(tSpikes)),'.');
@@ -107,14 +139,28 @@
 scaledProb.plot;
 v=axis;
 axis([0 Tmax/4 v(3) v(4)]);
-

Simulate multiple realizations of a point process via thinning

The CIF class can generated realizations of a point process given a conditional intensity function (defined as a Covariate or SignalObj)

numRealizations = 20;
+
+

Simulate multiple realizations of a point process via thinning

+

The CIF class can generated realizations of a point process given a conditional intensity function (defined as a Covariate or SignalObj)

+
numRealizations = 20;
 spikeColl = CIF.simulateCIFByThinningFromLambda(lambda,numRealizations);
 figure(3);
 spikeColl.plot;
 lambda.plot;
 v=axis;
 axis([0 Tmax/4 v(3) v(4)]);
-
\ No newline at end of file +--> + + diff --git a/helpfiles/PPThinning.png b/helpfiles/PPThinning.png index 611d13f..0c946c9 100644 Binary files a/helpfiles/PPThinning.png and b/helpfiles/PPThinning.png differ diff --git a/helpfiles/PPThinning_01.png b/helpfiles/PPThinning_01.png index 5ae1cf5..d79dc8d 100644 Binary files a/helpfiles/PPThinning_01.png and b/helpfiles/PPThinning_01.png differ diff --git a/helpfiles/PPThinning_02.png b/helpfiles/PPThinning_02.png index 65d50ec..8c5584c 100644 Binary files a/helpfiles/PPThinning_02.png and b/helpfiles/PPThinning_02.png differ diff --git a/helpfiles/PPThinning_03.png b/helpfiles/PPThinning_03.png index 7e59326..928b586 100644 Binary files a/helpfiles/PPThinning_03.png and b/helpfiles/PPThinning_03.png differ diff --git a/helpfiles/PPThinning_04.png b/helpfiles/PPThinning_04.png new file mode 100644 index 0000000..63ccfc4 Binary files /dev/null and b/helpfiles/PPThinning_04.png differ diff --git a/helpfiles/PSTHEstimation.html b/helpfiles/PSTHEstimation.html index 871e415..705b398 100644 --- a/helpfiles/PSTHEstimation.html +++ b/helpfiles/PSTHEstimation.html @@ -1,68 +1,99 @@ - - - - - PSTH Estimation

PSTH Estimation

We illustrate two ways to estimate a peristimulus time histogram using the nSTAT toolbox. One technique is the standard binning in time, averaging across trials, and dividing by the binwidth to estimate the spike rate and the other is based on the method presented in "Analysis of Between-Trial and Within-Trial Neural Spiking Dynamics" by Czanner et al in J Neurophysiology 2008.

Contents

Generate a known Conditional Intensity Function

We generated a known conditional intensity function (rate function) and generate distinct realizations of point processes consistent with this rate function. We use the method of thinning to simulate a point process.

close all;
+      -->
+PSTH Estimation
+
+
+
+
+
+
+
+
+

PSTH Estimation

+ +

We illustrate two ways to estimate a peristimulus time histogram using the nSTAT toolbox. One technique is the standard binning in time, averaging across trials, and dividing by the binwidth to estimate the spike rate and the other is based on the method presented in "Analysis of Between-Trial and Within-Trial Neural Spiking Dynamics" by Czanner et al in J Neurophysiology 2008.

+ +

Contents

+ +

Generate a known Conditional Intensity Function

+

We generated a known conditional intensity function (rate function) and generate distinct realizations of point processes consistent with this rate function. We use the method of thinning to simulate a point process.

+
close all;
 delta = 0.001;
 Tmax = 10;
 time = 0:delta:Tmax;
@@ -73,26 +104,45 @@
 spikeColl = CIF.simulateCIFByThinningFromLambda(lambda,numRealizations);
 spikeColl.plot; set(gca,'ytickLabel',[]);
 lambda.plot;
-

Estimate the PSTH with 500ms windows

figure;
+
+

Estimate the PSTH with 500ms windows

+
figure;
 binsize = .5; %500ms window
 psth    = spikeColl.psth(binsize);
 psthGLM = spikeColl.psthGLM(binsize);
-true = lambda; %rate*delta = expected number of arrivals per bin
-h1=true.plot;
+trueRate = lambda; %rate*delta = expected number of arrivals per bin
+h1=trueRate.plot;
 h3=psthGLM.plot([],{{' ''k'',''Linewidth'',4'}});
 h2=psth.plot([],{{' ''rx'',''Linewidth'',4'}});
 legend off;
 legend([h1(1) h2(1)  h3(1)],'true','PSTH','PSTH_{glm}');
 
+% Scalar summaries for automated parity checks.
+psth_mean_hz = mean(psth.data);
+psth_glm_mean_hz = mean(psthGLM.data);
+lambda_mean_hz = mean(lambda.data);
+parity = struct();
+parity.psth_mean_hz = psth_mean_hz;
+parity.psth_glm_mean_hz = psth_glm_mean_hz;
+parity.lambda_mean_hz = lambda_mean_hz;
+
 % Because currently the psthGLM estimated the psth coefficients in each bin
 % for each realization, we want the show the mean and standard error of the
 % cofficient in each bin. We make the upper and lower confidence bounds
 % equal to 1/sqrt(numRealization)=1/sqrt(psth.dimension) to view the
 % standard error instead of the standard deviation
-
Running in batch mode: neurons with same name are fit simultaneously
+
+
Running in batch mode: neurons with same name are fit simultaneously
 Analyzing Configuration #1: Neuron #1
-

Note the mean of the PSTH estimated via the GLM model and the PSTH computed via standard methods agree precisely. The benefit of the GLM estimated PSTH is the presence of confidence bounds on the estimate. Both the standard and GLM PSTH are in close agreement with the "true" underlying rate function (conditional intensity function) used in this simulated example. Both the PSTH and PSTHGLM code could be updated in the future to allow for variable bin sizes (e.g. in the vein of Baysian Adaptive Regression Splines by Wallstrom, Leibner and Kass). Alternatively, porting of BARS to Matlab may allow for it to be easily integrated into the nSTAT toolbox.

\ No newline at end of file +--> + + diff --git a/helpfiles/PSTHEstimation.png b/helpfiles/PSTHEstimation.png index 5c0d575..ac895d8 100644 Binary files a/helpfiles/PSTHEstimation.png and b/helpfiles/PSTHEstimation.png differ diff --git a/helpfiles/PSTHEstimation_01.png b/helpfiles/PSTHEstimation_01.png index 0c4cff5..1ccb21b 100644 Binary files a/helpfiles/PSTHEstimation_01.png and b/helpfiles/PSTHEstimation_01.png differ diff --git a/helpfiles/PSTHEstimation_02.png b/helpfiles/PSTHEstimation_02.png index e9acde6..ef76025 100644 Binary files a/helpfiles/PSTHEstimation_02.png and b/helpfiles/PSTHEstimation_02.png differ diff --git a/helpfiles/PaperOverview.html b/helpfiles/PaperOverview.html new file mode 100644 index 0000000..4118902 --- /dev/null +++ b/helpfiles/PaperOverview.html @@ -0,0 +1,284 @@ + + + + + +Paper-Aligned Toolbox Map + + + + + + + +
+

Paper-Aligned Toolbox Map

+ +

This page aligns the nSTAT toolbox documentation with the original toolbox paper:

+
+
    +
  • Cajigas I, Malik WQ, Brown EN. nSTAT: Open-source neural spike train analysis toolbox for Matlab. Journal of Neuroscience Methods 211: 245-264 (2012).
  • +
  • DOI: 10.1016/j.jneumeth.2012.08.009
  • +
  • PMID: 22981419
  • +
+
+

Full text links:

+
+ +
+ +

Contents

+ +

Class Hierarchy and Object Model

+

nSTAT is organized around reusable signal and trial abstractions.

+
+
    +
  • Signal and covariate primitives: `SignalObj`, `Covariate`, `ConfidenceInterval`, `CovColl`
  • +
  • Spiking data structures: `nspikeTrain`, `nstColl`, `History`, `Events`
  • +
  • Experiment and configuration objects: `Trial`, `TrialConfig`, `ConfigColl`
  • +
  • Modeling and inference objects: `CIF`, `Analysis`, `FitResult`, `FitResSummary`, `DecodingAlgorithms`
  • +
+
+

Class references and examples:

+ +

Fitting and Assessment Workflow

+

The paper's core workflow fits point-process GLMs and evaluates fit quality.

+

1. Build trial data with `Trial`, `CovColl`, and `nstColl`. 2. Define candidate models with `TrialConfig` and `ConfigColl`. 3. Fit models with `Analysis.RunAnalysisForNeuron` or `Analysis.RunAnalysisForAllNeurons`. 4. Assess goodness-of-fit using `FitResult` diagnostics (KS, residuals, confidence bands) and summarize across neurons with `FitResSummary`.

+

Related examples:

+ +

Simulation Workflow

+

The toolbox supports simulation of point-process and related neural models.

+
+ +
+

Decoding Workflow

+

nSTAT includes point-process and Gaussian-state decoding algorithms that are described in the paper's adaptive filtering sections.

+
+ +
+

Example-to-Paper Section Mapping

+

The examples below correspond directly to the paper's representative workflows.

+
+ +
+ +
+ + + diff --git a/helpfiles/PaperOverview.m b/helpfiles/PaperOverview.m new file mode 100644 index 0000000..b6ff6ff --- /dev/null +++ b/helpfiles/PaperOverview.m @@ -0,0 +1,79 @@ +%% Paper-Aligned Toolbox Map +% This page aligns the nSTAT toolbox documentation with the original toolbox +% paper: +% +% * Cajigas I, Malik WQ, Brown EN. nSTAT: Open-source neural spike train +% analysis toolbox for Matlab. Journal of Neuroscience Methods 211: +% 245-264 (2012). +% * DOI: 10.1016/j.jneumeth.2012.08.009 +% * PMID: 22981419 +% +% Full text links: +% +% * +% * +% +%% Class Hierarchy and Object Model +% nSTAT is organized around reusable signal and trial abstractions. +% +% * Signal and covariate primitives: `SignalObj`, `Covariate`, +% `ConfidenceInterval`, `CovColl` +% * Spiking data structures: `nspikeTrain`, `nstColl`, `History`, `Events` +% * Experiment and configuration objects: `Trial`, `TrialConfig`, `ConfigColl` +% * Modeling and inference objects: `CIF`, `Analysis`, `FitResult`, +% `FitResSummary`, `DecodingAlgorithms` +% +% Class references and examples: +% +% * +% * +% +%% Fitting and Assessment Workflow +% The paper's core workflow fits point-process GLMs and evaluates fit quality. +% +% 1. Build trial data with `Trial`, `CovColl`, and `nstColl`. +% 2. Define candidate models with `TrialConfig` and `ConfigColl`. +% 3. Fit models with `Analysis.RunAnalysisForNeuron` or +% `Analysis.RunAnalysisForAllNeurons`. +% 4. Assess goodness-of-fit using `FitResult` diagnostics (KS, residuals, +% confidence bands) and summarize across neurons with `FitResSummary`. +% +% Related examples: +% +% * +% * +% * +% +%% Simulation Workflow +% The toolbox supports simulation of point-process and related neural models. +% +% * Conditional intensity specification and simulation: `CIF` +% * Thinning-based point-process simulation: +% +% * End-to-end simulated analysis: +% +% +%% Decoding Workflow +% nSTAT includes point-process and Gaussian-state decoding algorithms that are +% described in the paper's adaptive filtering sections. +% +% * Static decoding methods: `DecodingAlgorithms` +% * Example workflows: +% , +% , and +% +% +%% Example-to-Paper Section Mapping +% The examples below correspond directly to the paper's representative +% workflows. +% +% * and +% : model-based event process analysis +% * and +% : +% stimulus-response and receptive field modeling +% * , +% , and +% : decoding and state estimation +% * : consolidated reproduction +% workflow for paper analyses diff --git a/helpfiles/README.md b/helpfiles/README.md new file mode 100644 index 0000000..b5c8a3a --- /dev/null +++ b/helpfiles/README.md @@ -0,0 +1,9 @@ +# nSTAT Rendered Help (GitHub Pages) + +HTML files in GitHub repository view open as source. Use the GitHub Pages +site below to open rendered documentation: + +- Help home: https://cajigaslab.github.io/nSTAT/ +- Toolbox top page: https://cajigaslab.github.io/nSTAT/NeuralSpikeAnalysis_top.html +- Paper examples: https://cajigaslab.github.io/nSTAT/nSTATPaperExamples.html + diff --git a/helpfiles/SignalObj.html b/helpfiles/SignalObj.html index 3a56fe6..6297ed0 100644 --- a/helpfiles/SignalObj.html +++ b/helpfiles/SignalObj.html @@ -1,134 +1,161 @@ - - + - - - - - SignalObj - - - - -
classdef SignalObj < handle
-    %SignalObj Class representing a signal abstraction
-    %   SignalObj consist of data that is indexed by time (as a default). The
-    %   indexing variable can be any other type of data and the x-axis labels
-    %   modified to represent this change.
-    %
-    %   A SignalObj can be multivariate in that the data can have more than one component. The
-    %   sample rate of the SignalObj is determined by the time increment used
-    %   in the time sequence used when the SingalObj is created
-    %
-    %   Usage:
-    %   >> s=SignalObj(time, data, name, xlabelval, xunits, yunits, dataLabels, plotProps)
-    %
-    %   Only time and data need to be specified. Other arguments are optional.
-    %
-    %
-    %  time: indexing variable for the data. n x 1 or 1 x n array. The sample
-    %        rate is determined by the time increment between samples of this vector. Units of
-    %        [sec] are assumed, but need not be used. If the time vector is
-    %        in units of [sec], the sampleRate is in units of [Hz]. If the
-    %        time vector is in units of [msec] then the sampleRate is in
-    %        units of [1/msec] or 10^3 [Hz].
-    %
-    %  data: n x m or m x n array reprenting the signal at each index of the time vector.
-    %        The dimension that is compatible with the time vector will be automatically detected.
-    %        Thus a SignalObj can be created by either passing the data matrix or its transpose. The remaining
-    %        dimension will determine the dimensionality of the SignalObj.
-    %
-    %  name: string that determines the name of the signal. This is used to
-    %        label the y-axis of the SignalObj.
-    %
-    %  xlabelval: A string specifying the name of the indexing variable. If
-    %        this value is not specified, 'time' is used.
-    %
-    %  xunits: A string specifying the name of the units of the indexing
-    %          variable. In not specified, 'sec' is used.
-    %
-    %  yunits: A string specifying the units of the SignalObj. Used when plotting the SignalObj.
-    %
-    %  dataLabels: If data is multivariate, the names of the components of the SignalObj can be specified.
-    %              These can be used to reference specific data within the
-    %              signal (e.g. the x-component of a 3-d vector) and are
-    %              also used for plotting. SignalObj's will be created for
-    %              each component of the orignal SignalObj under the
-    %              vars field. Can be specified all at once or by a cell of
-    %              strings.
-    %
-    %  plotProps:  Can be specified for each component of the SignalObj
-    %              individually or by a cell of string of same dimension as the
-    %              number of components in the data.
-    %
-    %
-    %
-    % <a href="SignalObjExamples.html">SignalObj Examples</a>
+SignalObj
+
+
+
+
+
+
+
+
+
+classdef SignalObj < handle
+%SIGNALOBJ Class representing a signal abstraction
+%   SignalObj consist of data that is indexed by time (as a default). The
+%   indexing variable can be any other type of data and the x-axis labels
+%   modified to represent this change.
+%
+%   A SignalObj can be multivariate in that the data can have more than one component. The
+%   sample rate of the SignalObj is determined by the time increment used
+%   in the time sequence used when the SingalObj is created
+%
+%   Usage:
+%   >> s=SignalObj(time, data, name, xlabelval, xunits, yunits, dataLabels, plotProps)
+%
+%   Only time and data need to be specified. Other arguments are optional.
+%
+%
+%  time: indexing variable for the data. n x 1 or 1 x n array. The sample
+%        rate is determined by the time increment between samples of this vector. Units of
+%        [sec] are assumed, but need not be used. If the time vector is
+%        in units of [sec], the sampleRate is in units of [Hz]. If the
+%        time vector is in units of [msec] then the sampleRate is in
+%        units of [1/msec] or 10^3 [Hz].
+%
+%  data: n x m or m x n array reprenting the signal at each index of the time vector.
+%        The dimension that is compatible with the time vector will be automatically detected.
+%        Thus a SignalObj can be created by either passing the data matrix or its transpose. The remaining
+%        dimension will determine the dimensionality of the SignalObj.
+%
+%  name: string that determines the name of the signal. This is used to
+%        label the y-axis of the SignalObj.
+%
+%  xlabelval: A string specifying the name of the indexing variable. If
+%        this value is not specified, 'time' is used.
+%
+%  xunits: A string specifying the name of the units of the indexing
+%          variable. In not specified, 'sec' is used.
+%
+%  yunits: A string specifying the units of the SignalObj. Used when plotting the SignalObj.
+%
+%  dataLabels: If data is multivariate, the names of the components of the SignalObj can be specified.
+%              These can be used to reference specific data within the
+%              signal (e.g. the x-component of a 3-d vector) and are
+%              also used for plotting. SignalObj's will be created for
+%              each component of the orignal SignalObj under the
+%              vars field. Can be specified all at once or by a cell of
+%              strings.
+%
+%  plotProps:  Can be specified for each component of the SignalObj
+%              individually or by a cell of string of same dimension as the
+%              number of components in the data.
+%
+%
+% <a href="matlab: methods('SignalObj')">methods</a>
+% <a href="matlab:nstatOpenHelpPage('SignalObjExamples.html')">SignalObj Examples</a>
+%
+% Reference page in Help browser
+% <a href="matlab:nstatOpenHelpPage('SignalObj.html')">SignalObj Reference</a>
+
+
+%
+% nSTAT v1 Copyright (C) 2012 Masschusetts Institute of Technology
+% Cajigas, I, Malik, WQ, Brown, EN
+% This program is free software; you can redistribute it and/or
+% modify it under the terms of the GNU General Public License as published
+% by the Free Software Foundation; either version 2 of the License, or
+% (at your option) any later version.
+%
+% This program is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+% See the GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with this program; if not, write to the Free Software Foundation,
+% Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
     properties (SetAccess = private)
         name       % name of the SignalObj
-        time       % time vector
+        time       % time vector. Time increment determines sampleRate
         data       % actual SignalObj data
         dimension  % number of different components of the SignalObj
         minTime    % minimum Time value of the SignalObj
@@ -146,12 +173,12 @@
         originalTime %original timeVector
         originalData %original Data
     end
-    properties (Dependent = true)
-        vars %Contains subfields of the same names as the dataLabels that contain Signals with only the data corresponding to that label.
-    end
+%     properties (Dependent = true)
+%         vars %Contains subfields of the same names as the dataLabels that contain Signals with only the data corresponding to that label.
+%     end
     methods
         %Constructor
-        function s=SignalObj(time, data, name, xlabelval, xunits, yunits, dataLabels, plotProps)
+         function s=SignalObj(time, data, name, xlabelval, xunits, yunits, dataLabels, plotProps)
 
             if(nargin<6)
                 yunits='';
@@ -166,7 +193,7 @@
                 name='';
             end
             [l,w]=size(time);
-            if(l>=w);
+            if(l>=w)
                 if(w>1)
                     error('Time vector can only have one dimension');
                 else
@@ -185,7 +212,7 @@
            if(l==length(s.time));
                 s.data=data;
                 s.dimension =w;
-            elseif(w==length(s.time));
+            elseif(w==length(s.time))
                 s.data=data';
                 s.dimension=l;
            else
@@ -193,15 +220,25 @@
            end
            s.originalData = s.data;
             if(nargin <7)
-                for i=1:s.dimension
-                    dataLabels{i} = '';
+                if(s.dimension==0)
+                    dataLabels ='';
+                else
+                    for i=1:s.dimension
+                        dataLabels{i} = '';
+                    end
                 end
             end
             s.dataMask  = ones(1,s.dimension);
             if(nargin<8)
                 plotProps = cell(s.dimension,1);
             end
-            s.sampleRate = 1/mean(diff(s.time));
+            deltaT=mean(diff(s.time));
+            if(~isfinite(deltaT) || deltaT<=0) %diff not well defined
+                deltaT=0.001;
+            end
+            % Keep sample-rate estimation stable without rounding large
+            % deltaT values to zero (which yields Inf sample rates).
+            s.sampleRate = 1/deltaT;
             s.origSampleRate = s.sampleRate;
             s.name=name;
             s.xlabelval=xlabelval;
@@ -216,7 +253,9 @@
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         %Set functions
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-        function setName(sObj,name) %set the name after construction
+        function setName(sObj,name)
+            % setName(sObj,name)
+            % set the name after construction
             if(isa(name,'char'))
                 sObj.name = name;
             else
@@ -224,12 +263,23 @@
             end
         end
         function setXlabel(sObj,name)
+            %setXlabel(sObj,name)
+            %set the x-axis label to string name
             sObj.xlabelval = name;
         end
         function setYLabel(sObj,name)
+            %setYLabel(sObj,name)
+            %set the ylabel to string name;
+            %Same as calling setName(sObj,name);
             sObj.setName(name);
         end
         function setUnits(sObj, xUnits, yUnits)
+            %setUnits(sObj, xUnits, yUnits)
+            %Set the axis units.
+            %Same as calling sObj.setXUnits(xUnits) and
+            %sObj.setYUnits(yUnits) separately.
+            %yUnits is optional argument. If it is not specified, the this
+            %function behaves like setXUnits.
             if(nargin==3)
                 if(isa(yUnits,'char'))
                     sObj.setYunits(yUnits);
@@ -242,48 +292,82 @@
             end
         end
         function setXUnits(sObj, units)
+            %setXUnits(sObj, units)
+            %Sets the units of the x-axis
             if(isa(units, 'char'))
                 sObj.xunits = units;
             end
         end
         function setYUnits(sObj, units)
+            %setYUnits(sObj, units)
+            %Sets the units of the y-axis
             if(isa(units,'char'))
                 sObj.yunits = units;
             end
         end
         function setSampleRate(sObj, sampleRate)
-%             sampleRate
-%             sObj.sampleRate
-%             ~(sampleRate==sObj.sampleRate)
-            if(~(floor(sampleRate*100)/100==floor(sObj.sampleRate*100)/100)) %Compare to 2 decimal places (finite precision has caused errors 500.000001 ~= 500.00000x
-                if(sampleRate>sObj.sampleRate)
-                    %fprintf(strcat('SignalObj,',sObj.name',', upsampled to:',num2str(sampleRate)));
-                else
-                    %fprintf(strcat('SignalObj,',sObj.name',', downsampled to:',num2str(sampleRate)));
+            % setSampleRate(sObj, sampleRate)
+            % sets the current sampleRate of the object to rate specified
+            if(sObj.sampleRate~=sampleRate)
+                if(~(floor(sampleRate*1000)/1000==floor(sObj.sampleRate*1000)/1000)) %Compare to 3 decimal places (finite precision has caused errors 500.000001 ~= 500.00000x
+                    if(sampleRate>sObj.sampleRate)
+                        %fprintf(strcat('SignalObj,',sObj.name',', upsampled to:',num2str(sampleRate)));
+                    else
+                        %fprintf(strcat('SignalObj,',sObj.name',', downsampled to:',num2str(sampleRate)));
+                    end
+                    sObj.resampleMe(sampleRate);
                 end
-                sObj.resampleMe(sampleRate);
             end
         end
         function setDataLabels(sObj,dataLabels)
-            if(isa(dataLabels,'char'))
-                if(sObj.dimension==1)
-                    sObj.dataLabels{1}=dataLabels;
-                else
-                    error('Adding single dataLabel to a SignalObj with more that 1 dimension');
-                end
+            %setDataLabels(sObj,dataLabels)
+            %sets the labels for each of the components of the SignalObj.
+            %if sObj has only a single component, then dataLabels can be a
+            %string. Otherwise, dataLabels must be a cell with the same
+            %dimensions as sObj. dataLabels{i} specifies the string for the
+            %ith component of sObj.
+            if(~isempty(dataLabels))
+                if(isa(dataLabels,'char'))
+                    if(sObj.dimension==1)
+                        sObj.dataLabels{1}=dataLabels;
+                    else
+                        display('Adding single dataLabel to a SignalObj with more that 1 dimension. All dimensions have same label now!');
+                        for i=1:sObj.dimension
+                            sObj.dataLabels{i} = dataLabels;
+                        end
+                    end
 
-            elseif(isa(dataLabels,'cell'))
-                if(length(dataLabels)==sObj.dimension)
-                    ind=sObj.findIndFromDataMask;
-                    for i=ind
-                        sObj.dataLabels{i} = dataLabels{i};
+                elseif(isa(dataLabels,'cell'))
+                    if(length(dataLabels)==sObj.dimension)
+                        %ind=sObj.findIndFromDataMask;
+                        %for i=ind
+                        %    sObj.dataLabels{i} = dataLabels{i};
+                        %end
+                        sObj.dataLabels = dataLabels;
+                    else
+                        error('Need the number of labels to match the number of dimensions of the SignalObj');
                     end
+                end
+            else
+                if(sObj.dimension==1)
+                    sObj.dataLabels='';
                 else
-                    error('Need the number of labels to match the number of dimensions of the SignalObj');
+                   for i=1:sObj.dimension
+                       sObj.dataLabels{i}='';
+                   end
                 end
             end
         end
         function setMinTime(sObj,minTime,holdVals)
+            %setMinTime(sObj,minTime,holdVals)
+            %sets the minimun value of the time vector to minTime. If
+            %minTime>min(sObj.time) then the data before minTime will be
+            %ignored. If minTime < min(sObj.time) then the time vector is
+            %extended at the current sampleRate to minTime.
+            %holdVals: 1 or 0. If not specifed, defaults to 0. If
+            %holdVals=1, then the value at min(sObj.time) is extended to
+            %the new minTime. Otherwise, the added time is padded with
+            %zeros.
             if(nargin<3)
                 holdVals=0;
             end
@@ -295,6 +379,7 @@
             if(minTime<min(timeVec))
                 maxTime=max(timeVec);
                 newTime=minTime:1/sObj.sampleRate:maxTime;
+                newTime=newTime';
                 numSamples = length(newTime)-length(timeVec);
                 if(holdVals==1)
                     newData=[ones(numSamples,1)*sObj.data(1,:);sObj.data];
@@ -314,6 +399,15 @@
             sObj.minTime=min(sObj.time);
         end
         function setMaxTime(sObj,maxTime, holdVals)
+            %setMaxTime(sObj,maxTime,holdVals)
+            %sets the maximum value of the time vector to maxTime. If
+            %maxTime<max(sObj.time) then the data after maxTime will be
+            %ignored. If maxTime > max(sObj.time) then the time vector is
+            %extended at the current sampleRate to maxTime.
+            %holdVals: 1 or 0. If not specifed, defaults to 0. If
+            %holdVals=1, then the value at min(sObj.time) is extended to
+            %the new minTime. Otherwise, the added time is padded with
+            %zeros.
             if(nargin<3)
                 holdVals=0;
             end
@@ -324,7 +418,8 @@
             timeVec=sObj.getTime;
             if(max(timeVec)<maxTime)
                 minTime=min(timeVec);
-                newTime=minTime:1/sObj.sampleRate:maxTime;
+                newTime=linspace(minTime,maxTime,(sObj.sampleRate)*(maxTime-minTime)+1);
+                newTime = newTime';
                 numSamples = length(newTime)-length(timeVec);
                 if(holdVals==1)
                     newData=[sObj.data;ones(numSamples,1)*sObj.data(end,:)];
@@ -334,7 +429,7 @@
 
                 sObj.data=newData;
                 sObj.time=newTime;
-                sPnj.maxTime=max(sObj.time);
+                sObj.maxTime=max(sObj.time);
             elseif(max(timeVec)==maxTime)
                     %do nothing
 
@@ -346,6 +441,19 @@
             sObj.maxTime=max(sObj.time);
         end
         function setPlotProps(sObj, plotProps,index)
+            %setPlotProps(sObj, plotProps,index)
+            %if index is not specified:
+            %   - plotProps is a cell with sObj.dimension elements, then plotProps{i} specifies a string
+            %     that will be used to plot the ith component of sObj.
+            %   - plotProps is a string, then the string will be used to
+            %     plot all of the components of sObj.
+            %
+            %if index is specified and index is within range of the number
+            %of components of the signal:
+            %   - plotProps is a cell of length 1 then the property is
+            %     applied to the component specified by the index
+            %   - plotProps is a string, the property is applied to the
+            %     component specified by the index.
             if(nargin<=2)
                 if(isa(plotProps,'cell'))
                     if(length(plotProps) == sObj.dimension)
@@ -355,7 +463,7 @@
                     elseif(length(plotProps)==1)
                         for i=1:sObj.dimension
                             sObj.plotProps{i} = cell2str(plotProps);
-                            display('Index not specified. All dimensions set to have same plotting properties');
+%                             display('Index not specified. All dimensions set to have same plotting properties');
                         end
                     else
                         error('Index not specified and more than 1 plotProp specified. Need to number of plotProps same as sObj.dimension or length 1');
@@ -385,6 +493,11 @@
             end
         end
         function setMask(sObj, mask)
+            %setMask(sObj, mask)
+            % if called with no arguments, all the components of the signal
+            % are masked. No data will be visible.
+            % mask: either a set of indices or a cell array of characters
+            % indicating which signal components are to remain visible.
             if(nargin<2)
                 mask=zeros(1,sObj.dimension);
                 sObj.setDataMask(mask);
@@ -403,63 +516,85 @@
                 error('Can only set datamask with strings or indices')
             end
         end
-        function setDataMask(sObj, dataMask)
-        %DataMasks affect how the SignalObj is visualized and converted to
-        %other representations. It doesnt change the dimensions of the
-        %object, nor does it delete data.
-            if(length(dataMask)==sObj.dimension)
-                  sObj.dataMask = dataMask;
-            end
 
-        end
-        function setMaskByInd(sObj,index)
-            if(length(index)==sObj.dimension)
-                sObj.setDataMask(index);
-            else
-                mask=zeros(1,sObj.dimension);
-                mask(index)=1;
-                sObj.setDataMask(mask);
-            end
-        end
-        function setMaskByLabels(sObj,labels)
-            ind=sObj.getIndicesFromLabels(labels);
-            mask=zeros(1,sObj.dimension);
-            mask(ind)=1;
-            sObj.setDataMask(mask);
-        end
 
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         function tVec       = getTime(sObj)
+            % tVec       = getTime(sObj)
+            % returns the time vector of the Signal Obj
             tVec=sObj.time;
         end
         function data       = getData(sObj)
+            % data       = getData(sObj)
+            % Returns the signal data as a matrix. If masks are set, then
+            % only the components that are visible will be returned. Each
+            % column corresponds to each component of the SignalObj that is
+            % visible. The columns are in the same order as the dataLabels.
             data=sObj.dataToMatrix;
         end
         function [t,d]      = getOriginalData(sObj)
+            % [t,d]      = getOriginalData(sObj)
+            % SignalObjs have memory. The original data and time vectors
+            % are stored even when the signal is resamples, windowed, etc.
+            % This commands returns the original data used to create the
+            % SignalObj
             t=sObj.originalTime;
             d=sObj.originalData;
         end
         function s          = getOrigDataSig(sObj)
-                [time,data]=sObj.getOriginalData;
-                name=sObj.name;
-                xlabelval=sObj.xlabelval;
-                xunits=sObj.xunits;
-                yunits=sObj.yunits;
-                dataLabels=sObj.dataLabels;
-                plotProps=sObj.plotProps;
-                s = SignalObj(time, data,name, xlabelval, xunits, yunits,dataLabels,plotProps);
+            % s          = getOrigDataSig(sObj)
+            % same as getOriginalData, except that a SignalObj containing the original data is returned.
+
+            [time,data]=sObj.getOriginalData;
+            name=sObj.name;
+            xlabelval=sObj.xlabelval;
+            xunits=sObj.xunits;
+            yunits=sObj.yunits;
+            dataLabels=sObj.dataLabels;
+            plotProps=sObj.plotProps;
+            ctorHandle = str2func(class(sObj));
+            s = ctorHandle(time, data,name, xlabelval, xunits, yunits,dataLabels,plotProps);
+            %s = SignalObj(time, data,name, xlabelval, xunits, yunits,dataLabels,plotProps);
         end
-        function index      = getIndexFromLabel(sObj,label)
-            index=[];
-            for i=1:length(sObj.dataLabels)
-                if(strcmp(label, sObj.dataLabels{i}))
-                    index = [index i];
-                end
+        function val        = getValueAt(sObj,x)
+            %val        = getValueAt(sObj,x)
+            %returns a row vector of length sObj.dimension corresponding to
+            %the values of the signal evaluated at time=x
+
+            %ind=sObj.findNearestTimeIndices(x);
+            %val=sObj.data(ind,:);
+
+            [l,w]=size(x);
+            if(w>l)
+                x=x';
+            end
+
+%             val = interp1(sObj.time,sObj.data,x,'spline',0); %extrapolate to zero
+            val = interp1(sObj.time,sObj.data,x,'nearest',0); %extrapolate to zero
+%             if(any(isnan(sObj.data)))
+%                 pause
+%             end
+%             if(sObj.dimension==1)
+%                 val=val';
+%             end
+
+        end
+        function PropsStr   = getPlotProps(sObj,index)
+            %PropsStr   = getPlotProps(sObj,index)
+            %Returns the string correspond to the plotting properties of
+            %the SignalObj component corresponding to index
+            if(index>0 && index<=sObj.dimension)
+                PropsStr = cell2str(sObj.plotProps{index});
+            else
+                error('index is out of bounds!');
             end
         end
         function indices    = getIndicesFromLabels(sObj,label)
+            %indices    = getIndicesFromLabels(sObj,label)
             %Returns a cell array if the label appears for various point
-            %int the SignalObj
+            %in the SignalObj. indices{i} contains all the the indices
+            %corresponding to label{i} if label is a cell-array or label if
+            %it is a string.
             %Returns an array if the SignalObj label appears only once in the
             %SignalObj
             if(isa(label,'cell'))
@@ -476,47 +611,42 @@
 
                 end
             elseif(isa(label,'char'))
-                indices = getIndexFromLabel(label);
-                numInd = length(indices);
+                indices = sObj.getIndexFromLabel(label);
+                numInd(1) = length(indices);
             end
+
+
             if(max(numInd)==1)      %For backwards compatibility if assuming only on index per label
-                for i=1:length(label)
-                    tempInd(i) = indices{i};
+                if(isa(indices,'cell'))
+                    for i=1:length(numInd)
+
+                        tempInd(i) = indices{i};
+                    end
+                    indices = tempInd;
                 end
-                indices = tempInd;
-            end
-        end
-        function val        = getValueAt(sObj,x)
-            ind=sObj.findNearestTimeIndex(x);
-            val=sObj.data(ind,:);
-        end
-        function PropsStr   = getPlotProps(sObj,index)
-            if(index>0 && index<=sObj.dimension)
-                PropsStr = cell2str(sObj.plotProps{index});
-            else
-                error('index is out of bounds!');
             end
         end
 
-
-
-
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         % Operand Definitions and other mathematical operations
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         function s3 = plus(s1,s2)
+            % s3 = plus(s1,s2)
+            % Adds two signals
             if(isa(s1,'SignalObj') && isa(s2,'SignalObj'))
-                % What is s2 is a constant or double?
+                % What if s2 is a constant or double?
                 if(s1.dimension == s2.dimension)
                     [s1c,s2c] = makeCompatible(s1,s2);
                     s3=s1c.copySignal;
                     s3.data = s1c.data+s2c.data;
 
                     for i=1:length(s3.dataLabels)
-                        if(strcmp(s2c.dataLabels{i}(1),'-'))
-                            s3.dataLabels{i} = [s1c.dataLabels{i} '-' s2c.dataLabels{i}(2:end)];
-                        else
-                            s3.dataLabels{i} = [s1c.dataLabels{i} '+' s2c.dataLabels{i}];
+                        if(~s2c.areDataLabelsEmpty && ~isempty(s2c.dataLabels{i}))
+                            if(strcmp(s2c.dataLabels{i}(1),'-'))
+                                s3.dataLabels{i} = [s1c.dataLabels{i} '-' s2c.dataLabels{i}(2:end)];
+                            else
+                                s3.dataLabels{i} = [s1c.dataLabels{i} '+' s2c.dataLabels{i}];
+                            end
                         end
                     end
                 else
@@ -570,20 +700,43 @@
             end
         end
         function s3 = minus(s1,s2)
+            % s3 = minus(s1,s2)
+            % Subtracts two signals
             s3=plus(s1,-s2);
         end
         function s3 = uplus(s1) %+s1
+            % s3 = uplus(s1)
+            % Multiplies signal by +1
             s3=s1.copySignal;
         end
         function s3 = uminus(s1) %-s1
-
+            % s3 = uminus(s1) %-s1
+            % Multiplies the signal by -1.
+            % dataLabels are updated to reflect this.
             s3=s1.copySignal;
             s3.data=-s3.data;
             for i=1:length(s3.dataLabels)
                 s3.dataLabels{i} = strcat('-',s1.dataLabels{i});
             end
         end
+        function s3 = power(s1,exponent)
+           if(isa(exponent,'double'))
+               s3=s1.copySignal;
+               s3.data=s3.data.^exponent;
+           else
+               error('Exponent should be a double');
+           end
+        end
+
+        function s3 = sqrt(s1)
+           s3=s1.copySignal;
+           s3.data=sqrt(s3.data);
+
+        end
         function s3 = times(s1,s2) %s1.*s2
+            %s3 = times(s1,s2)
+            %Multiplies each sample in s1 with each sample of s2.
+            %s1 or s2 can be doubles
             if(isa(s1,'SignalObj') && isa(s2,'SignalObj'))
                if(s1.dimension == s2.dimension)
                    [s1c,s2c] = makeCompatible(s1,s2);
@@ -614,6 +767,9 @@
             end
         end
         function s3 = mtimes(s1,s2) %s1*s2
+            %Matrix multiplication of two signals
+            %Needs work
+            %If s1 and s2 are signals, same as times(s1,s2)
             if(isa(s1,'SignalObj') && isa(s2,'SignalObj'))
                    %[s1c,s2c] = makeCompatible(s1,s2);
                    s3 = s1.copySignal;
@@ -682,6 +838,8 @@
             s3.dimension=w;
         end
         function s3 = derivative(sObj)
+            %Computes derivative of each component of the SignalObj with
+            %respect to the x-axis variable.
 %             B=[1 -1]*sObj.sampleRate;
 %             A=1;
 %             s3=sObj.filter(B,A);
@@ -695,16 +853,34 @@
 
             s3.setName(strcat('\frac{d}{',denomstr,'}',s3.name));
             for i=1:s3.dimension
-                if(~strcmp(sObj.dataLabels{i},''))
-                    s3.dataLabels{i}= strcat('\frac{d}{',denomstr,'}',s3.dataLabels{i});
+                if(s3.dimension ==1)
+                    if(~strcmp(sObj.dataLabels,''))
+                        s3.dataLabels{i}= strcat('\frac{d}{',denomstr,'}',s3.dataLabels);
+                    end
+                else
+                    if(~strcmp(sObj.dataLabels{i},''))
+                        s3.dataLabels{i}= strcat('\frac{d}{',denomstr,'}',s3.dataLabels{i});
+                    end
                 end
             end
         end
         function val = derivativeAt(sObj,x0)
+            % val = derivativeAt(sObj,x0)
+            %computes the derivative of the Signal at x0. Returns a row
+            %vector of length equal to sObj.dimension.
             sTemp = sObj.derivative;
             val = sTemp.getValueAt(x0);
         end
         function s3 = integral(sObj,t0,tf)
+            %s3 = integral(sObj,t0,tf)
+            %computes the integral of the signal in the window from t0 to tf.
+            %if tf is not specified, sObj.maxTime is used.
+            %if t0 is not specified, sObj.minTime is used.
+            % Both t0 and tf are optional but t0 must be specified if tf is
+            % to be specified (e.g. cant specified tf only)
+            % Data labels are updated with latex notation for integral.
+            % the value of the returned signal at time t is the value of
+            % the integral from minTime to t.
             if(nargin<3)
                 tf=sObj.maxTime;
             end
@@ -720,68 +896,303 @@
             s3.setYUnits(strcat(s3.yunits,'*',s3.xunits));
             dtstr = strcat(' d','\tau');
             s3.setName(['\int_',num2str(s3.minTime),'^',s3.xlabelval(1),'\!\!{',[s3.name dtstr],'}']);
-            for i=1:s3.dimension
-                if(~strcmp(sObj.dataLabels{i},''))
-                    s3.dataLabels{i}= ['\int_',num2str(s3.minTime),'^',s3.xlabelval(1),'\!\!{',[s3.dataLabels{i} dtstr],'}'];
+            if(~sObj.areDataLabelsEmpty)
+                for i=1:s3.dimension
+                    if(~strcmp(sObj.dataLabels{i},''))
+                        s3.dataLabels{i}= ['\int_',num2str(s3.minTime),'^',s3.xlabelval(1),'\!\!{',[s3.dataLabels{i} dtstr],'}'];
+                    else
+                        s3.dataLabels{i} = '';
+                    end
                 end
             end
         end
         function s3 = filter(sObj, B,A)
+            %s3 = filter(sObj, B,A)
+            %applies the discrete filter specified by B and A to the sObj
+            %data. Same as running filter(B,A,sObj.dataToMatrix). dataMasks
+            %are ignores so that the signal dimensionality does not change.
             s3=sObj.copySignal;
             s3.data = filter(B,A,s3.data);
         end
         function s3 = filtfilt(sObj,B,A)
+            %s3 = filtfilt(sObj,B,A)
+            %applies the discrete filter specified by B and A to the sObj
+            %data using filtfilt. Same as running filtfilt(B,A,sObj.dataToMatrix). dataMasks
+            %are ignores so that the signal dimensionality does not change.
             s3=sObj.copySignal;
             s3.data = filtfilt(B,A,s3.data);
         end
         function [s1c,s2c] = makeCompatible(s1,s2,holdVals)
+            %[s1c,s2c] = makeCompatible(s1,s2,holdVals)
+            %returns two signals that copies of the original signals but
+            %that have been resampled or extended in time so that the time
+            %axis of both signals in the same. This is done before most
+            %mathmatical operations to make sure that the signals have the
+            %same support.
+            % holdVals = 1 makes the signals keep their endpoint values if
+            % they are extended in time. holdVals is an optional argument.
             if(nargin<3)
                 holdVals=0;
             end
-            s1c = s1.copySignal; s2c = s2.copySignal;
-            minTime=min(s1c.minTime,s2c.minTime);
-            maxTime=max(s1c.maxTime,s2c.maxTime);
-            sampleRate=max(s1c.sampleRate,s2c.sampleRate);
-            s1c.setSampleRate(sampleRate); s2c.setSampleRate(sampleRate);
-            s1c.setMinTime(minTime,holdVals);       s2c.setMinTime(minTime,holdVals);
-            s1c.setMaxTime(maxTime,holdVals);       s2c.setMaxTime(maxTime,holdVals);
-
-            %pause
-%             for i=1:s2c.dimension
-             data = spline(s2c.time,s2c.data',s1c.time);
-%             end
-             s2c.time = s1c.time;
-             [nrows,ncolumns] = size(data);
-             if(nrows>ncolumns)
-                s2c.data = data;
-             else
-                s2c.data = data';
-             end
+            if(s1.minTime~=s2.minTime || s1.maxTime~=s2.maxTime || s1.sampleRate ~=s2.sampleRate)
+                s1c = s1.copySignal; s2c = s2.copySignal;
+                minTime=min(s1c.minTime,s2c.minTime);
+                maxTime=max(s1c.maxTime,s2c.maxTime);
+                sampleRate=max(s1c.sampleRate,s2c.sampleRate);
+                s1c.setSampleRate(sampleRate); s2c.setSampleRate(sampleRate);
+                s1c.setMinTime(minTime,holdVals);       s2c.setMinTime(minTime,holdVals);
+                s1c.setMaxTime(maxTime,holdVals);       s2c.setMaxTime(maxTime,holdVals);
+
+                %pause
+    %             for i=1:s2c.dimension
+                 %if(max(s2c.time-s1c.time)>0)
+                    data = interp1(s2c.time,s2c.data,s1c.time,'nearest',0);
+                 %else
+                 %   data = s2c.data;
+                 %end
+    %             end
+                 s2c.time = s1c.time;
+                 [nrows,ncolumns] = size(data);
+                 if(nrows>ncolumns)
+                    s2c.data = data;
+                 else
+                    s2c.data = data';
+                 end
+            else
+                s1c = s1;
+                s2c = s2;
+            end
+
+        end
+        function s = abs(sObj)
+            absData=abs(sObj.data);
+            [nrows,ncolumns]=size(absData);
+            name =  ['|', sObj.name '|'];
+            plotProps  = sObj.plotProps;
+            if(~sObj.areDataLabelsEmpty)
+                    dataLabels = cell(size(sObj.dataLabels));
+%                     plotProps  = sObj.plotProps;
+                    for i=1:sObj.dimension
+                        dataLabels{i} = strcat('|',sObj.dataLabels{i},'|');
+                    end
+                evalstring = strcat('s=',class(sObj),'(sObj.time, absData,name,sObj.xlabelval, sObj.xunits,sObj.yunits,dataLabels,plotProps);');
+            else
+
+                evalstring = strcat('s=',class(sObj),'(sObj.time, absData,name,sObj.xlabelval, sObj.xunits,sObj.yunits,[],plotProps);');
+            end
+            eval(evalstring);
+        end
+
+        function s = log(sObj)
+            logData=log(sObj.data);
+            [nrows,ncolumns]=size(logData);
+            name =  ['ln(' sObj.name ')'];
+            yunits = ['ln(' sObj.yunits ')'];
+            plotProps  = sObj.plotProps;
+            if(~sObj.areDataLabelsEmpty)
+                    dataLabels = cell(size(sObj.dataLabels));
+%                     plotProps  = sObj.plotProps;
+                    for i=1:sObj.dimension
+                        dataLabels{i} = strcat('ln(',sObj.dataLabels{i},')');
+                    end
+                evalstring = strcat('s=',class(sObj),'(sObj.time, logData,name,sObj.xlabelval, sObj.xunits,yunits,dataLabels,plotProps);');
+            else
+
+                evalstring = strcat('s=',class(sObj),'(sObj.time, logData,name,sObj.xlabelval, sObj.xunits,yunits,[],plotProps);');
+            end
+            eval(evalstring);
+        end
+
+        function m=median(sObj,varargin)
+            %m=median(sObj,varargin)
+            %Computes the column-wise median of SignalObj data. Returns a
+            %signal with the corresponding median values
+            %same as calling median(sObj.dataToMatrix,varargin)
+            %Additional parameters are passed to the matlab median function.
+            %Default computes median of each signal component across time.
+            %mean(sObj,2) computes median value of the SignalObj at each
+            %point in time.
+            mdata=median(sObj.data,varargin{:});
+            [nrows,ncolumns]=size(mdata);
+            if( (nrows==length(sObj.time)) && (ncolumns==1) ) %mean across dimensions
+                name =  ['median(', sObj.name ')'];
+                evalstring = strcat('m=',class(sObj),'(sObj.time, mdata,name,sObj.xlabelval, sObj.xunits,sObj.yunits);');
+                eval(evalstring);
+            elseif( (nrows==1) && (ncolumns == sObj.dimension) )  %mean of each dimension
+                if(~sObj.areDataLabelsEmpty)
+                    dataLabels = cell(size(sObj.dataLabels));
+                    for i=1:sObj.dimension
+                        dataLabels{i} = strcat('median(',sObj.dataLabels{i},')');
+                    end
+                     name =  ['median(', sObj.name ')'];
+                     evalstring = strcat('m=',class(sObj),'([sObj.time(1); sObj.time(end)], [mdata;mdata],name,sObj.xlabelval, sObj.xunits,sObj.yunits,dataLabels);');
+                     eval(evalstring);
+                     %m=SignalObj([sObj.time(1); sObj.time(end)], [mdata;mdata], ['Mean of ' sObj.name],sObj.xlabelval, sObj.xunits,sObj.yunits,dataLabels);
+                else
+                     name =  ['median(', sObj.name ')'];
+                     evalstring = strcat('m=',class(sObj),'([sObj.time(1); sObj.time(end)], [mdata;mdata],name,sObj.xlabelval, sObj.xunits,sObj.yunits);');
+                     eval(evalstring);
+                     %m=SignalObj([sObj.time(1); sObj.time(end)], [mdata;mdata],  ['Mean of ' sObj.name],sObj.xlabelval, sObj.xunits,sObj.yunits);
+                end
+
+
+            end
+
+
+
         end
+        function m=mode(sObj,varargin)
+            %m=mode(sObj,varargin)
+            %Computes the column-wise mode of SignalObj data. Returns a
+            %signal with the corresponding mode values
+            %same as calling mode(sObj.dataToMatrix,varargin)
+            %Additional parameters are passed to the matlab mode function.
+            %Default computes mode of each signal component across time.
+            %mean(sObj,2) computes mode value of the SignalObj at each
+            %point in time.
+            mdata=mode(sObj.data,varargin{:});
+            [nrows,ncolumns]=size(mdata);
+            if( (nrows==length(sObj.time)) && (ncolumns==1) ) %mean across dimensions
+                name =  ['mode(', sObj.name ')'];
+                evalstring = strcat('m=',class(sObj),'(sObj.time, mdata,name,sObj.xlabelval, sObj.xunits,sObj.yunits);');
+                eval(evalstring);
+            elseif( (nrows==1) && (ncolumns == sObj.dimension) )  %mean of each dimension
+                if(~sObj.areDataLabelsEmpty)
+                    dataLabels = cell(size(sObj.dataLabels));
+                    for i=1:sObj.dimension
+                        dataLabels{i} = strcat('mode(',sObj.dataLabels{i},')');
+                    end
+                     name =  ['mode(', sObj.name ')'];
+                     evalstring = strcat('m=',class(sObj),'([sObj.time(1); sObj.time(end)], [mdata;mdata],name,sObj.xlabelval, sObj.xunits,sObj.yunits,dataLabels);');
+                     eval(evalstring);
+                     %m=SignalObj([sObj.time(1); sObj.time(end)], [mdata;mdata], ['Mean of ' sObj.name],sObj.xlabelval, sObj.xunits,sObj.yunits,dataLabels);
+                else
+                     name =  ['mode(', sObj.name ')'];
+                     evalstring = strcat('m=',class(sObj),'([sObj.time(1); sObj.time(end)], [mdata;mdata],name,sObj.xlabelval, sObj.xunits,sObj.yunits);');
+                     eval(evalstring);
+                     %m=SignalObj([sObj.time(1); sObj.time(end)], [mdata;mdata],  ['Mean of ' sObj.name],sObj.xlabelval, sObj.xunits,sObj.yunits);
+                end
+            end
+        end
+
+
         function m=mean(sObj,varargin)
+            %m=mean(sObj,varargin)
+            %Computes the column-wise mean of SignalObj data. Returns a
+            %signal with the corresponding mean values
+            %same as calling mean(sObj.dataToMatrix,varargin)
+            %Additional parameters are passed to the matlab mean function.
+            %Default computes mean of each signal component across time.
+            %mean(sObj,2) computes mean value of the SignalObj at each
+            %point in time.
             mdata=mean(sObj.data,varargin{:});
             [nrows,ncolumns]=size(mdata);
             if( (nrows==length(sObj.time)) && (ncolumns==1) ) %mean across dimensions
-                m=SignalObj(sObj.time, mdata, ['Mean of ' sObj.name],sObj.xlabelval, sObj.xunits,sObj.yunits);
+                name =  ['\mu(', sObj.name ')'];
+                evalstring = strcat('m=',class(sObj),'(sObj.time, mdata,name,sObj.xlabelval, sObj.xunits,sObj.yunits);');
+                eval(evalstring);
             elseif( (nrows==1) && (ncolumns == sObj.dimension) )  %mean of each dimension
                 if(~sObj.areDataLabelsEmpty)
                     dataLabels = cell(size(sObj.dataLabels));
                     for i=1:sObj.dimension
                         dataLabels{i} = strcat('\mu(',sObj.dataLabels{i},')');
                     end
-                     m=SignalObj([sObj.time(1); sObj.time(end)], [mdata;mdata], ['Mean of ' sObj.name],sObj.xlabelval, sObj.xunits,sObj.yunits,dataLabels);
+                     name =  ['\mu(', sObj.name ')'];
+                     evalstring = strcat('m=',class(sObj),'([sObj.time(1); sObj.time(end)], [mdata;mdata],name,sObj.xlabelval, sObj.xunits,sObj.yunits,dataLabels);');
+                     eval(evalstring);
+                     %m=SignalObj([sObj.time(1); sObj.time(end)], [mdata;mdata], ['Mean of ' sObj.name],sObj.xlabelval, sObj.xunits,sObj.yunits,dataLabels);
+                else
+                     name =  ['\mu(', sObj.name ')'];
+                     evalstring = strcat('m=',class(sObj),'([sObj.time(1); sObj.time(end)], [mdata;mdata],name,sObj.xlabelval, sObj.xunits,sObj.yunits);');
+                     eval(evalstring);
+                     %m=SignalObj([sObj.time(1); sObj.time(end)], [mdata;mdata],  ['Mean of ' sObj.name],sObj.xlabelval, sObj.xunits,sObj.yunits);
+                end
+            end
+        end
+        function m=std(sObj,varargin)
+            %m=std(sObj,varargin)
+            %Computes the column-wise std of SignalObj data. Returns a
+            %signal with the corresponding mean values
+            %same as calling std(sObj.dataToMatrix,varargin)
+            %Additional parameters are passed to the matlab std function.
+            %Default computes std of each signal component across time.
+            %std(sObj,[],2) computes std value of the SignalObj at each
+            %point in time.
+            stdData=std(sObj.data,varargin{:});
+            [nrows,ncolumns]=size(stdData);
+            if( (nrows==length(sObj.time)) && (ncolumns==1) ) %mean across dimensions
+                     name=['\sigma(' sObj.name ')'];
+                     evalstring = strcat('m=',class(sObj),'(sObj.time, stdData,name,sObj.xlabelval, sObj.xunits,sObj.yunits);');
+                     eval(evalstring);
+                     %m=SignalObj(sObj.time, stdData, name,sObj.xlabelval, sObj.xunits,sObj.yunits);
+            elseif( (nrows==1) && (ncolumns == sObj.dimension) )  %mean of each dimension
+                if(~sObj.areDataLabelsEmpty)
+                    dataLabels = cell(size(sObj.dataLabels));
+                    for i=1:sObj.dimension
+                        dataLabels{i} = strcat('\sigma(',sObj.dataLabels{i},')');
+                    end
+                     name=['\sigma(' sObj.name ')'];
+                     evalstring = strcat('m=',class(sObj),'([sObj.time(1); sObj.time(end)], [stdData;stdData],name,sObj.xlabelval, sObj.xunits,sObj.yunits,dataLabels);');
+                     eval(evalstring);
+                     %m=SignalObj([sObj.time(1); sObj.time(end)], [stdData;stdData], ['Std. Dev. of ' sObj.name],sObj.xlabelval, sObj.xunits,sObj.yunits,dataLabels);
                 else
-                    m=SignalObj([sObj.time(1); sObj.time(end)], [mdata;mdata],  ['Mean of ' sObj.name],sObj.xlabelval, sObj.xunits,sObj.yunits);
+                    name=['\sigma(' sObj.name ')'];
+                    evalstring = strcat('m=',class(sObj),'([sObj.time(1); sObj.time(end)], [stdData;stdData],name,sObj.xlabelval, sObj.xunits,sObj.yunits);');
+                    eval(evalstring);
+                    %m=SignalObj([sObj.time(1); sObj.time(end)], [stdData;stdData],  ['Std. Dev. of ' sObj.name],sObj.xlabelval, sObj.xunits,sObj.yunits);
                 end
             end
+
         end
-        function m=max(sObj,varargin)
-            m=max(sObj.data,varargin{:});
+        function [m, index,time]=max(sObj,varargin)
+            %[m,time]=max(sObj,varargin)
+            %calls matlab max function with the SignalObj's data.
+            %Additional parameters are passed to the matlab function
+            [m,index]=max(sObj.data,varargin{:});
+            time = sObj.time(index);
+
+        end
+        function [m,index,time]=min(sObj,varargin)
+            %m=min(sObj,varargin)
+            %calls matlab min function with the SignalObj's data.
+            %Additional parameters are passed to the matlab function
+            [m,index]=min(sObj.data,varargin{:});
+            time = sObj.time(index);
+        end
+
+        function s = autocorrelation(sObj)
+            if(sObj.dimension==1)
+                 [ACF,lags,bounds] = crosscorr(sObj.data,sObj.data,length(sObj.data)-1);
+                 s=SignalObj(lags/sObj.sampleRate, ACF,['ACF(' sObj.name, ')'], 'Lag', sObj.xunits, [sObj.yunits '^2']);
+            else
+                %if more than one dimension then computes the
+                %autocorrelation of each dimension with itself
+                for i=1:sObj.dimension
+
+                    [ACF(:,i),lags,bounds] = crosscor(sObj.data(:,i),sObj.data(:,i),length(sObj.data(:,i))-1);
+
+                end
+                s=SignalObj(lags/sObj.sampleRate, ACF,['ACF(' sObj.name, ')'], 'Lag', sObj.xunits, [sObj.yunits '^2']);
+            end
+
         end
-        function m=min(sObj,varargin)
-            m=min(sObj.data,varargin{:});
+
+        function s = crosscorrelation(sObj, s2)
+            if(and(sObj.dimension ==1, s2.dimension==1))
+
+             [xcf,lags,bounds] = crosscorr(sObj.data, s2.data,length(sObj.data)-1);
+             s=SignalObj(lags/sObj.sampleRate, xcf,['XCORF(' sObj.name, ')'], 'Lag', sObj.xunits, [sObj.yunits '^2']);
+            else
+                error('Does not support multidimensional signals');
+            end
         end
+
         function periodogram = periodogram(sObj)
+            %periodogram = periodogram(sObj)
+            % computes the periodogram of each component of the SignalObj.
+            % Calls matlab periodogram with each component.
+            %   fs = sObj.sampleRate;
+            %   NFFT = 1024
             fs = sObj.sampleRate;  % Sampling frequency
             periodogram = cell(1,sObj.dimension);
             for i=1:sObj.dimension
@@ -802,10 +1213,20 @@
                          h=gcf;figure(h);
                 end
                 periodogram{i}=psd(Hs,xn,'Fs',fs,'NFFT',1024);
-                h=periodogram{i}.plot;legend(h, sObj.dataLabels{i});
+
+                if(~isempty(sObj.dataLabels))
+                    h=periodogram{i}.plot;legend(h, sObj.dataLabels{i});
+                else
+                    h=periodogram{i}.plot;
+                end
             end
         end
         function mtmSpec = MTMspectrum(sObj,NW,NFFT,Pval)
+            %mtmSpec = MTMspectrum(sObj,NW,NFFT,Pval)
+            %computes Multi-taper spectral estimate of each component of
+            %the SignalObj with defaults:
+            %NW=4, NFFT=[], Pval=.95
+            % Defaults can be changed by passing in additional arguments
             if(nargin<4)
                 Pval=.95;
             end
@@ -841,10 +1262,134 @@
                 h=plot(hpsd); legend(h, sObj.dataLabels{i},strcat('-',str1),strcat('+',str1));
             end
         end
+        function [spectrogramData,h] = spectrogram(sObj,freqVec,h)
+            if(nargin<3 ||isempty(h))
+                h=figure;
+            end
+            if(nargin<2 || isempty(freqVec))
+                freqVec=0:.1:50;
+            end
+            t=sObj.time;             % 2 secs @ 1kHz sample rate
+            x=sObj.data;             % Start @ DC, cross 150Hz at t=1sec
+            F = freqVec;
+            window=kaiser(round(length(t)/20));
+            nooverlap = round(round(length(t)/40));
+            clear y f t p;
+            for i=1:sObj.dimension
+%                 figure;
+                [y{i},f{i},t{i},p{i}] = spectrogram(x(:,i),window,nooverlap,F,sObj.sampleRate);
+                t{i} = t{i}+min(sObj.time);
+                surf(t{i},f{i},10*log10(abs(p{i})),'EdgeColor','none');
+                axis xy; axis tight; colormap(jet); view(0,90);
+                xlabel('time [s]');
+                ylabel('frequency [Hz]');
+
+            end
+            if(sObj.dimension>1)
+                for i=1:sObj.dimension
+                    spectrogramData{i}.t = t{i};
+                    spectrogramData{i}.f = f{i};
+                    spectrogramData{i}.p = p{i};
+                    spectrogramData{i}.y = y{i};
+                end
+            else
+                spectrogramData.t = t{i};
+                spectrogramData.f = f{i};
+                spectrogramData.p = p{i};
+                spectrogramData.y = y{i};
+
+            end
+        end
+
+
+        function sxCorr= xcorr(s1,s2,varargin)
+            if(nargin<2)
+                s2=s1;
+            end
+
+             [s1c,s2c] = makeCompatible(s1,s2);
+             if(~isempty(varargin))
+                [tempC tempLags] =xcorr([s1c.data, s2c.data],varargin{1});
+             else
+                 [tempC tempLags] =xcorr([s1c.data, s2c.data]);
+             end
+             index=[];
+             for i=1:s1c.dimension
+                 offset(i)=(i-1)*(s1c.dimension+s2c.dimension);
+                 index = [index, (offset(i)+s1c.dimension+1):(offset(i)+s1c.dimension+s2c.dimension)];
+             end
+             sum=0;
+             dataLabels = cell(1,length(index));
+             for i=1:s1c.dimension
+                 for j=1:s2c.dimension
+                     sum = sum+1;
+                     dataLabels{sum} = strcat('corr(',s1c.dataLabels{i},',',s2c.dataLabels{j}, ']');
+                 end
+             end
+
+             M=length(s1c.data);
+             if(nargin<2)
+                data=tempC(M-1:end,index);
+                lags=tempLags(M-1:end)./s1c.sampleRate;
+             else
+                 data=tempC(1:end,index);
+                lags=tempLags(1:end)./s1c.sampleRate;
+             end
+             name = [ 'corr(' s1.name ',' s2.name ')'];
+             evalstring=strcat('sxCorr=',class(s1),'(lags,data,name,''\Delta \tau'',''s'',dataLabels);');
+             eval(evalstring);
+        end
+        function sxCov = xcov(s1,s2,varargin)
+             if(nargin<2)
+                s2=s1;
+             end
+             [s1c,s2c] = makeCompatible(s1,s2);
+             if(~isempty(varargin))
+                [tempC tempLags] =xcov([s1c.data, s2c.data],varargin{1});
+             else
+                 [tempC tempLags] =xcov([s1c.data, s2c.data]);
+             end
+             index=[];
+             for i=1:s1c.dimension
+                 offset(i)=(i-1)*(s1c.dimension+s2c.dimension);
+                 index = [index, (offset(i)+s1c.dimension+1):(offset(i)+s1c.dimension+s2c.dimension)];
+             end
+             sum=0;
+             dataLabels = cell(1,length(index));
+             for i=1:s1c.dimension
+                 for j=1:s2c.dimension
+                     sum = sum+1;
+                     dataLabels{sum} = strcat('cov(',s1c.dataLabels{i},',',s2c.dataLabels{j}, ']');
+                 end
+             end
+
+             M=length(s1c.data);
+             if(nargin<2)
+                data=tempC(M-1:end,index);
+                lags=tempLags(M-1:end)./s1c.sampleRate;
+             else
+                 data=tempC(1:end,index);
+                 lags=tempLags(1:end)./s1c.sampleRate;
+             end
+             name = [ 'cov(' s1.name ',' s2.name ')'];
+             evalstring=strcat('sxCov=',class(s1),'(lags,data,name,''\Delta \tau'',''s'',dataLabels);');
+             eval(evalstring);
+             %sxCov=SignalObj(lags,data,name,'lags','n',dataLabels);
+
+
+         end
+
+
+
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         %Utility Functions
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         function mergedSig = merge(sObj,varargin)
+            %mergedSig = merge(sObj,varargin)
+            %merges the data in any signals passed in with the data in sObj.
+            %if one of the input arguments is a double then it is assumed
+            %to be the value of holdVals to be used when making the signals
+            %compatible. See makeCompatible(s1,s2)
             numToMerge=0;
             holdVals=0;
             for i=1:length(varargin)
@@ -857,6 +1402,7 @@
             end
 
             if(numToMerge == 1)
+
                 [s1c, s2c] = sObj.makeCompatible(varargin{1},holdVals);
                  data=[s1c.dataToMatrix, s2c.dataToMatrix];
                  dataLabels = cell(s1c.dimension+s2c.dimension,1);
@@ -866,8 +1412,10 @@
                  for i=1:s2c.dimension
                         dataLabels{s1c.dimension+i}=s2c.dataLabels{i};
                  end
-                 name = [s1c.name, ',', s2c.name];
-                 mergedSig = SignalObj(s1c.time, data, name, s1c.xlabelval,s1c.xunits, s1c.yunits, dataLabels);
+                 name = s1c.name;%, ',', s2c.name]; %name of the original is kept
+
+                 evalstring = strcat('mergedSig = ',class(sObj),'(s1c.time, data, name, s1c.xlabelval,s1c.xunits, s1c.yunits, dataLabels);');
+                 eval(evalstring);
             else
                   mergedSig = sObj.merge(varargin{1},holdVals);
                   for i=2:numToMerge
@@ -876,6 +1424,12 @@
             end
         end
         function sigOut=copySignal(sigIn)
+            %sigOut=copySignal(sigIn)
+            %sigOut is a copy of the original signal sigIn.
+            %This function is necessary because SignalObj's are handles and
+            %as such, setting s2=s1, and then changing s1, causes changes
+            %in s2 as well. To avoid this, make a copy of s1 and set it as
+            %s2. (eg. s2=s1.copySignal; )
                 time=sigIn.time;
                 data=sigIn.data;
                 name=sigIn.name;
@@ -884,28 +1438,56 @@
                 yunits=sigIn.yunits;
                 dataLabels=sigIn.dataLabels;
                 plotProps=sigIn.plotProps;
-                sigOut = SignalObj(time, data,name, xlabelval, xunits, yunits,dataLabels,plotProps);
+                evalstring = strcat('sigOut=',class(sigIn),'(time, data,name, xlabelval, xunits, yunits,dataLabels,plotProps);');
+                eval(evalstring);
+                %sigOut = SignalObj(time, data,name, xlabelval, xunits, yunits,dataLabels,plotProps);
                 sigOut.dataMask = sigIn.dataMask;
         end
         function sObjOut=resample(sObj, newSampleRate)
-            sObjOut = sObj.copySignal;
-            sObjOut.resampleMe(newSampleRate);
+            %sObjOut=resample(sObj, newSampleRate)
+            %sObjOut is a copy of sObj with the newSampleRate specified;
+            if(sObj.sampleRate ~=newSampleRate)
+                sObjOut = sObj.copySignal;
+                if(or(~isnan(sObjOut.sampleRate),size(sObjOut.data,1)>1))
+                    sObjOut.resampleMe(newSampleRate);
+                end
+            else
+                sObjOut = sObj.copySignal;
+            end
         end
         function resampleMe(sObj, newSampleRate)
-            sObj.restoreToOriginal; %use default data for changing samplerate
-            %Change the sampling rate of this object
-            minTime=sObj.minTime;
-            maxTime=sObj.maxTime;
-            newTime=minTime:1/newSampleRate:maxTime;
-            newData=zeros(length(newTime),sObj.dimension);
-            for i=1:sObj.dimension
-                newData(:,i)= spline(sObj.time,sObj.data(:,i),newTime);
-            end
-                sObj.time=newTime;
+            %resampleMe(sObj, newSampleRate)
+            %Actually changes the sampleRate of the sObj signal. There is
+            %no output variable. Recall that SignalObj's are handles.
+
+            if(sObj.sampleRate~=newSampleRate)
+                sObj.restoreToOriginal; %use default data for changing samplerate
+                %Change the sampling rate of this object
+                minTime=sObj.minTime;
+                maxTime=sObj.maxTime;
+                newTime=minTime:1/newSampleRate:maxTime;
+                newData=zeros(length(newTime),sObj.dimension);
+                if(size(sObj.data,1)>1)
+                    for i=1:sObj.dimension
+        %                 newData(:,i)= interp1(sObj.time,sObj.data(:,i),newTime,'spline','extrap');
+                        newData(:,i)= interp1(sObj.time,sObj.data(:,i),newTime,'spline',0);
+                    end
+                else
+                    newData = sObj.data;
+                end
+                sObj.time=newTime';
                 sObj.data=newData;
                 sObj.sampleRate=newSampleRate;
+            end
         end
         function restoreToOriginal(sObj,rMask)
+            %restoreToOriginal(sObj)
+            %return the signal to its original state when created. The
+            %dataMasks are the only thing preserved.
+            %
+            %restoreToOriginal(sObj,rMask)
+            %if rMask=1, then the dataMask is reset.
+
             if(nargin<2)
               rMask = 0; %keep mask even when data reset
             end
@@ -920,15 +1502,29 @@
              end
         end
         function resetMask(sObj)
+            %resetMask(sObj)
+            %Resets the dataMask for the SignalObj so that all components
+            %of the object are visible.
             sObj.dataMask = ones(1,sObj.dimension);
         end
         function ind = findIndFromDataMask(sObj)
+            %ind = findIndFromDataMask(sObj)
+            %returns the indices of the visible components of the data
             ind = find(sObj.dataMask ==1);
 %             if(isempty(ind))
 %                 error('All data masked out of SignalObj');
 %             end
         end
+        function ind = findNearestTimeIndices(sObj,times)
+            ind = zeros(size(times));
+            for i=1:length(ind)
+                ind(i) = sObj.findNearestTimeIndex(times(i));
+            end
+        end
+
         function ind=findNearestTimeIndex(sObj, time)
+            %ind=findNearestTimeIndex(sObj, time)
+            %returns the nearest index to t=time;
             if(time<sObj.minTime)
                 ind=1;
             elseif(time>sObj.maxTime)
@@ -948,23 +1544,42 @@
 %                 end
             end
         end
-        function sOut = shift(sObj, deltaT)
-
-            %returns s shift by deltaT. If deltaT is positive
+        function sOut = shift(sObj, deltaT,updateLabels)
+            %sOut = shift(sObj, deltaT
+            %returns a SignalObj sOut shifted by deltaT. If deltaT is positive
             %the SignalObj is moved deltaT time steps forward
             %if deltaT is negative, the SignalObj is moved deltaT units
             %backwards.
+            if(nargin<3)
+                updateLabels =0;
+            end
+
             %compute number of samples delayed
             sOut=sObj.copySignal;
-            newMinTime = sOut.minTime+deltaT;
-            newMaxTime = sOut.maxTime+deltaT;
-            newTime=newMinTime:1/sOut.sampleRate:newMaxTime;
-            sOut.time = newTime;
-            sOut.minTime = newMinTime;
-            sOut.maxTime = newMaxTime;
-        end
-        function shiftMe(sObj,deltaT)
-            sTemp = sObj.shift(deltaT);
+            if(deltaT~=0)
+                newMinTime = sOut.minTime+deltaT;
+                newMaxTime = sOut.maxTime+deltaT;
+                newTime = sOut.time + deltaT;
+%                 newTime=newMinTime:1/sOut.sampleRate:newMaxTime;
+                sOut.time = newTime;
+                sOut.minTime = newMinTime;
+                sOut.maxTime = newMaxTime;
+                if(updateLabels)
+                    dataLabels = strcat(sObj.dataLabels,'(t-',num2str(deltaT),')');
+                    sOut.setName(strcat(sObj.name,'(t-',num2str(deltaT),')'));
+                    sOut.setDataLabels(dataLabels);
+                end
+
+            end
+        end
+        function shiftMe(sObj,deltaT,updateLabels)
+            %shiftMe(sObj,deltaT)
+            % same as shift(sObj,deltaT) except that no signal is return.
+            % The shift is done to sObj.
+            if(nargin<3)
+                updateLabels=0;
+            end
+            sTemp = sObj.shift(deltaT,updateLabels);
             sObj.data=sTemp.data;
             sObj.time=sTemp.time;
         end
@@ -975,6 +1590,9 @@
             end
         end
         function answer = plotPropsSet(sObj)
+            %answer = plotPropsSet(sObj)
+            %returns 1 if any of the plotting properties has been set. Else
+            %0.
             answer =0;
             for i=1:sObj.dimension
                 if(~strcmp(sObj.getPlotProps(i),''))
@@ -984,6 +1602,8 @@
             end
         end
         function answer = areDataLabelsEmpty(sObj)
+            %answer = areDataLabelsEmpty(sObj)
+            %returns 0 is dataLabels are empty and 1 otherwise
             answer = 1;
             for i=1:length(sObj.dataLabels);
                 if(~strcmp(sObj.dataLabels{i},''))
@@ -993,6 +1613,8 @@
             end
         end
         function answer = isLabelPresent(sObj, label)
+            %answer = isLabelPresent(sObj, label)
+            %returns 1 if label is present in sObj, and 0 otherwise
             if(isa(label,'char'))
                 if(strcmp(label,'all')||~isempty(sObj.getIndexFromLabel(label)))
                     answer=1;
@@ -1004,9 +1626,14 @@
             end
         end
         function answer = isMaskSet(sObj)
-            answer=~isempty(sObj.dataMask==0);
+            %answer = isMaskSet(sObj)
+            %returns 0 is mask is not set, 1 if it is.
+            answer=any(sObj.dataMask==0);
         end
         function sArray = convertNamesToIndices(sObj, selectorArray)
+            %sArray = convertNamesToIndices(sObj, selectorArray)
+            %converts the names in selectorArray to a vector of indices.
+
             if(sObj.areDataLabelsEmpty)
                 sArray = 1:sObj.dimension; % Return all the data;
                 fprintf('tried to find data by labels but data doesnot have labels assigned');
@@ -1034,29 +1661,68 @@
                 end
             end
         end
-        function [indices, values] = findPeaks(sObj,type)
+        function [sAligned,meanTime] = alignToMax(sObj)
+
+            [indices,values] = sObj.findGlobalPeak('maxima');
+            meanTime = mean(indices);
+            deltaT = -(indices-meanTime); % if index is greater than mean index then deltaT is negative so that we can align it witht the mean
+
+            for i=1:sObj.dimension
+               if(i==1)
+                   sAligned = sObj.getSubSignal(i).shift(deltaT(i));
+               else
+                   sAligned = sAligned.merge(sObj.getSubSignal(i).shift(deltaT(i)));
+               end
+            end
+        end
+        function [ind, val] = findGlobalPeak(sObj,type)
+           if(nargin<2)
+               type='maxima';
+           end
+           if(strcmp(type,'maxima'))
+               [val,index] = max(sObj.data);
+               ind = sObj.time(index);
+           elseif(strcmp(type,'minima'))
+               [val,index] = min(sOBj.data);
+               ind = sObj.time(index);
+           end
+        end
+        function [indices, values] = findPeaks(sObj,type,minDistance)
+            %[indices, values] = findPeaks(sObj,type)
+            %type:'minima' or 'maxima'
+            %indices: indices at which the minima or maxima occur
+            %values: values at the minima or maxima
+            if(nargin<3)
+                minDistance = round(sObj.sampleRate*(sObj.maxTime-sObj.minTime)/10);
+            end
             if(nargin<2)
                 type='maxima';
             end
             values=cell(1,sObj.dimension);
             indices=cell(1,sObj.dimension);
             if(strcmp(type,'maxima'))
-                for i=1:w
-                    [values{i},indices{i}] = findPeaks(sObj.data(:,i));
+                for i=1:sObj.dimension
+                    [values{i},indices{i}] = findpeaks(sObj.data(:,i),'MINPEAKDISTANCE',minDistance);
                 end
             elseif(strcmp(type,'minima'))
-                for i=1:w
-                    [values{i},indices{i}] = findPeaks(sObj.data(:,i));
+                for i=1:sObj.dimension
+                    [values{i},indices{i}] = findpeaks(sObj.data(:,i),'MINPEAKDISTANCE',minDistance);
                 end
             end
         end
+
         function [indices, values] = findMaxima(sObj)
+            %Same as findPeaks(sObj,'maxima');
             [indices,values]=sObj.findPeaks('maxima');
         end
         function [indices, values] = findMinima(sObj)
-            [indices,times,values]= sObj.findPeaks('minima');
+            %Same as findPeaks(sObj,'minima');
+            [indices,values]= sObj.findPeaks('minima');
         end
         function clearPlotProps(sObj,index)
+            %clearPlotProps(sObj,index)
+            %clear the plotProps{index} if index is specified.
+            %Otherwise all plotProps are cleared.
             if(nargin<2)
                 index=1:sObj.dimension;
             end
@@ -1066,25 +1732,47 @@
             end
 
         end
-        function v=get.vars(sObj)
-            if(~sObj.areDataLabelsEmpty)
-                UniqueSigLabels = unique(sObj.dataLabels);
-
-                for i=1:length(UniqueSigLabels)
-                    eval(strcat('v.(''',UniqueSigLabels{i},''')=sObj.getSubSignal(''',UniqueSigLabels{i},''');'));
-                end
-            %sObj.vars=v;
-            else
-                for i=1:sObj.dimension
-
-                    eval(strcat('v.(''',sObj.dataLabels{i},''')=sObj.getSubSignal(i);'))
-                end
-            end
-        end
+%         function v=get.vars(sObj)
+%             %returns a structure with fieldnames for each dataLabel
+%             %eg: if s has components x and y. v will have two fields: v.x
+%             %and v.y .
+%             %Accessing v.x returns a SignalObj containing just the data
+%             %corresponding to the label x.
+%             v=[];
+%             if(~sObj.areDataLabelsEmpty)
+%                 UniqueSigLabels = unique(sObj.dataLabels);
+%
+%                 for i=1:length(UniqueSigLabels)
+%                     if(~isempty(UniqueSigLabels{i}))
+%                         K=strfind(UniqueSigLabels{i},'\');
+%                         actString = UniqueSigLabels{i}(K+1:end);
+%                         eval(strcat('v.(''',actString,''')=sObj.getSubSignal(''',UniqueSigLabels{i},''');'))
+%                     end
+%
+%                 end
+%             %sObj.vars=v;
+%             else
+%                 for i=1:sObj.dimension
+%                     if(~isempty(sObj.dataLabels{i}))
+%                         eval(strcat('v.(''',sObj.dataLabels{i},''')=sObj.getSubSignal(i);'))
+%                     end
+%                 end
+%             end
+%         end
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         %Change of Representation Functions
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         function structure=dataToStructure(sObj,selectorArray)
+            %structure=dataToStructure(sObj)
+            %return unmasked data in sObj as a standard matlab structure
+            %with fields:
+            %structure.time
+            %structure.signals.values
+            %
+            %structure=dataToStructure(sObj,selectorArray)
+            %returns a structure containing only the components specified
+            %by the selectorArray
+
             if(nargin<2)
                 if(sObj.isMaskSet)
                     selectorArray=sObj.findIndFromDataMask;
@@ -1095,8 +1783,35 @@
             %Convert to a standard matlab structure
             structure.time=sObj.time;
             structure.signals.values=sObj.dataToMatrix(selectorArray);
+            structure.name        = sObj.name;
+            structure.dimension   = min(sObj.dimension,length(selectorArray));
+            structure.signals.dimensions = min(sObj.dimension,length(selectorArray));
+            structure.minTime     = sObj.minTime;
+            structure.maxTime     = sObj.maxTime;
+            structure.xlabelval   = sObj.xlabelval;
+            structure.xunits      = sObj.xunits;
+            structure.yunits      = sObj.yunits;
+            if(isa(sObj.dataLabels,'char'))
+                structure.dataLabels  = sObj.dataLabels;
+            elseif(isa(sObj.dataLabels,'cell'))
+                structure.dataLabels  = sObj.dataLabels(selectorArray);
+            else
+                structure.dataLabels = [];
+            end
+            structure.dataMask    = sObj.dataMask(selectorArray);
+            structure.sampleRate  = sObj.sampleRate;
+            structure.plotProps   = sObj.plotProps(selectorArray);
         end
         function dataMat=dataToMatrix(sObj,selectorArray)
+            %dataMat=dataToMatrix(sObj)
+            %returns the data for the visible components (i.e. those not
+            %masked out, in a nxm matrix where n is the determined by the
+            %sampleRate and the length of the current time vector, and m is
+            %the number of unmasked dimensions.
+            %
+            %dataMat=dataToMatrix(sObj,selectorArray)
+            %returns a matrix containing only the components specified
+            %by the selectorArray
             if(nargin<2)
                 if(sObj.isMaskSet)
                     selectorArray=sObj.findIndFromDataMask;
@@ -1109,6 +1824,12 @@
             dataMat=sObj.data(:,selectorArray);
         end
         function sOut = getSubSignal(sObj,identifier)
+            %sOut = getSubSignal(sObj,identifier)
+            %Returns a new signal that consists of only the data
+            %correspnding to the identifier. The identifier can be a string specifying the name
+            %of the component, a cell array of strings specifying various
+            %components, a vector specifying the indices of the components
+            %of interest.
             if(isa(identifier,'cell'))
                 if(isa(identifier{1},'char'))
                     sOut = sObj.getSubSignalFromNames(identifier);
@@ -1117,91 +1838,176 @@
                 else
                     error('Cells must contain strings!');
                 end
+            elseif(isa(identifier,'char'))
+                sOut = sObj.getSubSignalFromNames(identifier);
             elseif(isa(identifier,'double'))
                 sOut = sObj.getSubSignalFromInd(identifier);
             end
         end
-        function wSignal = getSigInTimeWindow(sObj,wMin,wMax)
-            wSignal = sObj.copySignal;
-            if(wMin<wSignal.minTime)
-                %if the window starts before the SignalObj pad with zeros
-                %and set as new starttime
-                wSignal.setMinTime(wMin);
-            end
-            if(wMax>wSignal.maxTime)
-                %if wMax is too big, pad with zeros at end and set as new
-                %end
-                wSignal.setMaxTime(wMax);
-            end
-
-            startIndex= wSignal.findNearestTimeIndex(wMin);
-            endIndex  = wSignal.findNearestTimeIndex(wMax);
-            wSignal.time=wSignal.time(startIndex:endIndex); wSignal.data=wSignal.data(startIndex:endIndex,:);
-            wSignal.setMinTime;
-            wSignal.setMaxTime;
-        end
-        function sOut = getSubSignalFromInd(sObj, selectorArray)
-            if(nargin<2)
-                if(sObj.isDataMaskSet)
-                    selectorArray=sObj.findIndFromDataMask;
+        function sOut = normWindowedSignal(sObj,windowTimes,numPoints,lbound,ubound)
+            %Use the lower and upper bounds to specify the smallest and
+            %largest window sizes that are allowed. Windows smaller or
+            %larger than these will be ignored.
+            if(nargin<5)
+                if(nargin>4)
+                    ubound = lbound;
                 else
-                    selectorArray=1:sObj.dimension;
+                    ubound = [];
                 end
+
             end
-            s3   = sObj.copySignal;
-            time = s3.time;
-            if(isa(selectorArray,'cell'))
-                data=[];
-               for i=1:length(selectorArray)
-                   offset=0;
-                   data = [data s3.data(:,selectorArray{i})];
-                   for j=1:length(selectorArray{i})
-                        dataLabels{offset + j} = s3.dataLabels{selectorArray{i}(j)};
-                        if(~isempty(s3.plotProps))
-                            plotProps{offset+ j} = s3.plotProps{selectorArray{i}(j)};
-                        end
-                   end
-                   offset = offset + length(selectorArray{i});
-               end
-            else
-                data = s3.data(:,selectorArray);
-                dataLabels = cell(1,length(selectorArray));
-                plotProps  = [];
-                for i=1:length(selectorArray)
-                    dataLabels{i}= s3.dataLabels{selectorArray(i)};
-                    if(~isempty(s3.plotProps))
-                        plotProps{i} = s3.plotProps{selectorArray(i)};
-                    end
-                end
+            if(nargin<4 || isempty(lbound));
+                lbound=[];
+            end
+            if(nargin<3 || isempty(numPoints))
+                numPoints =100;
             end
 
+          if(sObj.dimension == 1)
+              data=[];
+              for i=1:length(windowTimes)-1
 
+                minTime = windowTimes(i);
+                maxTime = windowTimes(i+1);
+                if(and(~isempty(lbound),~isempty(ubound)))
+                    if(and(abs(maxTime-minTime)<=(ubound),abs(maxTime-minTime)>=(lbound)))
+                        dim=size(data,2);
+                        time = linspace(minTime,maxTime,numPoints);
+                        actSig= sObj.getSigInTimeWindow(minTime, maxTime);
+%                         data(:,dim+1) = interp1(actSig.time,actSig.data,time,'spline')';
 
+                        data(:,dim+1) = interp1(actSig.time,actSig.data,time,'nearest',0)';
+                    end
+                else
+                   time = linspace(minTime,maxTime,numPoints);
+                   actSig= sObj.getSigInTimeWindow(minTime, maxTime);
+%                    data(:,i) = interp1(actSig.time,actSig.data,time,'spline')';
+                    data(:,i) = interp1(actSig.time,actSig.data,time,'nearest',0)';
+                end
+              end
+                  actTime = (0:1:numPoints-1)./numPoints; %time -time(1);
+                  name = sObj.name;
+                  xlabelval = sObj.xlabelval;
+                  xunits = '%';
+                  yunits = sObj.yunits;
+                  dataLabels(1:size(data,2)) =sObj.dataLabels;
+                  %plotProps = sObj.plotProps;
+%                   if(i==1)
+                        evalstring = strcat('sOut=',class(sObj),'(actTime, data,name, xlabelval, xunits, yunits,dataLabels);');
+                        eval(evalstring);
+%                         sOut.setMinTime(0);
+%                     else
+%                         evalstring = strcat('sOut = sOut.merge(',class(sObj),'(actTime, data,name, xlabelval, xunits, yunits,dataLabels,plotProps));');
+%                         eval(evalstring);
+%                     end
 
-            name = s3.name;
-            xlabelval = s3.xlabelval; xunits=s3.xunits; yunits = s3.yunits;
-            sOut=SignalObj(time, data, name, xlabelval, xunits, yunits, dataLabels, plotProps);
+          end
+        end
+
+        function sOut = windowedSignal(sObj,windowTimes)
+            if(sObj.dimension == 1)
+                for i=1:length(windowTimes)-1
+                    if(i==1)
+                        sOut = sObj.getSigInTimeWindow(windowTimes(1), windowTimes(2));
+                    else
+                        temp =sObj.getSigInTimeWindow(windowTimes(i), windowTimes(i+1));
+                        temp = temp.shift(-windowTimes(i));
+                        sOut=sOut.merge(temp);
+                    end
+                end
 
+            end
         end
-        function sOut = getSubSignalFromNames(sObj,labels)
-            ind   = sObj.getIndicesFromLabels(labels);
-            if(isa(ind,'cell'))
-                if(length(ind)>1)
-                    for i=1:length(ind)
-                        s{i} = getSubSignalFromInd(ind{i});
+        function wSignals = getSigInTimeWindow(sObj,wMin,wMax,holdVals)
+            %wSignal = getSigInTimeWindow(sObj)
+            %returns a copy of sObj from t0=sObj.minTime to tf=sObj.maxTime;
+            %wSignal = getSigInTimeWindow(sObj,wMin)
+            %returns a new signal starting at t0=wMin to tf=sObj.maxTime
+            %
+            %wSignal = getSigInTimeWindow(sObj,wMin,wMax)
+            %returns a new signal starting at t0=wMin to tf=wMax;
+            %
+            %wSignal = getSigInTimeWindow(sObj,wMin,wMax,holdVals)
+            %if holdVals =1 and wMin or wMax are outside the bounds of the
+            %current SignalObj time vector, then the endpoint values are
+            %held. Otherwise, this region is padded with zeros;
+            if(nargin<4)
+                holdVals =0;
+            end
+            if(nargin<3)
+                wMax=sObj.maxTime;
+            end
+            if(nargin<2)
+                wMin=sObj.minTime;
+            end
+
+            if(length(wMin)~=length(wMax))
+                error('Window minTimes must contain the same number of elements as window maxTimes');
+            end
+            if(length(wMin)==1 && sObj.minTime==wMin && sObj.maxTime==wMax)
+                wSignals = sObj.copySignal;
+            else
+                for i=1:length(wMin)
+                    wSignal = sObj.copySignal;
+                    if(wMin(i)<wSignal.minTime)
+                        %if the window starts before the SignalObj pad with zeros
+                        %and set as new starttime
+                        wSignal.setMinTime(wMin(i),holdVals);
+                    end
+                    if(wMax(i)>wSignal.maxTime)
+                        %if wMax is too big, pad with zeros at end and set as new
+                        %end
+                        wSignal.setMaxTime(wMax(i),holdVals);
+                    end
+
+                    startIndex= wSignal.findNearestTimeIndex(wMin(i));
+                    endIndex  = wSignal.findNearestTimeIndex(wMax(i));
+                    wSignal.time=wSignal.time(startIndex:endIndex);
+                    wSignal.data=wSignal.data(startIndex:endIndex,:);
+                    if(length(wMin)>1)
+                        for j=1:wSignal.dimension
+                            dataLabels{j} = strcat(wSignal.dataLabels{j},'_{',num2str(i),'}');
+                        end
+                    else
+                        if(wSignal.dimension==1)
+                            dataLabels = wSignal.dataLabels;
+                        else
+                            for j=1:wSignal.dimension
+                                dataLabels{j} = wSignal.dataLabels{j};
+                            end
+                        end
+                    end
+                    wSignal.setDataLabels(dataLabels);
+                    wSignal.setMinTime;
+                    wSignal.setMaxTime;
+                    if(i==1)
+                        wSignals = wSignal;
+                    else
+                        wSignals = wSignals.merge(wSignal);
                     end
-                    sOut = s{1}.merge(s{2:end});
-                else
-                    sOut = sObj.getSubSignalFromInd(ind{1});
                 end
-            elseif(isa(ind,'double'))
-                sOut  = sObj.getSubSignalFromInd(ind);
             end
         end
 
+        function [s sigIndex]=getSubSignalsWithinNStd(sObj,nStd)
+            mSig=mean(sObj.data,2);
+            stdSig = std(sObj.data,0,2);
+            minVal = mSig-nStd*stdSig;
+            maxVal = mSig+nStd*stdSig;
+            indMin= find(sum(sObj.data-minVal*ones(1,sObj.dimension)<0)==0);
+            indMax=find(sum(sObj.data-maxVal*ones(1,sObj.dimension)>0)==0);
+            sigIndex = intersect(indMin,indMax);
+            s=sObj.getSubSignal(sigIndex);
+
+        end
+
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         %Plotting Functions
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+        function h=plot(sObj,selectorArray,plotPropsIn,handle)
+
+        %   h=plot(sObj,selectorArray,plotProps,handle)
         %   plotProps: is a cell array with strings in each a entry.
         %              The strings must evaluate to parameters that can be
         %              passed to the standard matlab plot function.
@@ -1218,16 +2024,20 @@
         %                   elements within the SignalObj data or a cell array
         %                   of characters that refers to the data elements
         %                   by their labels.
+        %   handle: is figure handle to tell the signal where to plot
+        %           itself.
         % % 8-25-09 : Legend wont plot is SignalObj dimension >10;
-        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-        function h=plot(sObj,selectorArray,plotProps,handle)
-            if(nargin<4)
-                handle=gca;
-            end
-           if(nargin<3)
-              plotProps=sObj.plotProps;
+
+
+
+           if(nargin<4)
+              handle=gca;
            end
-           if(nargin<2)
+           if((nargin<3) || isempty(plotPropsIn))
+              %plotPropsIn=[];
+              plotPropsIn=sObj.plotProps;
+           end
+           if((nargin<2) || isempty(selectorArray))
                 if(sObj.isMaskSet)
                     selectorArray=sObj.findIndFromDataMask;
                     if(isempty(selectorArray))
@@ -1240,6 +2050,7 @@
            end
 
 
+
             if(isa(selectorArray,'cell'))
                 sArray = sObj.convertNamesToIndices(selectorArray);
             elseif(isa(selectorArray,'char'))
@@ -1251,14 +2062,14 @@
             end
 
 
-
-           if(length(plotProps)==length(sArray))
+           % if plot params are passed in then assign them to the SignalObj
+           if(length(plotPropsIn)==length(sArray))
                 for i=1:length(sArray)
-                    sObj.setPlotProps(plotProps{i},sArray(i));
+                    sObj.setPlotProps(plotPropsIn{i},sArray(i));
                 end
-           elseif(length(plotProps)<=length(sArray) && length(plotProps)==1)
+           elseif(length(plotPropsIn)<=length(sArray) && length(plotPropsIn)==1)
                for i=1:length(sArray)
-                    sObj.setPlotProps(plotProps,sArray(i));
+                    sObj.setPlotProps(plotPropsIn,sArray(i));
                end
            end
 
@@ -1268,8 +2079,8 @@
             %If the dimensions of the data are larger than one can specify
             %the plot style for each dimension by placing their values in
             %an cell array.
-            h=gcf;figure(h);
-            if(nargin>=3)  %is we got called with parameter use those
+%             h=gcf;figure(h);
+            if((nargin>=3) && ~isempty(plotPropsIn))  %is we got called with parameter use those
                 %sObj.setPlotProps(plotProps); %Only accepts entire cell of plotting properties as input
                 %if(length(plotCell)>=length(sArray)) %if we specified plot parameters for each dimension
                 for i=sArray
@@ -1277,11 +2088,16 @@
                     plotStr=cell2str(sObj.getPlotProps(i));
                     if(~strcmp(plotStr,'') && ~isempty(plotStr))
                         evalstring = strcat('h(',num2str(find(sArray==i)),')=plot(handle,sObj.time,sObj.data(:,',num2str(i),'),', plotStr,');');
+                        eval(evalstring); hold on;
                     else
-                        evalstring = strcat('h(',num2str(find(sArray==i)),')=plot(handle,sObj.time,sObj.data(:,',num2str(i),'));');
+%                         set(gca,handle);
+                        axes(handle);
+                        evalstring = strcat('h(',num2str(find(sArray==i)),')=plot(sObj.time,sObj.data(:,',num2str(i),'));');
+%                         evalstring = strcat('h(',num2str(find(sArray==i)),')=plot(handle,sObj.time,sObj.data(:,',num2str(i),'));');
+                        eval(evalstring); hold on;
                     end
                     %evalstring
-                    eval(evalstring); hold on;
+                    %eval(evalstring); hold on;
                 end
             elseif(sObj.plotPropsSet) %% We use the values that have been set previously
                 %plotCell = cell2str(sObj.getPlotProps(i));
@@ -1295,14 +2111,26 @@
                         evalstring = strcat('h(',num2str(i),')=plot(handle,sObj.time,sObj.data(:,',num2str(i),'));');
                     end
                     %evalstring
-                    eval(evalstring); hold on;
+                    if(~isempty(sObj.data(:,i)))
+                        eval(evalstring); hold on;
+                    end
                 end
             else %We didnt get any plotting properties. Use matlab default
                 %plotCell{1} = '''';
                 %fprintf('varargin is empty')
+                set(gcf,'CurrentAxes',handle);
                 h=plot(handle,sObj.time,sObj.data(:,sArray));
             end
 
+                sObj.setupPlots(handle,sArray);
+        end
+        function setupPlots(sObj,handle,sArray)
+            %setupPlots(sObj,sArray)
+            %Sets the labels for the x-axis, y-axis, and legend.
+            %sArray is an array on indexes corresponding to which labels
+            %will appear in the legend.
+%              set(gcf,'CurrentAxes',handle);
+            warning off;
             if(~strcmp(sObj.xunits,''))
                 xunitsStr=strcat('\; [',sObj.xunits,']'); %\; is a large space in latex
             else
@@ -1336,164 +2164,400 @@
                 end
             end
 
-            if(~sObj.areDataLabelsEmpty && sObj.dimension<10)
-                labelArray= cell(1,length(sArray));
-                for i=1:length(sArray)
-                    labelArray{i} = strcat('$$',sObj.dataLabels{sArray(i)},'$$');
-
+            if(~sObj.areDataLabelsEmpty) %%&& sObj.dimension<10)
+                rawLabels = sObj.dataLabels(sArray);
+                labelArray = cell(1,length(rawLabels));
+                for i=1:length(rawLabels)
+                    currLabel = rawLabels{i};
+                    while iscell(currLabel) && numel(currLabel)==1
+                        currLabel = currLabel{1};
+                    end
+                    if isstring(currLabel)
+                        currLabel = char(currLabel);
+                    elseif isnumeric(currLabel) || islogical(currLabel)
+                        currLabel = num2str(currLabel);
+                    elseif ~ischar(currLabel)
+                        currLabel = '';
+                    end
+                    if isempty(currLabel)
+                        currLabel = sprintf('Signal %d',sArray(i));
+                    end
+                    labelArray{i} = currLabel;
                 end
-                legend(labelArray,'Interpreter','latex');
+                legend(handle,labelArray);%,'Interpreter','latex');
             end
             axis tight;
+            warning on;
         end
 
-        function h=plotVariability(sObj,linewidth,faceColor,ciUpper,ciLower)
-            if(nargin<3)
-                faceColor='red';
-            end
-            if(nargin<3)
-                linewidth=3;
-            end
+        function h=plotVariability(sObj,selectorArray)
+            %h=plotVariability(sObj)
+            %Calls plotAllVariability(sObj) for each set of unique labels
+            %in the SignalObj. For example, if a SignalObj s has for
+            %components (2 with label 'x', and 2 with label 'y'), two
+            %figures will be generated, one for the variability of the x
+            %component and one for the variability of the y component. If
+            %all components have the same label, then only one plot will be
+            %generated. Plot handles are returned in h.
+            %
+            %h=plotVariability(sObj,selectorArray)
+            %if selectorArray is specified, then plotAllVariability is
+            %called once on a SignalObj that only has the components
+            %specified by the selectorArray.
             if(nargin<2)
-                if(sObj.isMaskSet)
-                    selectorArray=sObj.findIndFromDataMask;
-                    if(isempty(selectorArray))
-                        h=[];
-                        return;
+                if(~sObj.areDataLabelsEmpty)
+                    uLabels =unique(sObj.dataLabels);
+                    for i=1:length(uLabels)
+                        selectorArray{i} = sObj.getIndicesFromLabels(uLabels{i});
                     end
                 else
-                    selectorArray=1:sObj.dimension;
+                    selectorArray = 1:sObj.dimension;
+                end
+            end
+            if(isa(selectorArray,'cell')) %More than component has the same
+                [numSubSignals]=length(selectorArray);
+                for i=1:numSubSignals
+                    %figure;
+                    h(i)=sObj.getSubSignal(selectorArray{i}).plotAllVariability(getAvailableColor(i));
                 end
+            elseif(isa(selectorArray,'double'))
+                h=sObj.getSubSignal(selectorArray).plotAllVariability;
             end
+        end
 
+        function h=plotAllVariability(sObj,faceColor,linewidth,ciUpper,ciLower)
+            %h=plotAllVariability(sObj)
+            % Computes the mean across all components in the signal and
+            % plots +/- 1 standard deviation from the mean. All other
+            % parameters are optional.
+            %
+            % faceColor: defaults to red if not specified.
+            % linewidth: defaults to 3 if not specified.
+            %
+            % ciUpper: if a single number then specifies the multiple of
+            %          standard deviations to be used. i.e. ciUpper=1
+            %          results in default. If ciUpper is an double array of
+            %          the same length as the signal, then the upper bound
+            %          of the confidence interval at time t will be
+            %          ciUpper+mean(sObj). If ciUpper is a SignalObj, then
+            %          the behavior is the same as if a double array were
+            %          specified. If only ciUpper is specified, ciLower is
+            %          assigned the same maginitude
+            % ciLower: if a single number then specifies the multiple of
+            %          standard deviations to be used. i.e. ciLower=1
+            %          results in default. If ciLower is an double array of
+            %          the same length as the signal, then the lower bound
+            %          of the confidence interval at time t will be
+            %          mean(sObj)-ciLower. If ciLower is a SignalObj, then
+            %          the behavior is the same as if a double array were
+            %          specified.
+            if(nargin<4 || isempty(ciUpper))
+                ciUpper =1.96;
+            end
+            if(nargin<=4)
+                ciLower=ciUpper;
+            end
+            if(nargin<2 || isempty(faceColor))
+                faceColor=getAvailableColor(1);
+            end
+            if(nargin<3 || isempty(linewidth))
+                linewidth=3;
+            end
+%              sObj=s;
+            meanSig = mean(sObj,2);
+            stdSig  = std(sObj,[],2);
 
-            if(isa(selectorArray,'cell'))
-                sArray = sObj.convertNamesToIndices(selectorArray);
-            elseif(isa(selectorArray,'char'))
-                sArray = sObj.convertNamesToIndices(selectorArray);
-            elseif(isa(selectorArray,'double'))
-                sArray = selectorArray;
+            if(length(ciUpper)==1)
+                ciTop=meanSig+ciUpper.*stdSig;
+            elseif(length(ciUpper)==length(sObj.time))
+                ciTop=meanSig+ciUpper;
             else
-                error('selectorArray must contain either dataLabels or their indices');
+                error('Upper confidence interval must be either length 1 or same length as the time vector');
             end
 
-            %Take the unique dataLabels and compute the variability of
-            %these signals
+            if(length(ciLower)==1)
+                ciBottom=meanSig-ciLower.*stdSig;
+            elseif(length(ciUpper)==length(sObj.time))
+                ciBottom=meanSig-ciLower;
+            else
+                error('Lower confidence interval must be either length 1 or same length as the time vector');
+            end
 
-            UniqueSigLabels = unique(sObj.getSubSignal(sArray).dataLabels);
+            ci2=ciTop.dataToMatrix;
+            ci1=ciBottom.dataToMatrix;
 
 
-            for i=1:length(UniqueSigLabels)
-                meanSig = mean(sObj.getSubSignal(UniqueSigLabels{i}),2);
 
-                if(nargin<4)
-                    ci2=meanSig.data+std(meanSig.dataToMatrix);
-                else
-                    ci2=meanSig.data+ciUpper;
-                end
+            p=patch([reshape(sObj.time,[1 length(sObj.time)]) reshape(fliplr(sObj.time'),[1 length(sObj.time)])],...
+                    [reshape(ci1',[1 length(sObj.time)])       reshape(fliplr(ci2'),[1,length(sObj.time)])],strcat('',faceColor,''));
+            set(p,'facecolor',faceColor,'edgecolor','none');
+            alpha(.5);hold on;
+            h=plot(sObj.time,meanSig.dataToMatrix,'k-','linewidth',linewidth);
 
-                if(nargin<5)
-                    ci1=meanSig.data-std(meanSig.dataToMatrix);
-                else
-                    ci1=meanSig.data+ciLower;
+            sArray=1; %only want the first label to plot;
+            meanSig.setupPlots(sArray);
+%             legend([h,p],'mean','variance');
+
+        end
+
+
+
+
+    end % public methods
+    methods (Access = private)
+        function index      = getIndexFromLabel(sObj,label)
+            %index      = getIndexFromLabel(sObj,label)
+            %Given a label, returns the index to the SignalObj component
+            %with the specified label
+            index=[];
+            for i=1:length(sObj.dataLabels)
+                if(strcmp(label, sObj.dataLabels{i}))
+                    index = [index i];
                 end
+            end
+        end
 
 
-                plot(sObj.time,meanSig.dataToMatrix,'k-','linewidth',linewidth);
-                hold on;
-                p=patch([reshape(sObj.time,[1 length(sObj.time)]) reshape(fliplr(sObj.time'),[1 length(sObj.time)])],...
-                        [reshape(ci1',[1 length(sObj.time)])       reshape(fliplr(ci2'),[1,length(sObj.time)])],strcat('',faceColor{1},''));
-                set(p,'facecolor',faceColor,'edgecolor','none');
-                alpha(.5);
+        function setDataMask(sObj, dataMask)
+        % setDataMask(sObj, dataMask)
+        % dataMask is a vector of ones and zeros of length sObj.dimension
+        % components marked by ones will be visible.i
+        %DataMasks affect how the SignalObj is visualized and converted to
+        %other representations. It doesnt change the dimensions of the
+        %object, nor does it delete data.
+            if(length(dataMask)==sObj.dimension)
+                  sObj.dataMask = dataMask;
             end
+
+        end
+        function setMaskByInd(sObj,index)
+            % setMaskByInd(sObj,index)
+            % if length(index)== sObj.dimension then the mask is set using
+            % the values in index.
+            % if length(index)< sObj.dimension only the component specified
+            % by the index will be visible. All others will be hidden.
+            if(length(index)==sObj.dimension)
+                sObj.setDataMask(index);
+            else
+                mask=zeros(1,sObj.dimension);
+                mask(index)=1;
+                sObj.setDataMask(mask);
+            end
+        end
+        function setMaskByLabels(sObj,labels)
+            %setMaskByLabels(sObj,labels)
+            %labels is a cell array with the labels of the components that
+            %are to remain visible. The indices for these labels are found
+            %and the mask set using setDataMask
+            ind=sObj.getIndicesFromLabels(labels);
+            mask=zeros(1,sObj.dimension);
+            mask(ind)=1;
+            sObj.setDataMask(mask);
         end
-    end % public methods
 
+        function sOut = getSubSignalFromInd(sObj, selectorArray)
+            if(nargin<2)
+                if(sObj.isDataMaskSet)
+                    selectorArray=sObj.findIndFromDataMask;
+                else
+                    selectorArray=1:sObj.dimension;
+                end
+            end
+            s3   = sObj.copySignal;
+            time = s3.time;
+            if(isa(selectorArray,'cell'))
+                data=[];
+               for i=1:length(selectorArray)
+                   offset=0;
+                   data = [data s3.data(:,selectorArray{i})];
+                   for j=1:length(selectorArray{i})
+                        dataLabels{offset + j} = s3.dataLabels{selectorArray{i}(j)};
+                        if(~isempty(s3.plotProps))
+                            plotProps{offset+ j} = s3.plotProps{selectorArray{i}(j)};
+                        end
+                   end
+                   offset = offset + length(selectorArray{i});
+               end
+            else
+                data = s3.data(:,selectorArray);
+                dataLabels = cell(1,length(selectorArray));
+                plotProps  = [];
+                for i=1:length(selectorArray)
+                    dataLabels{i}= s3.dataLabels{selectorArray(i)};
+                    if(~isempty(s3.plotProps))
+                        plotProps{i} = s3.plotProps{selectorArray(i)};
+                    end
+                end
+            end
 
 
 
-end%classdef
 
-% Helper function
-function stringout=cell2str(input)
-    if(isempty(input))
-        stringout = '';
-    else
-        if(isa(input,'char'))
+            name = s3.name;
+            xlabelval = s3.xlabelval; xunits=s3.xunits; yunits = s3.yunits;
+
+            %sOut=SignalObj(time, data, name, xlabelval, xunits, yunits, dataLabels, plotProps);
+            evalstring = strcat('sOut=',class(sObj),'(time, data,name, xlabelval, xunits, yunits,dataLabels,plotProps);');
+            eval(evalstring);
+        end
+        function sOut = getSubSignalFromNames(sObj,labels)
+            ind   = sObj.getIndicesFromLabels(labels);
+            if(isa(ind,'cell'))
+                if(length(ind)>1)
+                    for i=1:length(ind)
+                        s{i} = getSubSignalFromInd(ind{i});
+                    end
+                    sOut = s{1}.merge(s{2:end});
+                else
+                    sOut = sObj.getSubSignalFromInd(ind{1});
+                end
+            elseif(isa(ind,'double'))
+                sOut  = sObj.getSubSignalFromInd(ind);
+            end
+        end
+    end %private methods
+    methods (Static)
+        function sObj =signalFromStruct(structure)
+            fNames = {'time','signals','name','xlabelval','xunits','yunits','dataLabels','plotProps'};
+            structField = fields(structure);
+            for i=1:length(fNames)
+                if(~any(strcmp(structField,fNames{i})))
+                    error(['Field ' fNames{i} 'not present is structure! Needed to make a signal from a structure']);
+                end
+            end
+            sObj=SignalObj(structure.time, structure.signals.values, structure.name, structure.xlabelval, structure.xunits, structure.yunits, structure.dataLabels, structure.plotProps);
+            sObj.setMask(structure.dataMask);
+        end
+        function simpleStructure = convertSigStructureToStructure(sigStructure)
+           if(isa(sigStructure,'struct'))
+                fNames = fields(sigStructure);
+                for i=1:length(fNames)
+                    if(isa(sigStructure.(fNames{i}),'SignalObj'))
+                        simpleStructure.(fNames{i}) = sigStructure.(fNames{i}).dataToStructure;
+                    end
+                end
+           end
+        end
+        function sigStructure = convertSimpleStructureToSigStructure(simpleStructure)
+            if(isa(simpleStructure,'struct'))
+                fNames = fields(simpleStructure);
+                for i=1:length(fNames)
+                    if(isa(simpleStructure.(fNames{i}),'struct'))
+                        sigStructure.(fNames{i}) = SignalObj.signalFromStruct(simpleStructure.(fNames{i}));
+                    end
+                end
+           end
+        end
+
+
+
+    end
+end%classdef
+
+% Helper function
+function stringout=cell2str(input)
+    if(isempty(input))
+        stringout = '';
+    else
+        if(isa(input,'char'))
             stringout=input;
         elseif(isa(input,'cell'))
             stringout=cell2str(input{1});
         end
     end
 end
-
Input argument "time" is undefined.
-
-Error in ==> SignalObj>SignalObj.SignalObj at 96
-            [l,w]=size(time);
-
-
- - - \ No newline at end of file + + diff --git a/helpfiles/SignalObjExamples.html b/helpfiles/SignalObjExamples.html index 7fe798e..d2acc2d 100644 --- a/helpfiles/SignalObjExamples.html +++ b/helpfiles/SignalObjExamples.html @@ -1,91 +1,145 @@ - - - - - Using the SignalObj Class

Using the SignalObj Class

In this file we will give several examples of how the SignalObj can be used. A description of all of the properties of SignalObj can be found at: SignalObj Class Definition

Contents

Example 1: Defining and Plotting Signals

Define a two dimensional SignalObj, $s$, representing two voltage signals that were $v1$ and $v2$ aquired simultaneously at 100Hz. Another SignalObj, $s1$ , is created from just $v1$. Both signals are plotted.

close all;
+
+
+  
+
+
+
+

Using the SignalObj Class

+ +

In this file we will give several examples of how the SignalObj can be used. A description of all of the properties of SignalObj can be found at: SignalObj Class Definition +

+ +

Contents

+ +

Example 1: Defining and Plotting Signals

+

Define a two dimensional SignalObj, $s$, representing two voltage signals that were $v1$ and $v2$ aquired simultaneously at 100Hz. Another SignalObj, $s1$ , is created from just $v1$. Both signals are plotted.

+
close all;
 sampleRate=100; t=0:1/sampleRate:10; freq=2;
 v1=sin(2*pi*freq*t); v2=sin(v1.^2); v=[v1;v2];
 s=SignalObj(t,v,'Voltage','time','s','V',{'v1','v2'});
 s1=SignalObj(t,v1,'Voltage','time','s','V',{'v1'});
 subplot(2,1,1); s.plot;
 subplot(2,1,2); s1.plot;
-

Note how the legend show the appropriate labels. If the dataLabels had not been set, the legend would not appear.

Instead of defining a new SignalObj, $s1$, the $v2$ data in $s$ can be masked away and then the plot will only show the data of interest. It is important to note that setMask is used to set which signals should remain visible. Also note that when a data is masked, converting the SignalObj to a Matrix only returns the visible data. A new SignalObj can be created from the orignal SignalObj which only contains the data of interest. Lastly, the all labeled components can be accessed independently via the vars subfield.

subplot(2,1,1); s.setMask({'v1'}); s.plot; s.resetMask;
+
+

Note how the legend show the appropriate labels. If the dataLabels had not been set, the legend would not appear.

+

Instead of defining a new SignalObj, $s1$, the $v2$ data in $s$ can be masked away and then the plot will only show the data of interest. It is important to note that setMask is used to set which signals should remain visible. Also note that when a data is masked, converting the SignalObj to a Matrix only returns the visible data. A new SignalObj can be created from the orignal SignalObj which only contains the data of interest. Lastly, the all labeled components can be accessed independently via the vars subfield.

+
subplot(2,1,1); s.setMask({'v1'}); s.plot; s.resetMask;
 subplot(2,1,2); s.setMask({'v2'}); s.plot; size(s.dataToMatrix)
 s.resetMask;
-
+
+
 ans =
 
         1001           1
 
-

Note about repeated dataLabels It is possible to use SignalObj's to store different realizations of the same physical signal. For example, independent measurements of $v1$ at two distinct experiments.

s=SignalObj(t,[v1; v1; v2] ,'Voltage','time','s','V',{'v1','v1','v2'});
+
+

+Note about repeated dataLabels It is possible to use SignalObj's to store different realizations of the same physical signal. For example, independent measurements of $v1$ at two distinct experiments.

+
s=SignalObj(t,[v1; v1; v2] ,'Voltage','time','s','V',{'v1','v1','v2'});
 s.getSubSignal({'v1'}); %returns a SignalObj with both realizations of v1
 figure
 s.getSubSignal({'v1'}).plot;
-

Example 2: Changing Signal Properties

figure
+
+

Example 2: Changing Signal Properties

+
figure
 s=SignalObj(t,v,'Voltage','time','s','V',{'v1','v2'});
 subplot(2,1,1); s.plot;
 subplot(2,1,2); s.setXlabel('distance'); s.setXUnits('cm'); s.plot;
-
figure
+
+
figure
 subplot(2,1,1); s.setDataLabels({'r1','r2'}); s.setYLabel('Temperature'); s.setYUnits('C'); s.plot;
 subplot(2,1,2); s.setMaxTime(14); s.setMinTime(-2); s.plot;
 
@@ -96,23 +150,33 @@
     fprintf('Could not set name \n');
 end
 % s.name = 'testName'; %returns an error because the field is private;
-
Name successfully set 
-

setMaxTime and setMinTime can be given a second parameter, holdVals, that determines whether the endpoint values are kept or set to zero if the times being set are outside the original window of the data.

Plotting properties for individual components of the data can be set via the when plotting or by call the setPlotProps method.

figure
+
+
Name successfully set 
+
+

setMaxTime and setMinTime can be given a second parameter, holdVals, that determines whether the endpoint values are kept or set to zero if the times being set are outside the original window of the data.

+

Plotting properties for individual components of the data can be set via the when plotting or by call the setPlotProps method.

+
figure
 s=SignalObj(t,v,'Voltage','time','s','V',{'v1','v2'});
 subplot(2,1,1); s.plot('v1',{{' ''k'' '}});
 subplot(2,1,2); s.plot('all',{{' ''k'' '},{' ''-.g'' '}});
-
figure
+
+
figure
 subplot(2,1,1); s.plot({'v1','v2'});
 subplot(2,1,2); s.plot({'v1','v2'},{{' ''k'' '},{' ''-.g'' '}});
-

Example 3: Resampling and Windowing SignalObjs

figure
+
+

Example 3: Resampling and Windowing SignalObjs

+
figure
 s=SignalObj(t,v,'Voltage','time','s','V',{'v1','v2'});
 s1=s.resample(.1*sampleRate);
 subplot(2,1,1); s.plot;
 subplot(2,1,2); s1.plot;
-
figure
+
+
figure
 subplot(2,1,1); s.getSigInTimeWindow(-2,3).plot;
 subplot(2,1,2); s.plot;
-

Example 4: SignalObj Mathematical Operations

s=SignalObj(t,v,'Voltage','time','s','V',{'v1','v2'});
+
+

Example 4: SignalObj Mathematical Operations

+
s=SignalObj(t,v,'Voltage','time','s','V',{'v1','v2'});
 figure
 s2=mean(s); %mean of each dimension;
 s5=s-s2;  %zero mean version of s;
@@ -121,7 +185,9 @@
 figure
 s2=mean(s,2); %mean of s across its dimensions;
 s2.plot;
-

SignalObj's can be added, subtracted, and multiplied.\

figure
+
+

SignalObj's can be added, subtracted, and multiplied.\

+
figure
 s4=2*s+s5;
 s4.plot;
 
@@ -133,13 +199,18 @@
 subplot(3,1,3);
 s6=s.integral.derivative-s; %should equal zero;
 s6.plot;
-

Example 5: Spectra

s=SignalObj(t,v,'Voltage','time','s','V',{'v1','v2'});
+
+

Example 5: Spectra

+
s=SignalObj(t,v,'Voltage','time','s','V',{'v1','v2'});
 figure;
 s.MTMspectrum;
 
 figure
 s.periodogram;
-

Example 6: View signal variability

We can look at the variability of signals with regards to their labels or or irrespective of their labels. Lets create a SignalObj that has several components, and some noise. We mislabel two signals to add alot of variability for illustration

sampleRate=5000; t=0:1/sampleRate:1; t=t'; freq=2;
+
+

Example 6: View signal variability

+

We can look at the variability of signals with regards to their labels or or irrespective of their labels. Lets create a SignalObj that has several components, and some noise. We mislabel two signals to add alot of variability for illustration

+
sampleRate=5000; t=0:1/sampleRate:1; t=t'; freq=2;
 v1=sin(2*pi*freq*t); v2=sin(v1.^2);
 noise=.1*randn(length(t),6); %gaussian random noise
 data= [v1 v2 v2 v1 v2 v1] + noise;
@@ -148,15 +219,27 @@
 subplot(2,1,1); s.plot;
 subplot(2,1,2); s.plotAllVariability; %disregards labels;
 s.plotVariability; %creates two figures, one for 'v1' and one for 'v2'
-

sObj.plotAllVariability assumes that all the data can be considered together. It also allows more custimization of the plotting properties. Example we can specify a different faceColor by passing the color in the same format as plot. Assymetric confidence intervals can be specified. If the CIs are single numbers, they specify the multiple of the standard deviations to use (default is 1). If they are the same length as the SignalObj, then it specifies the actual values above and/or below the mean at each point in time. If ciLower is not specifed, it is assumed to equal ciUpper. For convinience the inputs to plotAllVariability are: s.plotAllVariability(sObj,faceColor,linewidth,ciUpper,ciLower)

figure;
+
+

sObj.plotAllVariability assumes that all the data can be considered together. It also allows more custimization of the plotting properties. Example we can specify a different faceColor by passing the color in the same format as plot. Assymetric confidence intervals can be specified. If the CIs are single numbers, they specify the multiple of the standard deviations to use (default is 1). If they are the same length as the SignalObj, then it specifies the actual values above and/or below the mean at each point in time. If ciLower is not specifed, it is assumed to equal ciUpper. For convinience the inputs to plotAllVariability are: s.plotAllVariability(sObj,faceColor,linewidth,ciUpper,ciLower)

+
figure;
 %blue color for CI patch;
 subplot(3,1,1); s.plotAllVariability('b');
 %green color and lineWidth=2;
 subplot(3,1,2); s.plotAllVariability('g',2);
 %cyan, lineWidth=3, 2*std for top CI, and 1*std for bottom CI
 subplot(3,1,3); s.plotAllVariability('c',3,2,1);
-
\ No newline at end of file +--> + + diff --git a/helpfiles/SignalObjExamples.png b/helpfiles/SignalObjExamples.png index 5cccdce..f028a14 100644 Binary files a/helpfiles/SignalObjExamples.png and b/helpfiles/SignalObjExamples.png differ diff --git a/helpfiles/SignalObjExamples_01.png b/helpfiles/SignalObjExamples_01.png index d9fdf71..c6d007e 100644 Binary files a/helpfiles/SignalObjExamples_01.png and b/helpfiles/SignalObjExamples_01.png differ diff --git a/helpfiles/SignalObjExamples_02.png b/helpfiles/SignalObjExamples_02.png index ec65ee5..dd73b8b 100644 Binary files a/helpfiles/SignalObjExamples_02.png and b/helpfiles/SignalObjExamples_02.png differ diff --git a/helpfiles/SignalObjExamples_03.png b/helpfiles/SignalObjExamples_03.png index 2bca294..b81f3c7 100644 Binary files a/helpfiles/SignalObjExamples_03.png and b/helpfiles/SignalObjExamples_03.png differ diff --git a/helpfiles/SignalObjExamples_04.png b/helpfiles/SignalObjExamples_04.png index fc15a7f..020accc 100644 Binary files a/helpfiles/SignalObjExamples_04.png and b/helpfiles/SignalObjExamples_04.png differ diff --git a/helpfiles/SignalObjExamples_05.png b/helpfiles/SignalObjExamples_05.png index 7f10bdd..401b355 100644 Binary files a/helpfiles/SignalObjExamples_05.png and b/helpfiles/SignalObjExamples_05.png differ diff --git a/helpfiles/SignalObjExamples_06.png b/helpfiles/SignalObjExamples_06.png index 8f18fbf..55f42cb 100644 Binary files a/helpfiles/SignalObjExamples_06.png and b/helpfiles/SignalObjExamples_06.png differ diff --git a/helpfiles/SignalObjExamples_07.png b/helpfiles/SignalObjExamples_07.png index 37cbd10..51b806f 100644 Binary files a/helpfiles/SignalObjExamples_07.png and b/helpfiles/SignalObjExamples_07.png differ diff --git a/helpfiles/SignalObjExamples_08.png b/helpfiles/SignalObjExamples_08.png index 1a169f0..3a78cc6 100644 Binary files a/helpfiles/SignalObjExamples_08.png and b/helpfiles/SignalObjExamples_08.png differ diff --git a/helpfiles/SignalObjExamples_09.png b/helpfiles/SignalObjExamples_09.png index 6e82619..d8b3d0a 100644 Binary files a/helpfiles/SignalObjExamples_09.png and b/helpfiles/SignalObjExamples_09.png differ diff --git a/helpfiles/SignalObjExamples_10.png b/helpfiles/SignalObjExamples_10.png index 6a5ff9f..a2cf7ea 100644 Binary files a/helpfiles/SignalObjExamples_10.png and b/helpfiles/SignalObjExamples_10.png differ diff --git a/helpfiles/SignalObjExamples_11.png b/helpfiles/SignalObjExamples_11.png index 033dba6..f871393 100644 Binary files a/helpfiles/SignalObjExamples_11.png and b/helpfiles/SignalObjExamples_11.png differ diff --git a/helpfiles/SignalObjExamples_12.png b/helpfiles/SignalObjExamples_12.png index 0193d3e..0eed5e3 100644 Binary files a/helpfiles/SignalObjExamples_12.png and b/helpfiles/SignalObjExamples_12.png differ diff --git a/helpfiles/SignalObjExamples_13.png b/helpfiles/SignalObjExamples_13.png index 6dd0e3a..90c87d5 100644 Binary files a/helpfiles/SignalObjExamples_13.png and b/helpfiles/SignalObjExamples_13.png differ diff --git a/helpfiles/SignalObjExamples_14.png b/helpfiles/SignalObjExamples_14.png index af99c75..877bf93 100644 Binary files a/helpfiles/SignalObjExamples_14.png and b/helpfiles/SignalObjExamples_14.png differ diff --git a/helpfiles/SignalObjExamples_15.png b/helpfiles/SignalObjExamples_15.png index b614e13..9527b0f 100644 Binary files a/helpfiles/SignalObjExamples_15.png and b/helpfiles/SignalObjExamples_15.png differ diff --git a/helpfiles/SignalObjExamples_16.png b/helpfiles/SignalObjExamples_16.png index d33e38a..2400580 100644 Binary files a/helpfiles/SignalObjExamples_16.png and b/helpfiles/SignalObjExamples_16.png differ diff --git a/helpfiles/SignalObjExamples_17.png b/helpfiles/SignalObjExamples_17.png index 9207998..ff11e5e 100644 Binary files a/helpfiles/SignalObjExamples_17.png and b/helpfiles/SignalObjExamples_17.png differ diff --git a/helpfiles/SignalObjExamples_18.png b/helpfiles/SignalObjExamples_18.png new file mode 100644 index 0000000..68a94e2 Binary files /dev/null and b/helpfiles/SignalObjExamples_18.png differ diff --git a/helpfiles/SignalObjExamples_19.png b/helpfiles/SignalObjExamples_19.png new file mode 100644 index 0000000..f9e39aa Binary files /dev/null and b/helpfiles/SignalObjExamples_19.png differ diff --git a/helpfiles/SignalObjExamples_20.png b/helpfiles/SignalObjExamples_20.png new file mode 100644 index 0000000..4b86ef9 Binary files /dev/null and b/helpfiles/SignalObjExamples_20.png differ diff --git a/helpfiles/SignalObjExamples_eq00266663809094582097.png b/helpfiles/SignalObjExamples_eq00266663809094582097.png new file mode 100644 index 0000000..fe62c28 Binary files /dev/null and b/helpfiles/SignalObjExamples_eq00266663809094582097.png differ diff --git a/helpfiles/SignalObjExamples_eq00878938188193217995.png b/helpfiles/SignalObjExamples_eq00878938188193217995.png new file mode 100644 index 0000000..340df77 Binary files /dev/null and b/helpfiles/SignalObjExamples_eq00878938188193217995.png differ diff --git a/helpfiles/SignalObjExamples_eq04799535687386240985.png b/helpfiles/SignalObjExamples_eq04799535687386240985.png new file mode 100644 index 0000000..13209f0 Binary files /dev/null and b/helpfiles/SignalObjExamples_eq04799535687386240985.png differ diff --git a/helpfiles/SignalObjExamples_eq06517260773231504700.png b/helpfiles/SignalObjExamples_eq06517260773231504700.png new file mode 100644 index 0000000..4eacb64 Binary files /dev/null and b/helpfiles/SignalObjExamples_eq06517260773231504700.png differ diff --git a/helpfiles/StimulusDecode2D.html b/helpfiles/StimulusDecode2D.html index 041984f..dac26c3 100644 --- a/helpfiles/StimulusDecode2D.html +++ b/helpfiles/StimulusDecode2D.html @@ -1,13 +1,18 @@ - - - - - StimulusDecode2D

Contents

2-D Stimulus Decode

Here we simulate hippocampal place cell receptive fields and their firing during a 2-d spatial task. We then use the ensemble firing activity to estimate the path based on the only the point process observations

delta = 0.001;
+  
+
+
+
+

Contents

+ +

2-D Stimulus Decode

+

Here we simulate hippocampal place cell receptive fields and their firing during a 2-d spatial task. We then use the ensemble firing activity to estimate the path based on the only the point process observations

+
delta = 0.001;
 Tmax = 1;
 time = 0:delta:Tmax;
 px = zeros(1,length(time));
@@ -87,7 +114,9 @@
 plot(px,py);
 title('Simulated X-Y trajectory');
 xlabel('x'); ylabel('y');
-

Generate random receptive fields to simulate different neurons

clear lambdaCIF lambda tempSpikeColl n spikeColl
+
+

Generate random receptive fields to simulate different neurons

+
clear lambdaCIF lambda tempSpikeColl n spikeColl
 numRealizations=80;
 
 coeffs = -abs(1*randn(numRealizations,5));
@@ -102,7 +131,15 @@
      n{i} = tempSpikeColl{i}.getNST(1);
      n{i}.setName(num2str(i));
 
-     lambdaCIF{i} = CIF(coeffs(i,:),{'1','x','y','x^2','y^2','x*y'},{'x','y'},'binomial');
+     try
+         lambdaCIF{i} = CIF(coeffs(i,:),{'1','x','y','x^2','y^2','x*y'},{'x','y'},'binomial');
+     catch ME_sym
+         if(i==1)
+             warning('StimulusDecode2D:SymbolicCIFFallback', ...
+                 ['CIF symbolic setup failed (' ME_sym.identifier '). Decoder will use linear fallback.']);
+         end
+         lambdaCIF{i} = [];
+     end
  end
 
 
@@ -139,20 +176,53 @@
     set(gca,'xtick',[],'ytick',[]);
 
 end
-

Decode the x-y trajectory

 spikeColl = nstColl(n);
+
+
Warning: CIF symbolic setup failed
+(symbolic:sym:sym:ArgumentMustBeVarnameOrNumber). Decoder will use linear
+fallback. 
+
+

Decode the x-y trajectory

+
 spikeColl = nstColl(n);
  spikeColl.resample(1/delta);
  dN = spikeColl.dataToMatrix;
-
vx=10*std(px(2:end)-px(1:end-1));
-vy=10*std(py(2:end)-py(1:end-1));
+
+
vx=var(px(2:end)-px(1:end-1));
+vy=var(py(2:end)-py(1:end-1));
 Q=[vx 0;0 vy];
 Px0=.1*eye(2,2); A=1*eye(2,2);
-% The PPDecodeFilter uses the matlab symbolic toolbox to evaluate the
-% gradient and hessian of the CIF. It is currently not working properly.
-[x_p, Pe_p, x_u, Pe_u] = DecodingAlgorithms.PPDecodeFilter(A, Q, Px0, dN',lambdaCIF,delta);
+decode_method = 'PPDecodeFilter';
+try
+    [x_p, Pe_p, x_u, Pe_u] = DecodingAlgorithms.PPDecodeFilter(A, Q, Px0, dN',lambdaCIF,delta);
+catch ME_decode
+    warning('StimulusDecode2D:SymbolicDecodeFallback', ...
+        ['PPDecodeFilter failed (' ME_decode.identifier '). Falling back to PPDecodeFilterLinear.']);
+    decode_method = 'PPDecodeFilterLinear';
+    mu_linear = coeffs(:,1);
+    beta_linear = coeffs(:,2:3)';
+    [x_p, Pe_p, x_u, Pe_u] = DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN', mu_linear, beta_linear, 'binomial', delta);
+end
+nCommon = min(length(px),size(x_u,2));
+decode_rmse = sqrt(mean((x_u(1,1:nCommon)'-px(1:nCommon)).^2 + (x_u(2,1:nCommon)'-py(1:nCommon)).^2));
+num_cells = numRealizations;
 figure;
 plot(x_u(1,:),x_u(2,:),'b',px,py,'k')
 legend('predicted path','actual path');
-
\ No newline at end of file +--> + + diff --git a/helpfiles/StimulusDecode2D.png b/helpfiles/StimulusDecode2D.png index 8ea1c05..0950146 100644 Binary files a/helpfiles/StimulusDecode2D.png and b/helpfiles/StimulusDecode2D.png differ diff --git a/helpfiles/StimulusDecode2D_01.png b/helpfiles/StimulusDecode2D_01.png index d06983b..6efa5ec 100644 Binary files a/helpfiles/StimulusDecode2D_01.png and b/helpfiles/StimulusDecode2D_01.png differ diff --git a/helpfiles/StimulusDecode2D_02.png b/helpfiles/StimulusDecode2D_02.png index e672204..cab1675 100644 Binary files a/helpfiles/StimulusDecode2D_02.png and b/helpfiles/StimulusDecode2D_02.png differ diff --git a/helpfiles/StimulusDecode2D_03.png b/helpfiles/StimulusDecode2D_03.png index 3ba2e5d..bfb0435 100644 Binary files a/helpfiles/StimulusDecode2D_03.png and b/helpfiles/StimulusDecode2D_03.png differ diff --git a/helpfiles/StimulusDecode2D_04.png b/helpfiles/StimulusDecode2D_04.png index b384e93..0a0f836 100644 Binary files a/helpfiles/StimulusDecode2D_04.png and b/helpfiles/StimulusDecode2D_04.png differ diff --git a/helpfiles/StimulusDecode2D_05.png b/helpfiles/StimulusDecode2D_05.png new file mode 100644 index 0000000..285b6b1 Binary files /dev/null and b/helpfiles/StimulusDecode2D_05.png differ diff --git a/helpfiles/StimulusDecode2D_06.png b/helpfiles/StimulusDecode2D_06.png new file mode 100644 index 0000000..e71b009 Binary files /dev/null and b/helpfiles/StimulusDecode2D_06.png differ diff --git a/helpfiles/TrialConfigExamples.html b/helpfiles/TrialConfigExamples.html index c93b55e..1e7d344 100644 --- a/helpfiles/TrialConfigExamples.html +++ b/helpfiles/TrialConfigExamples.html @@ -1,13 +1,18 @@ - - - - - TrialConfig Examples

TrialConfig Examples

tcObj=TrialConfig(covMask,sampleRate, history,minTime,maxTime)

tc1 = TrialConfig({'Force','f_x'},2000,[.1 .2],-1,2);
+  
+
+
+
+

TrialConfig Examples

+

tcObj=TrialConfig(covMask,sampleRate, history,minTime,maxTime)

+
tc1 = TrialConfig({'Force','f_x'},2000,[.1 .2],-1,2);
 tc2 = TrialConfig({'Position','x'},2000,[.1 .2],-1,2);
 tcc = ConfigColl({tc1,tc2});
-
\ No newline at end of file +--> + + diff --git a/helpfiles/TrialExamples.html b/helpfiles/TrialExamples.html index d2c6d32..8489529 100644 --- a/helpfiles/TrialExamples.html +++ b/helpfiles/TrialExamples.html @@ -1,13 +1,18 @@ - - - - - Trial Examples

Trial Examples

Contents

Example 1: A simple data set

close all; clear all;
+  
+
+
+
+

Trial Examples

+ + +

Contents

+ +

Example 1: A simple data set

+
close all; clear all;
 lengthTrial=1;
-

Create History windows of interest

windowTimes = [0 .1 .2 .4];
+
+

Create History windows of interest

+
windowTimes = [0 .1 .2 .4];
 h=History(windowTimes);
 figure; h.plot;
-

Load Covariates

load CovariateSample.mat; %load position and force covariates
+
+

Load Covariates

+
load CovariateSample.mat; %load position and force covariates
 cc=CovColl({position,force});
 cc.setMaxTime(lengthTrial);
 figure; cc.plot;
-

Create trial events

eTimes = sort(rand(1,2)*lengthTrial);
+
+

Create trial events

+
eTimes = sort(rand(1,2)*lengthTrial);
 eLabels={'E_1','E_2'};
 e=Events(eTimes,eLabels); %use default eventColor 'r'
 figure; e.plot;
-

Create neural Spike Train Data

clear nst;
+
+

Create neural Spike Train Data

+
clear nst;
 for i=1:4
     spikeTimes = sort(rand(1,100))*lengthTrial;
     nst{i}=nspikeTrain(spikeTimes,'',.001);
 end
 spikeColl=nstColl(nst); %create a nstColl
 figure; spikeColl.plot;
-

Finally we have everything we need to create a Trial object.

trial1=Trial(spikeColl, cc, e, h);
+
+

Finally we have everything we need to create a Trial object.

+
trial1=Trial(spikeColl, cc, e, h);
 figure; trial1.plot; % plot all the data;
-

Mask out some of the data and plot the trial once again

trial1.setCovMask({{'Position','x'},{'Force','f_x'}})
+
+

Mask out some of the data and plot the trial once again

+
trial1.setCovMask({{'Position','x'},{'Force','f_x'}})
 figure; trial1.plot;
 
 trial1.getHistForNeurons([1:2]);
-

Example 2: Analyzing Trial Data

Examples of neural spike analysis using the Neural Spike Analysis Toolbox or using standard methods standard methods

\ No newline at end of file +--> + + diff --git a/helpfiles/TrialExamples.png b/helpfiles/TrialExamples.png index 584a53f..0b8e5c9 100644 Binary files a/helpfiles/TrialExamples.png and b/helpfiles/TrialExamples.png differ diff --git a/helpfiles/TrialExamples_01.png b/helpfiles/TrialExamples_01.png index 62080e3..81c3cb9 100644 Binary files a/helpfiles/TrialExamples_01.png and b/helpfiles/TrialExamples_01.png differ diff --git a/helpfiles/TrialExamples_02.png b/helpfiles/TrialExamples_02.png index f9178d6..e725fff 100644 Binary files a/helpfiles/TrialExamples_02.png and b/helpfiles/TrialExamples_02.png differ diff --git a/helpfiles/TrialExamples_03.png b/helpfiles/TrialExamples_03.png index be00482..c40677c 100644 Binary files a/helpfiles/TrialExamples_03.png and b/helpfiles/TrialExamples_03.png differ diff --git a/helpfiles/TrialExamples_04.png b/helpfiles/TrialExamples_04.png index 4d38b72..715b016 100644 Binary files a/helpfiles/TrialExamples_04.png and b/helpfiles/TrialExamples_04.png differ diff --git a/helpfiles/TrialExamples_05.png b/helpfiles/TrialExamples_05.png index 54ff221..df0b962 100644 Binary files a/helpfiles/TrialExamples_05.png and b/helpfiles/TrialExamples_05.png differ diff --git a/helpfiles/TrialExamples_06.png b/helpfiles/TrialExamples_06.png index ccd73f5..86e436f 100644 Binary files a/helpfiles/TrialExamples_06.png and b/helpfiles/TrialExamples_06.png differ diff --git a/helpfiles/ValidationDataSet.html b/helpfiles/ValidationDataSet.html index fa7e103..efb11fe 100644 --- a/helpfiles/ValidationDataSet.html +++ b/helpfiles/ValidationDataSet.html @@ -1,13 +1,18 @@ - - - - - Software Validation Data Set

Software Validation Data Set

The purpose of this example is to two important test cases of data to validate the Neural Spike Analysis Toolbox.

Contents

Case #1: Constant Rate Poisson Process

First we want to show that when neural firing activity is generated from a constant rate poisson process, the algorithm is able to estimate the value of this constant rate.

clear all;
+  
+
+
+
+

Software Validation Data Set

+ +

The purpose of this example is to two important test cases of data to validate the Neural Spike Analysis Toolbox.

+ +

Contents

+ +

Case #1: Constant Rate Poisson Process

+

First we want to show that when neural firing activity is generated from a constant rate poisson process, the algorithm is able to estimate the value of this constant rate.

+
clear all;
 close all;
 
 p=0.01;         % bernoilli probability
@@ -75,7 +103,8 @@
 lambda=N*p/T    % lambda*T = N*p
 
 mu = log(lambda*delta/(1-lambda*delta))
-
+
+
 lambda =
 
     10
@@ -85,7 +114,10 @@
 
    -4.5951
 
-

Now generate data for two neurons based on this constant rate

for i=1:2
+
+

Now generate data for two neurons based on this constant rate

+
+for i=1:2
     t=linspace(0,T,N);
     ind=rand(1,N)<p;     %generate the coin-flip indices for heads or 1's
     spikeTimes = t(ind); %get time spikes based on indices
@@ -93,8 +125,12 @@
     nst{i}.setMinTime(0);
     nst{i}.setMaxTime(T);
 end
-

For a sanity check we can plot the ISI histogram for the two neurons and verify that they are exponentially distributed with \lambda = N*p/T;

nst{1}.plotISIHistogram;
-

Setup the analysis using the Neural Spike Analysis Toolbox Since we are going to try to fit a constant rate model, we create a baseline covariate that is constant and equal to 1 for the duration of the trial. This data in the covarate will be labeled 'constant';

spikeColl=nstColl(nst); %create a nstColl - a collection of spikeTrains
+
+

For a sanity check we can plot the ISI histogram for the two neurons and verify that they are exponentially distributed with \lambda = N*p/T;

+
nst{1}.plotISIHistogram;
+
+

Setup the analysis using the Neural Spike Analysis Toolbox Since we are going to try to fit a constant rate model, we create a baseline covariate that is constant and equal to 1 for the duration of the trial. This data in the covarate will be labeled 'constant';

+
spikeColl=nstColl(nst); %create a nstColl - a collection of spikeTrains
 cov=Covariate(t,ones(length(t),1),'Baseline','s','','',{'mu'});
 cc=CovColl({cov}); % Gather all the covariates
 trial=Trial(spikeColl, cc); %Create the trial
@@ -106,14 +142,21 @@
 c{1} = TrialConfig({{'Baseline','mu'}},sampleRate,[],[]);
 c{1}.setName('Baseline');
 cfgColl= ConfigColl(c); %place desired configurations in a ConfigColl structure
-

Run the analysis

results = Analysis.RunAnalysisForAllNeurons(trial,cfgColl,0);
+
+

Run the analysis

+
results = Analysis.RunAnalysisForAllNeurons(trial,cfgColl,0);
 results{1}.plotResults; subplot(2,4,[5 6]); plot(mu,'ro', 'MarkerSize',10);
 results{2}.plotResults; subplot(2,4,[5 6]); plot(mu,'ro', 'MarkerSize',10);
 figure;
 subplot(1,2,1);results{1}.lambda.plot; hold on; plot(results{1}.lambda.time,lambda*ones(length(results{1}.lambda.time),1),'r-.','LineWidth',3);
 subplot(1,2,2);results{2}.lambda.plot; hold on; plot(results{2}.lambda.time,lambda*ones(length(results{2}.lambda.time),1),'r-.','LineWidth',3);
-
Analyzing Configuration #1: Neuron #1,2
-

Case #2: Piece-wise Constant Rate Poisson Process

Make a joint process be the sum of two independet and non-overlapping Poisson processes with different rates. During the first interval, only observer arrivals from process 1, and during the second interval only observe arrivals from the second process. Compare the results of estimate the complete process as the sum of two distinct independent and non-overlapping Poisson processes versus a single constant rate process.

% Process 1
+
+
Analyzing Configuration #1: Neuron #1,2
+
+

Case #2: Piece-wise Constant Rate Poisson Process

+

Make a joint process be the sum of two independet and non-overlapping Poisson processes with different rates. During the first interval, only observer arrivals from process 1, and during the second interval only observe arrivals from the second process. Compare the results of estimate the complete process as the sum of two distinct independent and non-overlapping Poisson processes versus a single constant rate process.

+
+% Process 1
 p1=0.001; % bernoilli probability of process 1
 N1=100000; %
 delta = 0.001;
@@ -130,7 +173,8 @@
 %Estimate of constant rate process:
 lambdaConst = (N1*p1 + N2*p2)/(T1+T2)
 muConst = log(lambdaConst*delta/(1-lambdaConst*delta))
-
+
+
 lambda1 =
 
      1
@@ -160,7 +204,10 @@
 
    -5.1975
 
-

Generate the data for 2 neurons

for i=1:2
+
+

Generate the data for 2 neurons

+
+for i=1:2
     tTot = linspace(0,(T1+T2),(N1+N2+1));
     t1=tTot(tTot<=T1);
     ind1=rand(1,N1)<p1;
@@ -174,7 +221,9 @@
     nst{i}.setMinTime(0);
     nst{i}.setMaxTime(max(t2));
 end
-

Generate the trial data;

spikeColl=nstColl(nst); %create a nstColl
+
+

Generate the trial data;

+
spikeColl=nstColl(nst); %create a nstColl
 cov=Covariate(tTot,[ones(length(tTot),1), tTot<=max(t1), tTot>max(t1)],'Baseline','s','','',{'muConst','mu1','mu2'});
 cc=CovColl({cov});
 
@@ -189,17 +238,29 @@
 c{2} = TrialConfig({{'Baseline','mu1','mu2'}},sampleRate,[],[]);
 c{2}.setName('Variable');
 cfgColl= ConfigColl(c);
-

Run the analysis

results = Analysis.RunAnalysisForAllNeurons(trial,cfgColl,0);
+
+

Run the analysis

+
results = Analysis.RunAnalysisForAllNeurons(trial,cfgColl,0);
 results{1}.plotResults;
 results{2}.plotResults;
 figure;
 subplot(1,2,1); results{1}.lambda.plot;
 subplot(1,2,2); results{2}.lambda.plot;
-
Analyzing Configuration #1: Neuron #1,2
+
+
Analyzing Configuration #1: Neuron #1,2
 Analyzing Configuration #2: Neuron #1,2
-

Compare the results across the two neurons

Summary = FitResSummary(results);
+
+

Compare the results across the two neurons

+
Summary = FitResSummary(results);
 Summary.plotSummary;
-
\ No newline at end of file +--> + + diff --git a/helpfiles/ValidationDataSet.png b/helpfiles/ValidationDataSet.png index d3ccbad..6e28f48 100644 Binary files a/helpfiles/ValidationDataSet.png and b/helpfiles/ValidationDataSet.png differ diff --git a/helpfiles/ValidationDataSet_01.png b/helpfiles/ValidationDataSet_01.png index f25c2f7..4d688a8 100644 Binary files a/helpfiles/ValidationDataSet_01.png and b/helpfiles/ValidationDataSet_01.png differ diff --git a/helpfiles/ValidationDataSet_02.png b/helpfiles/ValidationDataSet_02.png index 16c76fc..82b91ee 100644 Binary files a/helpfiles/ValidationDataSet_02.png and b/helpfiles/ValidationDataSet_02.png differ diff --git a/helpfiles/ValidationDataSet_03.png b/helpfiles/ValidationDataSet_03.png index ecf27ab..dad4475 100644 Binary files a/helpfiles/ValidationDataSet_03.png and b/helpfiles/ValidationDataSet_03.png differ diff --git a/helpfiles/ValidationDataSet_04.png b/helpfiles/ValidationDataSet_04.png index c1e77c4..f9c4602 100644 Binary files a/helpfiles/ValidationDataSet_04.png and b/helpfiles/ValidationDataSet_04.png differ diff --git a/helpfiles/ValidationDataSet_05.png b/helpfiles/ValidationDataSet_05.png index fcf7cfc..95faee7 100644 Binary files a/helpfiles/ValidationDataSet_05.png and b/helpfiles/ValidationDataSet_05.png differ diff --git a/helpfiles/ValidationDataSet_06.png b/helpfiles/ValidationDataSet_06.png index 496bc74..95fdb95 100644 Binary files a/helpfiles/ValidationDataSet_06.png and b/helpfiles/ValidationDataSet_06.png differ diff --git a/helpfiles/ValidationDataSet_07.png b/helpfiles/ValidationDataSet_07.png index 2eb4215..ded394f 100644 Binary files a/helpfiles/ValidationDataSet_07.png and b/helpfiles/ValidationDataSet_07.png differ diff --git a/helpfiles/ValidationDataSet_08.png b/helpfiles/ValidationDataSet_08.png index 18d1a82..95e1e19 100644 Binary files a/helpfiles/ValidationDataSet_08.png and b/helpfiles/ValidationDataSet_08.png differ diff --git a/helpfiles/ValidationDataSet_09.png b/helpfiles/ValidationDataSet_09.png new file mode 100644 index 0000000..5ef82db Binary files /dev/null and b/helpfiles/ValidationDataSet_09.png differ diff --git a/helpfiles/ValidationDataSet_10.png b/helpfiles/ValidationDataSet_10.png new file mode 100644 index 0000000..db1a288 Binary files /dev/null and b/helpfiles/ValidationDataSet_10.png differ diff --git a/helpfiles/ValidationDataSet_11.png b/helpfiles/ValidationDataSet_11.png new file mode 100644 index 0000000..3720ef9 Binary files /dev/null and b/helpfiles/ValidationDataSet_11.png differ diff --git a/helpfiles/ValidationDataSet_12.png b/helpfiles/ValidationDataSet_12.png new file mode 100644 index 0000000..95561a7 Binary files /dev/null and b/helpfiles/ValidationDataSet_12.png differ diff --git a/helpfiles/custom_toolbox.json b/helpfiles/custom_toolbox.json index bf49583..1eaf577 100644 --- a/helpfiles/custom_toolbox.json +++ b/helpfiles/custom_toolbox.json @@ -1 +1 @@ -{"Name":"nSTAT-Neural Spike Train Analysis Toolbox","ShortName":"nstat_neural_spike_train_analysis_toolbox","ID":"3ptoolbox::nstat_neural_spike_train_analysis_toolbox","ToolboxHelpLocations":[{"ContentType":"doc","LocationOnDisk":"/Users/iahncajigas/Library/CloudStorage/Dropbox/Research/Matlab/nSTAT_currentRelease_Local/helpfiles","AbsolutePath":"/Users/iahncajigas/Library/CloudStorage/Dropbox/Research/Matlab/nSTAT_currentRelease_Local/info.xml","HelpLocation":"3ptoolbox/nstatneuralspiketrainanalysistoolbox/doc"}]} \ No newline at end of file +{"Name":"nSTAT Neural Spike Train Analysis Toolbox","ShortName":"nstat_neural_spike_train_analysis_toolbox","ID":"3ptoolbox::nstat_neural_spike_train_analysis_toolbox","ToolboxHelpLocations":[{"ContentType":"doc","LocationOnDisk":"/Users/iahncajigas/Library/CloudStorage/Dropbox/Research/Matlab/nSTAT_currentRelease_Local/helpfiles","AbsolutePath":"/Users/iahncajigas/Library/CloudStorage/Dropbox/Research/Matlab/nSTAT_currentRelease_Local/info.xml","HelpLocation":"3ptoolbox/nstatneuralspiketrainanalysistoolbox/doc"}]} \ No newline at end of file diff --git a/helpfiles/helpsearch-v4_en/store/000000000002.zap b/helpfiles/helpsearch-v4_en/store/000000000002.zap index c06d83d..920676b 100755 Binary files a/helpfiles/helpsearch-v4_en/store/000000000002.zap and b/helpfiles/helpsearch-v4_en/store/000000000002.zap differ diff --git a/helpfiles/helpsearch-v4_en/store/root.bolt b/helpfiles/helpsearch-v4_en/store/root.bolt index 9a96719..3849a15 100755 Binary files a/helpfiles/helpsearch-v4_en/store/root.bolt and b/helpfiles/helpsearch-v4_en/store/root.bolt differ diff --git a/helpfiles/helptoc.xml b/helpfiles/helptoc.xml index 5314082..da428cb 100644 --- a/helpfiles/helptoc.xml +++ b/helpfiles/helptoc.xml @@ -2,6 +2,7 @@ nSTAT Neural Spike Train Analysis Toolbox Overview + Paper-Aligned Toolbox Map MATLAB 2025b Help Integration Class Definitions @@ -36,6 +37,7 @@ nSTAT Paper Examples - Neuroscience Statistics Research Laboratory + Neuroscience Statistics Research Laboratory + RESToRe Lab diff --git a/helpfiles/index.html b/helpfiles/index.html new file mode 100644 index 0000000..f9f1ab9 --- /dev/null +++ b/helpfiles/index.html @@ -0,0 +1,14 @@ + + + + + + + nSTAT Helpfiles + + +

+ Redirecting to nSTAT Help Home. +

+ + diff --git a/helpfiles/mEPSCAnalysis.html b/helpfiles/mEPSCAnalysis.html index 62981f6..c93c74c 100644 --- a/helpfiles/mEPSCAnalysis.html +++ b/helpfiles/mEPSCAnalysis.html @@ -1,69 +1,141 @@ - - - - - MINIATURE EXCITATORY POST-SYNAPTIC CURRENTS (mEPSCs)

MINIATURE EXCITATORY POST-SYNAPTIC CURRENTS (mEPSCs)

Data from Marnie Phillips marnie.a.phillips@gmail.com This analysis is based on a partial version of the dataset used in

Phillips MA, Lewis LD, Gong J, Constantine-Paton M, Brown EN. 2011 Model-based statistical analysis of miniature synaptic transmission. J Neurophys (under consideration)

Author: Iahn Cajigas

Date: 03/01/2011

Contents

Data Description

epsc2.txt: Event times of selected, constant rate, miniature excitatory post-synaptic currents (mEPSCs) in 0mM magnesium condition]

washout1.txt: Variable rate recording: Event times of selected events, beginning approximately 260 seconds after magnesium is first removed.

washout2.txt: Event times of selected events from the same recording, beginning 745 seconds after magnesium is first removed

Column headers in the text files explain what each column represents.

Event selection criteria for the "washout1" and "washout2" condition were:

  • Amplitude > 10pA
  • 10-90% rise time < 20ms

For this washout experiment, the recording duration was so long, and there were so many events, that the minimum amplitude threshold was conservative.

The mean RMS noise was only 1.36pA, and a usual threshold would be 5*RMS = 6.8pA.

Constant Magnesium Concentration - Constant rate poisson

Under a constant Magnesium concentration, it is seen that the mEPSCs behave as a homogeneous poisson process (constant arrival rate).

    close all;
-    epsc2 = importdata('epsc2.txt');
+
+
+  
+
+
+
+

MINIATURE EXCITATORY POST-SYNAPTIC CURRENTS (mEPSCs)

+ +

Data from Marnie Phillips marnie.a.phillips@gmail.com This analysis is based on a partial version of the dataset used in

+

Phillips MA, Lewis LD, Gong J, Constantine-Paton M, Brown EN. 2011 Model-based statistical analysis of miniature synaptic transmission. J Neurophys (under consideration)

+

+Author: Iahn Cajigas

+

+Date: 03/01/2011

+ +

Contents

+ +

Data Description

+

+epsc2.txt: Event times of selected, constant rate, miniature excitatory post-synaptic currents (mEPSCs) in 0mM magnesium condition]

+

+washout1.txt: Variable rate recording: Event times of selected events, beginning approximately 260 seconds after magnesium is first removed.

+

+washout2.txt: Event times of selected events from the same recording, beginning 745 seconds after magnesium is first removed

+

Column headers in the text files explain what each column represents.

+

Event selection criteria for the "washout1" and "washout2" condition were:

+
+
    +
  • Amplitude > 10pA
  • +
  • 10-90% rise time < 20ms
  • +
+
+

For this washout experiment, the recording duration was so long, and there were so many events, that the minimum amplitude threshold was conservative.

+

The mean RMS noise was only 1.36pA, and a usual threshold would be 5*RMS = 6.8pA.

+

Constant Magnesium Concentration - Constant rate poisson

+

Under a constant Magnesium concentration, it is seen that the mEPSCs behave as a homogeneous poisson process (constant arrival rate).

+
    close all;
+    [~,mEPSCDir] = getPaperDataDirs();
+    epsc2 = importdata(fullfile(mEPSCDir,'epsc2.txt'));
     sampleRate = 1000;
     spikeTimes = epsc2.data(:,2)*1/sampleRate; %in seconds
     nst = nspikeTrain(spikeTimes);
@@ -86,10 +158,14 @@
     % Perform Analysis (Commented to since data already saved)
     results =Analysis.RunAnalysisForAllNeurons(trial,tcc,0);
     results.plotResults;
-
Analyzing Configuration #1: Neuron #1
-

Varying Magnesium Concentration - Piecewise Constant rate poisson

When the magnesium concentration of the bath decreased (i.e. magnesium is removed), the rate of mEPSCs begin to increase in frequency. This can be modeled in a many different ways (using the change in Magnesium directly as a model covariate, etc.) Here we approximate the rate as being constant during certain portions of the experiment. These segments can in principle be estimated (using heirarchical Bayesian methods), but here we select them via visual inspection. We compare three models: a constant rate model (from above), a piecewise constant rate model, and a piecewise constant rate model with history.

 % load the data;
-    washout1 = importdata('washout1.txt');
-    washout2 = importdata('washout2.txt');
+
+
Analyzing Configuration #1: Neuron #1
+
+

Varying Magnesium Concentration - Piecewise Constant rate poisson

+

When the magnesium concentration of the bath decreased (i.e. magnesium is removed), the rate of mEPSCs begin to increase in frequency. This can be modeled in a many different ways (using the change in Magnesium directly as a model covariate, etc.) Here we approximate the rate as being constant during certain portions of the experiment. These segments can in principle be estimated (using heirarchical Bayesian methods), but here we select them via visual inspection. We compare three models: a constant rate model (from above), a piecewise constant rate model, and a piecewise constant rate model with history.

+
 % load the data;
+    washout1 = importdata(fullfile(mEPSCDir,'washout1.txt'));
+    washout2 = importdata(fullfile(mEPSCDir,'washout2.txt'));
 
     sampleRate  = 1000;
     % Magnesium removed at t=0
@@ -97,9 +173,14 @@
     spikeTimes2 = sort(washout2.data(:,2))*1/sampleRate + 745;%in seconds
     nst = nspikeTrain([spikeTimes1; spikeTimes2]);
     time = 260:(1/sampleRate):nst.maxTime;
-

Data Visualization

Visual inspection of the spike train is used to pick three regions where the firing rate appears to be different. Here we do not estimate where these transitions happen but pick times in an ad-hoc manner.

    figure;
+
+

Data Visualization

+

Visual inspection of the spike train is used to pick three regions where the firing rate appears to be different. Here we do not estimate where these transitions happen but pick times in an ad-hoc manner.

+
    figure;
     nst.plot;
-

Define Covariates for the analysis

    timeInd1 =find(time<495,1,'last'); %0-495sec first constant rate
+
+

Define Covariates for the analysis

+
    timeInd1 =find(time<495,1,'last'); %0-495sec first constant rate
     timeInd2 =find(time<765,1,'last'); %495-765 second constant rate epoch
                                        %765 onwards third constant rate
                                        %epoch
@@ -121,18 +202,27 @@
     windowTimes =unique(round([0 logspace(log10(delta),...
     log10(maxWindow),numWindows)]*sampleRate)./sampleRate);
     windowTimes = windowTimes(1:11);
-

Define how we want to analyze the data

    clear tc tcc;
+
+

Define how we want to analyze the data

+
    clear tc tcc;
     tc{1} = TrialConfig({{'Baseline','\mu'}},sampleRate,[]); tc{1}.setName('Constant Baseline');
     tc{2} = TrialConfig({{'Baseline','\mu_{1}','\mu_{2}','\mu_{3}'}},sampleRate,[]); tc{2}.setName('Diff Baseline');
 %     tc{3} = TrialConfig({{'Baseline','\mu_{1}','\mu_{2}','\mu_{3}'}},sampleRate,windowTimes); tc{3}.setName('Diff Baseline+Hist');
     tcc = ConfigColl(tc);
-

Perform Analysis

We see that the piece-wise constant rate model (with and without history, outperform the constant baseline model in terms of AIC, BIC, and KS-statistic. While addition of the history effect yields a model that falls within the 95% confidence interval of the KS plot, it results in increases of the AIC and BIC because of the increased number of parameters.

    results =Analysis.RunAnalysisForAllNeurons(trial,tcc,0);
+
+

Perform Analysis

+

We see that the piece-wise constant rate model (with and without history, outperform the constant baseline model in terms of AIC, BIC, and KS-statistic. While addition of the history effect yields a model that falls within the 95% confidence interval of the KS plot, it results in increases of the AIC and BIC because of the increased number of parameters.

+
    results =Analysis.RunAnalysisForAllNeurons(trial,tcc,0);
     results.plotResults;
     Summary = FitResSummary(results);
     Summary.plotSummary;
-
Analyzing Configuration #1: Neuron #1
+
+
Analyzing Configuration #1: Neuron #1
 Analyzing Configuration #2: Neuron #1
-

Decode Rate using Point Process Filter

%     clear lambdaCIF;
+
+

Decode Rate using Point Process Filter

+
+%     clear lambdaCIF;
 %     delta = .001;
 %
 %     washout1 = importdata('washout1.txt');
@@ -185,8 +275,14 @@
 %     axis([v(1) v(2) 0 5]);
 %     xlabel('time [s]');
 %     ylabel('\lambda(t) [Hz]');
-
\ No newline at end of file +--> + + diff --git a/helpfiles/mEPSCAnalysis.m b/helpfiles/mEPSCAnalysis.m index ec12054..422d04d 100644 --- a/helpfiles/mEPSCAnalysis.m +++ b/helpfiles/mEPSCAnalysis.m @@ -38,11 +38,12 @@ % 5*RMS = 6.8pA. % -%% Constant Magnesium Concentration - Constant rate poisson -% Under a constant Magnesium concentration, it is seen that the mEPSCs -% behave as a homogeneous poisson process (constant arrival rate). - close all; - epsc2 = importdata('epsc2.txt'); +%% Constant Magnesium Concentration - Constant rate poisson +% Under a constant Magnesium concentration, it is seen that the mEPSCs +% behave as a homogeneous poisson process (constant arrival rate). + close all; + [~,mEPSCDir] = getPaperDataDirs(); + epsc2 = importdata(fullfile(mEPSCDir,'epsc2.txt')); sampleRate = 1000; spikeTimes = epsc2.data(:,2)*1/sampleRate; %in seconds nst = nspikeTrain(spikeTimes); @@ -78,9 +79,9 @@ % constant rate model (from above), a piecewise constant rate model, and a % piecewise constant rate model with history. - % load the data; - washout1 = importdata('washout1.txt'); - washout2 = importdata('washout2.txt'); + % load the data; + washout1 = importdata(fullfile(mEPSCDir,'washout1.txt')); + washout2 = importdata(fullfile(mEPSCDir,'washout2.txt')); sampleRate = 1000; % Magnesium removed at t=0 @@ -196,4 +197,4 @@ % v=axis; % axis([v(1) v(2) 0 5]); % xlabel('time [s]'); -% ylabel('\lambda(t) [Hz]'); \ No newline at end of file +% ylabel('\lambda(t) [Hz]'); diff --git a/helpfiles/mEPSCAnalysis.png b/helpfiles/mEPSCAnalysis.png index 6cd2348..d47f19d 100644 Binary files a/helpfiles/mEPSCAnalysis.png and b/helpfiles/mEPSCAnalysis.png differ diff --git a/helpfiles/mEPSCAnalysis_01.png b/helpfiles/mEPSCAnalysis_01.png index af39dc3..7b61d4f 100644 Binary files a/helpfiles/mEPSCAnalysis_01.png and b/helpfiles/mEPSCAnalysis_01.png differ diff --git a/helpfiles/mEPSCAnalysis_02.png b/helpfiles/mEPSCAnalysis_02.png index f5079af..b167805 100644 Binary files a/helpfiles/mEPSCAnalysis_02.png and b/helpfiles/mEPSCAnalysis_02.png differ diff --git a/helpfiles/mEPSCAnalysis_03.png b/helpfiles/mEPSCAnalysis_03.png index f035123..ad238b3 100644 Binary files a/helpfiles/mEPSCAnalysis_03.png and b/helpfiles/mEPSCAnalysis_03.png differ diff --git a/helpfiles/mEPSCAnalysis_04.png b/helpfiles/mEPSCAnalysis_04.png index 72ec564..72b404b 100644 Binary files a/helpfiles/mEPSCAnalysis_04.png and b/helpfiles/mEPSCAnalysis_04.png differ diff --git a/helpfiles/mEPSCAnalysis_05.png b/helpfiles/mEPSCAnalysis_05.png new file mode 100644 index 0000000..7badbfd Binary files /dev/null and b/helpfiles/mEPSCAnalysis_05.png differ diff --git a/helpfiles/nSTATPaperExamples.html b/helpfiles/nSTATPaperExamples.html index fa02d03..8049cdc 100644 --- a/helpfiles/nSTATPaperExamples.html +++ b/helpfiles/nSTATPaperExamples.html @@ -1,13 +1,18 @@ - - - - - nSTAT J. Neuroscience Methods Paper Examples

nSTAT J. Neuroscience Methods Paper Examples

Author: Iahn Cajigas

Date: 01/04/2012

Contents

Experiment 1

MINIATURE EXCITATORY POST-SYNAPTIC CURRENTS (mEPSCs) Data from Marnie Phillips marnie.a.phillips@gmail.com This analysis is based on a partial version of the dataset used in

Phillips MA, Lewis LD, Gong J, Constantine-Paton M, Brown EN. 2011 Model-based statistical analysis of miniature synaptic transmission. J Neurophys (under consideration)

Date: 03/01/2011

Constant Magnesium Concentration - Constant rate poisson

Under a constant Magnesium concentration, it is seen that the mEPSCs behave as a homogeneous poisson process (constant arrival rate).

    close all; clear all;
-    epsc2 = importdata('epsc2.txt');
+  
+
+
+
+

Contents

+
+ +
+

nSTAT J. Neuroscience Methods Paper Examples

+

+Author: Iahn Cajigas

+

+Date: 01/04/2012

+

Force command echo off so published output does not include repeated executed source lines.

+
echo off;
+%
+% Paper reference:
+%
+% * Cajigas I, Malik WQ, Brown EN. nSTAT: Open-source neural spike train
+%   analysis toolbox for Matlab. Journal of Neuroscience Methods 211:
+%   245-264 (2012).
+% * DOI: 10.1016/j.jneumeth.2012.08.009
+% * PMID: 22981419
+%
+% Navigation:
+%
+% * <PaperOverview.html Paper-Aligned Toolbox Map>
+
+

Experiment 1

+

MINIATURE EXCITATORY POST-SYNAPTIC CURRENTS (mEPSCs) Data from Marnie Phillips marnie.a.phillips@gmail.com This analysis is based on a partial version of the dataset used in

+

Phillips MA, Lewis LD, Gong J, Constantine-Paton M, Brown EN. 2011 Model-based statistical analysis of miniature synaptic transmission. J Neurophys (under consideration)

+

+Date: 03/01/2011

+

Constant Magnesium Concentration - Constant rate poisson

+

Under a constant Magnesium concentration, it is seen that the mEPSCs behave as a homogeneous poisson process (constant arrival rate).

+
    close all; clear all;
+    [dataDir,mEPSCDir,explicitStimulusDir,psthDir,placeCellDataDir] = ...
+        getPaperDataDirs();
+    nSTATRootDir = fileparts(dataDir);
+    if exist(nSTATRootDir,'dir') == 7 && ~strcmp(pwd,nSTATRootDir)
+        cd(nSTATRootDir);
+    end
+    epsc2 = importdata(fullfile(mEPSCDir,'epsc2.txt'));
     sampleRate = 1000;
     spikeTimes = epsc2.data(:,2)*1/sampleRate; %in seconds
     nstConst = nspikeTrain(spikeTimes);
@@ -118,47 +259,15 @@
     pos = get(h_legend,'position');
     set(h_legend, 'position',[pos(1)+.05 pos(2) pos(3:4)]);
     set(h_legend,'FontSize',14)
-
Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\private\evalmxdom.m' could not be
-cleared because it contains MATLAB code that is currently executing. 
-Warning: The file
-'C:\Users\Developer\Dropbox\GitHub\nSTAT\helpfiles\nSTATPaperExamples.m' could
-not be cleared because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\mdbpublish.m' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\publish.p' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\private\evalmxdom.m' could not be
-cleared because it contains MATLAB code that is currently executing. 
-Warning: The file
-'C:\Users\Developer\Dropbox\GitHub\nSTAT\helpfiles\nSTATPaperExamples.m' could
-not be cleared because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\mdbpublish.m' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\publish.p' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\private\evalmxdom.m' could not be
-cleared because it contains MATLAB code that is currently executing. 
-Warning: The file
-'C:\Users\Developer\Dropbox\GitHub\nSTAT\helpfiles\nSTATPaperExamples.m' could
-not be cleared because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\mdbpublish.m' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\publish.p' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Analyzing Configuration #1: Neuron #1
-

Varying Magnesium Concentration - Piecewise Constant rate poisson

When the magnesium concentration of the bath decreased (i.e. magnesium is removed), the rate of mEPSCs begin to increase in frequency. This can be modeled in a many different ways (using the change in Magnesium directly as a model covariate, etc.) Here we approximate the rate as being constant during certain portions of the experiment. These segments can in principle be estimated (using heirarchical Bayesian methods), but here we select them via visual inspection. We compare three models: a constant rate model (from above), a piecewise constant rate model, and a piecewise constant rate model with history.

    close all;
+
+
Analyzing Configuration #1: Neuron #1
+
+

Varying Magnesium Concentration - Piecewise Constant rate poisson

+

When the magnesium concentration of the bath decreased (i.e. magnesium is removed), the rate of mEPSCs begin to increase in frequency. This can be modeled in a many different ways (using the change in Magnesium directly as a model covariate, etc.) Here we approximate the rate as being constant during certain portions of the experiment. These segments can in principle be estimated (using heirarchical Bayesian methods), but here we select them via visual inspection. We compare three models: a constant rate model (from above), a piecewise constant rate model, and a piecewise constant rate model with history.

+
    close all;
  % load the data;
-    washout1 = importdata('washout1.txt');
-    washout2 = importdata('washout2.txt');
+    washout1 = importdata(fullfile(mEPSCDir,'washout1.txt'));
+    washout2 = importdata(fullfile(mEPSCDir,'washout2.txt'));
 
     sampleRate  = 1000;
     % Magnesium removed at t=0
@@ -166,7 +275,10 @@
     spikeTimes2 = sort(washout2.data(:,2))*1/sampleRate + 745;%in seconds
     nst = nspikeTrain([spikeTimes1; spikeTimes2]);
     time = 260:(1/sampleRate):nst.maxTime;
-

Data Visualization

Visual inspection of the spike train is used to pick three regions where the firing rate appears to be different. Here we do not estimate where these transitions happen but pick times in an ad-hoc manner.

    scrsz = get(0,'ScreenSize');
+
+

Data Visualization

+

Visual inspection of the spike train is used to pick three regions where the firing rate appears to be different. Here we do not estimate where these transitions happen but pick times in an ad-hoc manner.

+
    scrsz = get(0,'ScreenSize');
     h=figure('OuterPosition',[scrsz(3)*.01 scrsz(4)*.04 scrsz(3)*.6 ...
         scrsz(4)*.9]);
 
@@ -187,7 +299,9 @@
             'FontName','Arial');
     hx=get(gca,'XLabel');
     set([hx,hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-

Define Covariates for the analysis

          timeInd1 =find(time<495,1,'last'); %0-495sec first constant rate
+
+

Define Covariates for the analysis

+
          timeInd1 =find(time<495,1,'last'); %0-495sec first constant rate
         timeInd2 =find(time<765,1,'last'); %495-765 second constant rate epoch
                                        %765 onwards third constant rate
                                        %epoch
@@ -210,19 +324,26 @@
     windowTimes =unique(round([0 logspace(log10(delta),...
     log10(maxWindow),numWindows)]*sampleRate)./sampleRate);
     windowTimes = windowTimes(1:11);
-

Define how we want to analyze the data

    clear tc tcc;
+
+

Define how we want to analyze the data

+
    clear tc tcc;
     tc{1} = TrialConfig({{'Baseline','\mu'}},sampleRate,[]);
     tc{1}.setName('Constant Baseline');
     tc{2} = TrialConfig({{'Baseline','\mu_{1}','\mu_{2}','\mu_{3}'}},...
         sampleRate,[]); tc{2}.setName('Diff Baseline');
     tcc = ConfigColl(tc);
-

Perform Analysis

We see that the piece-wise constant rate model (without history) outperforms the constant baseline model in terms of AIC, BIC, and KS-statistic.

    results =Analysis.RunAnalysisForAllNeurons(trial,tcc,0);
+
+

Perform Analysis

+

We see that the piece-wise constant rate model (without history) outperforms the constant baseline model in terms of AIC, BIC, and KS-statistic.

+
    results =Analysis.RunAnalysisForAllNeurons(trial,tcc,0);
 %     h=results.plotResults;
 %     Summary = FitResSummary(results);
 %     h=Summary.plotSummary;
-
Analyzing Configuration #1: Neuron #1
+
+
Analyzing Configuration #1: Neuron #1
 Analyzing Configuration #2: Neuron #1
-
close all;
+
+
close all;
     scrsz = get(0,'ScreenSize');
     results.lambda.setDataLabels({'\lambda_{const}',...
         '\lambda_{const-epoch}'});
@@ -258,16 +379,23 @@
     pos = get(h_legend,'position');
     set(h_legend, 'position',[pos(1)+.05 pos(2)-.01 pos(3:4)]);
     set(h_legend,'FontSize',14)
-

Experiment 2

EXPLICIT STIMULUS EXAMPLE - WHISKER STIMULATION/THALAMIC NEURON In the worksheet with analyze the stimulus effect and history effect on the firing of a thalamic neuron under a known stimulus consisting of whisker stimulation. Data from Demba Ba (demba@mit.edu)

Load the data

clear all;

close all; currdir = pwd;
-index = strfind(currdir,'helpfiles')-1;
-rootpath = currdir(1:index);
+
+

Experiment 2

+

EXPLICIT STIMULUS EXAMPLE - WHISKER STIMULATION/THALAMIC NEURON In the worksheet with analyze the stimulus effect and history effect on the firing of a thalamic neuron under a known stimulus consisting of whisker stimulation. Data from Demba Ba (demba@mit.edu)

+

Load the data

+

clear all;

+
close all;
+[dataDir,mEPSCDir,explicitStimulusDir,psthDir,placeCellDataDir] = ...
+    getPaperDataDirs();
+nSTATRootDir = fileparts(dataDir);
+if exist(nSTATRootDir,'dir') == 7 && ~strcmp(pwd,nSTATRootDir)
+    cd(nSTATRootDir);
+end
 
 Direction=3; Neuron=1; Stim=2;
-% datapath = strcat(rootpath,['data/Explicit Stimulus/Dir' num2str(Direction)...
-%     '\Neuron' num2str(Neuron) '\Stim' num2str(Stim) '\']);
-datapath = strcat(rootpath,['data/Explicit Stimulus/Dir' num2str(Direction)...
-    '/Neuron' num2str(Neuron) '/Stim' num2str(Stim) '/']);
-data=load(strcat(datapath,'trngdataBis.mat'));
+datapath = fullfile(explicitStimulusDir,['Dir' num2str(Direction)], ...
+    ['Neuron' num2str(Neuron)],['Stim' num2str(Stim)]);
+data = load(fullfile(datapath,'trngdataBis.mat'));
 
 time=0:.001:(length(data.t)-1)*.001;
 stimData = data.t;
@@ -325,7 +453,9 @@
   'XTick'       , 0:1:max(time), ...
   'YTick'       , -80:40:80, ...
   'LineWidth'   , 1         );
-

Fit a constant baseline and Find Stimulus Lag We fit a constant rate (Poisson) model to the data and use the look at the cross-covariance function of between the stimulus and the fit residual to determine the appropriate lag for the stimulus.

clear c; close all;
+
+

Fit a constant baseline and Find Stimulus Lag We fit a constant rate (Poisson) model to the data and use the look at the cross-covariance function of between the stimulus and the fit residual to determine the appropriate lag for the stimulus.

+
clear c; close all;
 selfHist = [] ; NeighborHist = []; sampleRate = 1000;
 c{1} = TrialConfig({{'Baseline','constant'}},sampleRate,selfHist,NeighborHist);
 c{1}.setName('Baseline');
@@ -362,8 +492,12 @@
 nspikeColl = nstColl(nst);
 cc = CovColl({stim,baseline});
 trial2 = Trial(nspikeColl,cc);
-
Analyzing Configuration #1: Neuron #1
-

Compare constant rate model with model including stimulus effect

Addition of the stimulus improves the fits in terms of the KS plot and the making the rescaled ISIs less correlated. The Point Process Residula also looks more "white"

clear c;
+
+
Analyzing Configuration #1: Neuron #1
+
+

Compare constant rate model with model including stimulus effect

+

Addition of the stimulus improves the fits in terms of the KS plot and the making the rescaled ISIs less correlated. The Point Process Residula also looks more "white"

+
clear c;
 selfHist = [] ; NeighborHist = []; sampleRate = 1000;
 c{1} = TrialConfig({{'Baseline','\mu'}},sampleRate,selfHist,...
     NeighborHist);
@@ -374,9 +508,13 @@
 cfgColl= ConfigColl(c);
 results = Analysis.RunAnalysisForAllNeurons(trial2,cfgColl,0);
 % results.plotResults;
-
Analyzing Configuration #1: Neuron #1
+
+
Analyzing Configuration #1: Neuron #1
 Analyzing Configuration #2: Neuron #1
-

History Effect

Determine the best history effect model using AIC, BIC, and KS statistic

sampleRate=1000;
+
+

History Effect

+

Determine the best history effect model using AIC, BIC, and KS statistic

+
sampleRate=1000;
 delta=1/sampleRate*1;
 maxWindow=1; numWindows=32;
 windowTimes =unique(round([0 logspace(log10(delta),...
@@ -469,7 +607,8 @@
     '\lambda_{const+stim+hist}'});
 subplot(7,2,[9 11 13]); results.KSPlot;
 subplot(7,2,[10 12 14]); results.plotCoeffs; legend off;
-
Analyzing Configuration #1: Neuron #1
+
+
Analyzing Configuration #1: Neuron #1
 Analyzing Configuration #2: Neuron #1
 Analyzing Configuration #3: Neuron #1
 Analyzing Configuration #4: Neuron #1
@@ -507,11 +646,16 @@
 Analyzing Configuration #1: Neuron #1
 Analyzing Configuration #2: Neuron #1
 Analyzing Configuration #3: Neuron #1
-

Example 3 - PSTH Data

% Generate a known Conditional Intensity Function
+
+

Example 3 - PSTH Data

+
+% Generate a known Conditional Intensity Function
 % We generated a known conditional intensity function (rate function) and
 % generate distinct realizations of point processes consistent with this
 % rate function. We use the method of thinning to simulate a point process.
 clear all;
+[dataDir,mEPSCDir,explicitStimulusDir,psthDir,placeCellDataDir] = ...
+    getPaperDataDirs();
 close all;
 delta = 0.001;
 Tmax = 1;
@@ -547,13 +691,7 @@
 hy=get(gca,'YLabel');
 set(hy,'FontName', 'Arial','FontSize',14,'FontWeight','bold');
 
-fileLocation = which('nSTAT_Install');
-index = strfind(fileLocation,'nSTAT_Install.m')-1;
-nSTATDir =fileLocation(1:index);
-
-rootDir = [nSTATDir 'data' filesep 'PSTH' filesep];
-filename = 'Results.mat';
-x=load(strcat(rootDir,filename));
+x = load(fullfile(psthDir,'Results.mat'));
 numTrials = x.Results.Data.Spike_times_STC.balanced_SUA.Nr_trials;
 cellNum=6; clear nst;
 for i=1:numTrials
@@ -592,43 +730,9 @@
     'Fontsize',12,'FontWeight','bold');
 title('Response to Moving Visual Stimulus (Neuron 1)','FontWeight',...
     'bold','Fontsize',14,'FontName','Arial');
-
Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\private\evalmxdom.m' could not be
-cleared because it contains MATLAB code that is currently executing. 
-Warning: The file
-'C:\Users\Developer\Dropbox\GitHub\nSTAT\helpfiles\nSTATPaperExamples.m' could
-not be cleared because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\mdbpublish.m' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\publish.p' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\private\evalmxdom.m' could not be
-cleared because it contains MATLAB code that is currently executing. 
-Warning: The file
-'C:\Users\Developer\Dropbox\GitHub\nSTAT\helpfiles\nSTATPaperExamples.m' could
-not be cleared because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\mdbpublish.m' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\publish.p' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\private\evalmxdom.m' could not be
-cleared because it contains MATLAB code that is currently executing. 
-Warning: The file
-'C:\Users\Developer\Dropbox\GitHub\nSTAT\helpfiles\nSTATPaperExamples.m' could
-not be cleared because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\mdbpublish.m' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\publish.p' could not be cleared
-because it contains MATLAB code that is currently executing. 
-

Estimate the PSTH with 50ms windows

close all;
+
+

Estimate the PSTH with 50ms windows

+
close all;
 
 scrsz = get(0,'ScreenSize');
 h=figure('Position',[scrsz(3)*.1 scrsz(4)*.1 scrsz(3)*.8 scrsz(4)*.8]);
@@ -703,14 +807,20 @@
     12,'FontWeight','bold');
 ylabel('Trial [k]','Interpreter','none','FontName', 'Arial',...
     'Fontsize',12,'FontWeight','bold');
-
Running in batch mode: neurons with same name are fit simultaneously
+
+
Running in batch mode: neurons with same name are fit simultaneously
 Analyzing Configuration #1: Neuron #1
 Running in batch mode: neurons with same name are fit simultaneously
 Analyzing Configuration #1: Neuron #6
 Running in batch mode: neurons with same name are fit simultaneously
 Analyzing Configuration #1: Neuron #1
-

Example 3b - SSGLM Example

Example of estimating with-in and across trial dynamics Methods from: G. Czanner, U. T. Eden, S. Wirth, M. Yanike, W. A. Suzuki, and E. N. Brown, "Analysis of between-trial and within-trial neural spiking dynamics.," Journal of neurophysiology, vol. 99, no. 5, pp. 2672?2693, May. 2008.

close all;
+
+

Example 3b - SSGLM Example

+

Example of estimating with-in and across trial dynamics Methods from: G. Czanner, U. T. Eden, S. Wirth, M. Yanike, W. A. Suzuki, and E. N. Brown, "Analysis of between-trial and within-trial neural spiking dynamics.," Journal of neurophysiology, vol. 99, no. 5, pp. 2672?2693, May. 2008.

+
close all;
 clear all;
+[dataDir,mEPSCDir,explicitStimulusDir,psthDir,placeCellDataDir] = ...
+    getPaperDataDirs();
 % set(0,'DefaultFigureRenderer','ZBuffer')
 delta = 0.001; Tmax = 1;
 time = 0:delta:Tmax;
@@ -752,43 +862,9 @@
 end
 
 spikeColl = nstColl(nst); %Create a collection of the spike trains across trials
-
Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\private\evalmxdom.m' could not be
-cleared because it contains MATLAB code that is currently executing. 
-Warning: The file
-'C:\Users\Developer\Dropbox\GitHub\nSTAT\helpfiles\nSTATPaperExamples.m' could
-not be cleared because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\mdbpublish.m' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\publish.p' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\private\evalmxdom.m' could not be
-cleared because it contains MATLAB code that is currently executing. 
-Warning: The file
-'C:\Users\Developer\Dropbox\GitHub\nSTAT\helpfiles\nSTATPaperExamples.m' could
-not be cleared because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\mdbpublish.m' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\publish.p' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\private\evalmxdom.m' could not be
-cleared because it contains MATLAB code that is currently executing. 
-Warning: The file
-'C:\Users\Developer\Dropbox\GitHub\nSTAT\helpfiles\nSTATPaperExamples.m' could
-not be cleared because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\mdbpublish.m' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\publish.p' could not be cleared
-because it contains MATLAB code that is currently executing. 
-

Summarize Simulated Data

close all;
+
+

Summarize Simulated Data

+
close all;
 scrsz = get(0,'ScreenSize');
 h=figure('Position',[scrsz(3)*.1 scrsz(4)*.1 scrsz(3)*.8 scrsz(4)*.8]);
 
@@ -844,7 +920,10 @@
 
 
 axis tight;
-

Estimation of the Stimulus Response

% Create the covariates that will be used for the GLM regression
+
+

Estimation of the Stimulus Response

+
+% Create the covariates that will be used for the GLM regression
 stim = Covariate(time,sin(2*pi*f*time),'Stimulus','time','s','V',{'stim'});
 baseline = Covariate(time,ones(length(time),1),'Baseline','time','s','',...
                     {'constant'});
@@ -902,7 +981,8 @@
     numVarEstIter,fitType);
 A=eye(numBasis,numBasis);
 delta = 1/spikeColl.sampleRate;
-
Running in batch mode: neurons with same name are fit simultaneously
+
+
Running in batch mode: neurons with same name are fit simultaneously
 Analyzing Configuration #1: Neuron #1
 Running in batch mode: neurons with same name are fit simultaneously
 Analyzing Configuration #1: Neuron #1
@@ -926,7 +1006,9 @@
 Analyzing Configuration #1: Neuron #1
 A=eye(numBasis,numBasis);
 delta = 1/spikeColl.sampleRate;
-

Run the SSGLM Filter

CompilingHelpFile=1;
+
+

Run the SSGLM Filter

+
CompilingHelpFile=1;
     % Commented out to speed up help file creation ...
     if(~CompilingHelpFile)
         Q0d=diag(Q0);
@@ -939,20 +1021,43 @@
         psthR = psthResult.toStructure;
     end
 % save SSGLMExampleData psthR fR xK WK WkuFinal Qhat gammahat fitResults stimulus stimCIs logll QhatAll gammahatAll nIter;
-
+
+
 %% Run the SSGLM Filter
 CompilingHelpFile=1;
     % Commented out to speed up help file creation ... 
     if(~CompilingHelpFile)
-
load SSGLMExampleData;
+    end
+
+
installPath = which('nSTAT_Install');
+if isempty(installPath)
+    error('nSTATPaperExamples:MissingInstallPath', ...
+        'Could not locate nSTAT_Install.m on MATLAB path.');
+end
+nstatRoot = fileparts(installPath);
+if exist(nstatRoot,'dir') == 7 && ~strcmp(pwd,nstatRoot)
+    cd(nstatRoot);
+end
+addpath(nstatRoot,'-begin');
+load(fullfile(nstatRoot,'data','SSGLMExampleData.mat'));
 fitResults = FitResult.fromStructure(fR);
 psthResult = FitResult.fromStructure(psthR);
-
% save SSGLMExampleData psthR fR xK WK WkuFinal Qhat gammahat fitResults stimulus stimCIs logll QhatAll gammahatAll nIter;
+
+
% save SSGLMExampleData psthR fR xK WK WkuFinal Qhat gammahat fitResults stimulus stimCIs logll QhatAll gammahatAll nIter;
 %%
-load SSGLMExampleData;
+installPath = which('nSTAT_Install');
+if isempty(installPath)
+        'Could not locate nSTAT_Install.m on MATLAB path.');
+end
+nstatRoot = fileparts(installPath);
+if exist(nstatRoot,'dir') == 7 && ~strcmp(pwd,nstatRoot)
+end
+addpath(nstatRoot,'-begin');
+load(fullfile(nstatRoot,'data','SSGLMExampleData.mat'));
 fitResults = FitResult.fromStructure(fR);
 psthResult = FitResult.fromStructure(psthR);
-
t=psthResult.mergeResults(fitResults);
+
+
t=psthResult.mergeResults(fitResults);
 %t.plotResults; %Compare the results with the PSTH Model
 t.lambda.setDataLabels({'\lambda_{PSTH}','\lambda_{SSGLM}'});
 scrsz = get(0,'ScreenSize');
@@ -966,7 +1071,8 @@
 dAIC=diff(S.AIC)
 dBIC=diff(S.BIC)
 dKS =diff(S.KSStats);
-
+
+
 %%
 t=psthResult.mergeResults(fitResults);
 %t.plotResults; %Compare the results with the PSTH Model
@@ -992,7 +1098,8 @@
   -6.9523e+03
 
 dKS =diff(S.KSStats); 
-
close all;
+
+
close all;
 % Generate the actual stimulus effect
 minTime=0; maxTime = Tmax;
 stimData = stim.data*b1;
@@ -1098,7 +1205,8 @@
 set(gca,'ytick',[],'ytickLabel',[]);
 set(gca, 'YDir','normal')
 view(gca,[90 -90]);
-
+
+
 
 %%
 close all;
@@ -1117,7 +1225,6 @@
     basisWidth = (maxTime-minTime)/numBasis;
     sampleRate=1/delta;
     unitPulseBasis=nstColl.generateUnitImpulseBasis(basisWidth,minTime,...
-        maxTime,sampleRate);
     basisMat = unitPulseBasis.data;
  end
 
@@ -1205,7 +1312,10 @@
 set(gca,'ytick',[],'ytickLabel',[]);
 set(gca, 'YDir','normal')
 view(gca,[90 -90]);
-

Compare differences across trials

close all;
+
+

Compare differences across trials

+
echo off;
+close all;
    minTime=0; maxTime = Tmax;
 % Generate the basis function so that the estimated effect can be plotted
 % at the same temporal resolution as the theoretical effect
@@ -1293,78312 +1403,951 @@
 h_legend=legend([h1(1) h2(1)],'\lambda_{1}(t)',['\lambda_{' num2str(lt) '}(t)']);
 pos = get(h_legend,'position');
 set(h_legend, 'position',[pos(1)+.03 pos(2)+.01 pos(3:4)]);
-
+
+
 
 
 %% Compare differences across trials 
-close all;
-   minTime=0; maxTime = Tmax;
-% Generate the basis function so that the estimated effect can be plotted
-% at the same temporal resolution as the theoretical effect
- if(~isempty(numBasis))
-    basisWidth = (maxTime-minTime)/numBasis;
-    sampleRate=1/delta;
-    unitPulseBasis=nstColl.generateUnitImpulseBasis(basisWidth,...
-        minTime,maxTime,sampleRate);
-    basisMat = unitPulseBasis.data;
- end
+echo off;
+The learning trial (compared to the first trial) is trial #6
+
+

Example 4 - HIPPOCAMPAL PLACE CELL - RECEPTIVE FIELD ESTIMATION

+

Estimation of receptive fields of neurons is a very common data analysis problem in neuroscience. Here we use the nSTAT software to perform an estimation of the receptive fields of hippocampal place cells using a bivariate Gaussian model and Zernike polynomials. The number of zernike polynomials is based on "An Analysis of Hippocampal Spatio-Temporal Representations Using a Bayesian Algorithm for Neural Spike Train Decoding" Barbieri et. al 2005. The data used herein in was provided by Dr. Ricardo Barbieri on 2/28/2011.

+

+Author: Iahn Cajigas

+

+Date: 3/1/2011

+

Example Data

+

The x and y coordinates of a freely foraging rat in a circular environment (70cm in diameter and 30cm high walls) and a fixed visual cue. The x and y coordinates at the time when a spike was observed are marked in red. The position coordinates have been normalized to be between -1 and 1 to allow to simplify the analysis.

+
    close all;
+    load(fullfile(placeCellDataDir,'PlaceCellDataAnimal1.mat'));
+    exampleCell = [2 21 25 49];
+%     exampleCell = 1:length(neuron);
+%     figure(1);
+    scrsz = get(0,'ScreenSize');
+    h=figure('OuterPosition',[scrsz(3)*.1 scrsz(4)*.1 scrsz(3)*.6 scrsz(4)*.9]);
 
+    for i=1:length(exampleCell)
+        subplot(2,2,i);
+        h1=plot(x,y,'b','Linewidth',.5); hold on;
+        h2=plot(neuron{exampleCell(i)}.xN,neuron{exampleCell(i)}.yN,'r.',...
+            'MarkerSize',7);
+        hx=xlabel('X Position'); hy=ylabel('Y Position');
+%         title(['Animal#1, Cell#' num2str(exampleCell(i))]);
+        title(['Cell#' num2str(exampleCell(i))],'FontWeight','bold',...
+            'Fontsize',12,'FontName','Arial');
+        set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
+        set(gca,'xTick',-1:.5:1,'yTick',-1:.5:1); axis square;
+        if(i==4)
+            h_legend = legend([h1 h2],'Animal Path',...
+                'Location at time of spike');
+            pos = get(h_legend,'position');
+            set(h_legend, 'position',[pos(1)+.09 pos(2)+.06 pos(3:4)]);
+        end
+    end
+
+

Analyze All Cells

+
numAnimals=2;
+CompilingHelpFile=1;
+if(~CompilingHelpFile)
+    for n=1:numAnimals
+        % load the data
+        clear x y neuron time nst tc tcc z;
+        load(fullfile(placeCellDataDir,['PlaceCellDataAnimal' num2str(n) '.mat']));
 
-% close all;
+        % Create the spikeTrains for each cell
+        for i=1:length(neuron)
+            nst{i} = nspikeTrain(neuron{i}.spikeTimes);
+        end
 
-t0=0; tf=Tmax;
-[spikeRateBinom, ProbMat,sigMat]=DecodingAlgorithms.computeSpikeRateCIs(xK,...
-    WkuFinal,dN,t0,tf,fitType,delta,gammahat,windowTimes);
 
-lt=find(sigMat(1,:)==1,1,'first');
-display(['The learning trial (compared to the first trial) is trial #' ...
-    num2str(find(sigMat(1,:)==1,1,'first'))]);
-The learning trial (compared to the first trial) is trial #12
-scrsz = get(0,'ScreenSize');
-h=figure('OuterPosition',[scrsz(3)*.1 scrsz(4)*.1 scrsz(3)*.8 scrsz(4)*.8]);
+        % Convert to polar coordinates
+        [theta,r] = cart2pol(x,y);
 
-subplot(2,3,1);
-spikeRateBinom.setName(['(' num2str(Tmax) '-0)^-1*\Lambda(0,' ...
-    num2str(Tmax) ')']);
-spikeRateBinom.plot([],{{' ''k'',''Linewidth'',4'}});
-% e = Events(lt,{''});
-% e.plot;
-v=axis;
-plot(lt*[1;1],v(3:4),'r','Linewidth',2);
-hx=xlabel('Trial [k]','Interpreter','none'); hold all;
-hy=ylabel('Average Firing Rate [spikes/sec]','Interpreter','none');
-set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-title(['Learning Trial:' num2str(lt)],'FontWeight','bold',...
-            'Fontsize',12,...
-            'FontName','Arial');
-        
 
+        % Evaluate the Zernike Polynomials
+        % Number of polynomials from "An Analysis of Hippocampal
+        % Spatio-Temporal Representations Using a Bayesian Algorithm for Neural
+        % Spike Train Decoding" Barbieri et. al 2005
+        cnt=0;
+        for l=0:3
+           for m=-l:l
+               if(~any(mod(l-m,2))) % otherwise the polynomial = 0
+                cnt = cnt+1;
+                z(:,cnt) = zernfun(l,m,r,theta,'norm');
+                % zernfun by Paul Fricker
+                % http://www.mathworks.com/matlabcentral/fileexchange/7687
+               end
+           end
+        end
 
-h=subplot(2,3,[2 3 5 6]);
-K=size(dN,1);
-colormap(flipud(gray));
-imagesc(ProbMat); hold on;
-for k=1:K
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-        if(sigMat(k,m)==1)
-            plot3(m,k,1,'r*'); hold on;
-        end
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-        if(sigMat(k,m)==1)
-    end
-end
-    for m=(k+1):K
-        if(sigMat(k,m)==1)
-    end
-end
-    for m=(k+1):K
-end
-%
-set(h,'XAxisLocation','top','YAxisLocation','right');
-hx=xlabel('Trial Number','Interpreter','none'); hold all;
-hy=ylabel('Trial Number','Interpreter','none');
-set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-
-subplot(2,3,4)
-stim1 = Covariate(time, basisMat*stimulus(:,1),'Trial1','time','s',...
-    'spikes/sec');
-temp = ConfidenceInterval(time, basisMat*squeeze(stimCIs(:,1,:)));
-stim1.setConfInterval(temp);
-stimlt = Covariate(time, basisMat*stimulus(:,lt),'Trial1','time','s',...
-    'spikes/sec');
-temp = ConfidenceInterval(time, basisMat*squeeze(stimCIs(:,lt,:)));
-temp.setColor('r');
-stimlt.setConfInterval(temp);
-stimltm1 = Covariate(time, basisMat*stimulus(:,lt-1),'Trial1','time','s',...
-    'spikes/sec');
-temp = ConfidenceInterval(time, basisMat*squeeze(stimCIs(:,lt-1,:)));
-temp.setColor('r');
-stimltm1.setConfInterval(temp);
-
-% figure;
-h1=stim1.plot([],{{' ''k'',''Linewidth'',4'}}); hold all;
-h2=stimlt.plot([],{{' ''r'',''Linewidth'',4'}});
-hx=xlabel('time [s]','Interpreter','none'); hold all;
-hy=ylabel('Firing Rate [spikes/sec]','Interpreter','none');
-set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-
-title({'Learning Trial Vs. Baseline Trial';'with 95% CIs'},'FontWeight','bold',...
-            'Fontsize',12,...
-            'FontName','Arial');
-h_legend=legend([h1(1) h2(1)],'\lambda_{1}(t)',['\lambda_{' num2str(lt) '}(t)']);
-pos = get(h_legend,'position');
-set(h_legend, 'position',[pos(1)+.03 pos(2)+.01 pos(3:4)]);
-

Example 4 - HIPPOCAMPAL PLACE CELL - RECEPTIVE FIELD ESTIMATION

Estimation of receptive fields of neurons is a very common data analysis problem in neuroscience. Here we use the nSTAT software to perform an estimation of the receptive fields of hippocampal place cells using a bivariate Gaussian model and Zernike polynomials. The number of zernike polynomials is based on "An Analysis of Hippocampal Spatio-Temporal Representations Using a Bayesian Algorithm for Neural Spike Train Decoding" Barbieri et. al 2005. The data used herein in was provided by Dr. Ricardo Barbieri on 2/28/2011.

Author: Iahn Cajigas

Date: 3/1/2011

Example Data

The x and y coordinates of a freely foraging rat in a circular environment (70cm in diameter and 30cm high walls) and a fixed visual cue. The x and y coordinates at the time when a spike was observed are marked in red. The position coordinates have been normalized to be between -1 and 1 to allow to simplify the analysis.

    close all;
-    load(strcat('PlaceCellDataAnimal1.mat'));
-    exampleCell = [2 21 25 49];
-%     exampleCell = 1:length(neuron);
-%     figure(1);
-    scrsz = get(0,'ScreenSize');
-    h=figure('OuterPosition',[scrsz(3)*.1 scrsz(4)*.1 scrsz(3)*.6 scrsz(4)*.9]);
-
-    for i=1:length(exampleCell)
-        subplot(2,2,i);
-        h1=plot(x,y,'b','Linewidth',.5); hold on;
-        h2=plot(neuron{exampleCell(i)}.xN,neuron{exampleCell(i)}.yN,'r.',...
-            'MarkerSize',7);
-        hx=xlabel('X Position'); hy=ylabel('Y Position');
-%         title(['Animal#1, Cell#' num2str(exampleCell(i))]);
-        title(['Cell#' num2str(exampleCell(i))],'FontWeight','bold',...
-            'Fontsize',12,'FontName','Arial');
-        set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-        set(gca,'xTick',-1:.5:1,'yTick',-1:.5:1); axis square;
-        if(i==4)
-            h_legend = legend([h1 h2],'Animal Path',...
-                'Location at time of spike');
-            pos = get(h_legend,'position');
-            set(h_legend, 'position',[pos(1)+.09 pos(2)+.06 pos(3:4)]);
-        end
-    end
-
-%% Example 4 - HIPPOCAMPAL PLACE CELL - RECEPTIVE FIELD ESTIMATION 
-% Estimation of receptive fields of neurons is a very common data analysis problem in neuroscience.
-% Here we use the nSTAT software to perform an estimation of the receptive fields of hippocampal
-% place cells using a bivariate Gaussian model and Zernike polynomials. The number of zernike polynomials 
-% is based on "An Analysis of Hippocampal Spatio-Temporal Representations Using a Bayesian Algorithm for Neural
-% Spike Train Decoding" Barbieri et. al 2005. The data used herein in was
-% provided by Dr. Ricardo Barbieri on 2/28/2011.
-%
-% *Author*: Iahn Cajigas 
-%
-% *Date*: 3/1/2011
-%%
-
-    
-%% Example Data
-% The x and y coordinates of a freely foraging rat in a circular environment (70cm in diameter and 30cm high walls) and a fixed visual cue. 
-% The x and y coordinates at the time when a spike was observed are marked
-% in red. The position coordinates have been normalized to be between -1
-% and 1 to allow to simplify the analysis. 
-    close all;
-    load(strcat('PlaceCellDataAnimal1.mat'));    
-    exampleCell = [2 21 25 49];
-%     exampleCell = 1:length(neuron);
-%     figure(1);
-    scrsz = get(0,'ScreenSize');
-    h=figure('OuterPosition',[scrsz(3)*.1 scrsz(4)*.1 scrsz(3)*.6 scrsz(4)*.9]);
-
-    for i=1:length(exampleCell)
-        subplot(2,2,i);
-        h1=plot(x,y,'b','Linewidth',.5); hold on;
-        h2=plot(neuron{exampleCell(i)}.xN,neuron{exampleCell(i)}.yN,'r.',...
-            'MarkerSize',7);
-        hx=xlabel('X Position'); hy=ylabel('Y Position'); 
-%         title(['Animal#1, Cell#' num2str(exampleCell(i))]);
-        title(['Cell#' num2str(exampleCell(i))],'FontWeight','bold',...
-            'Fontsize',12,'FontName','Arial');
-        set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-        set(gca,'xTick',-1:.5:1,'yTick',-1:.5:1); axis square;
-        if(i==4)
-    end
-        subplot(2,2,i);
-        h1=plot(x,y,'b','Linewidth',.5); hold on;
-        h2=plot(neuron{exampleCell(i)}.xN,neuron{exampleCell(i)}.yN,'r.',...
-            'MarkerSize',7);
-        hx=xlabel('X Position'); hy=ylabel('Y Position'); 
-%         title(['Animal#1, Cell#' num2str(exampleCell(i))]);
-        title(['Cell#' num2str(exampleCell(i))],'FontWeight','bold',...
-            'Fontsize',12,'FontName','Arial');
-        set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-        set(gca,'xTick',-1:.5:1,'yTick',-1:.5:1); axis square;
-        if(i==4)
-    end
-        subplot(2,2,i);
-        h1=plot(x,y,'b','Linewidth',.5); hold on;
-        h2=plot(neuron{exampleCell(i)}.xN,neuron{exampleCell(i)}.yN,'r.',...
-            'MarkerSize',7);
-        hx=xlabel('X Position'); hy=ylabel('Y Position'); 
-%         title(['Animal#1, Cell#' num2str(exampleCell(i))]);
-        title(['Cell#' num2str(exampleCell(i))],'FontWeight','bold',...
-            'Fontsize',12,'FontName','Arial');
-        set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-        set(gca,'xTick',-1:.5:1,'yTick',-1:.5:1); axis square;
-        if(i==4)
-    end
-        subplot(2,2,i);
-        h1=plot(x,y,'b','Linewidth',.5); hold on;
-        h2=plot(neuron{exampleCell(i)}.xN,neuron{exampleCell(i)}.yN,'r.',...
-            'MarkerSize',7);
-        hx=xlabel('X Position'); hy=ylabel('Y Position'); 
-%         title(['Animal#1, Cell#' num2str(exampleCell(i))]);
-        title(['Cell#' num2str(exampleCell(i))],'FontWeight','bold',...
-            'Fontsize',12,'FontName','Arial');
-        set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-        set(gca,'xTick',-1:.5:1,'yTick',-1:.5:1); axis square;
-        if(i==4)
-            h_legend = legend([h1 h2],'Animal Path',...
-                'Location at time of spike');
-            pos = get(h_legend,'position');
-            set(h_legend, 'position',[pos(1)+.09 pos(2)+.06 pos(3:4)]);
-        end
-    end
-

Analyze All Cells

numAnimals=2;
-CompilingHelpFile=1;
-if(~CompilingHelpFile)
-    for n=1:numAnimals
-        % load the data
-        clear x y neuron time nst tc tcc z;
-        load(strcat('PlaceCellDataAnimal',num2str(n),'.mat'));
-
-        % Create the spikeTrains for each cell
-        for i=1:length(neuron)
-            nst{i} = nspikeTrain(neuron{i}.spikeTimes);
-        end
-
-
-        % Convert to polar coordinates
-        [theta,r] = cart2pol(x,y);
-
-
-        % Evaluate the Zernike Polynomials
-        % Number of polynomials from "An Analysis of Hippocampal
-        % Spatio-Temporal Representations Using a Bayesian Algorithm for Neural
-        % Spike Train Decoding" Barbieri et. al 2005
-        cnt=0;
-        for l=0:3
-           for m=-l:l
-               if(~any(mod(l-m,2))) % otherwise the polynomial = 0
-                cnt = cnt+1;
-                z(:,cnt) = zernfun(l,m,r,theta,'norm');
-                % zernfun by Paul Fricker
-                % http://www.mathworks.com/matlabcentral/fileexchange/7687
-               end
-           end
-        end
-
-        % Data sampled at 30 Hz but just to be sure
-        delta=min(diff(time));
-        sampleRate = round(1/delta);
-        % Define Covariates for the analysis
-        baseline = Covariate(time,ones(length(x),1),'Baseline','time','s','',...
-                            {'mu'});
-        zernike  = Covariate(time,z,'Zernike','time','s','m',{'z1','z2','z3',...
-                            'z4','z5','z6','z7','z8','z9','z10'});
-        gaussian = Covariate(time,[x y x.^2 y.^2 x.*y],'Gaussian','time',...
-                            's','m',{'x','y','x^2','y^2','x*y'});
-        covarColl = CovColl({baseline,gaussian,zernike});
-
-        % Create the trial structure
-        spikeColl = nstColl(nst);
-        trial     = Trial(spikeColl,covarColl);
-
-
-        % Define how we want to analyze the data
-        tc{1} = TrialConfig({{'Baseline','mu'},{'Gaussian',...
-                            'x','y','x^2','y^2','x*y'}},sampleRate,[]);
-        tc{1}.setName('Gaussian');
-        tc{2} = TrialConfig({{'Zernike' 'z1','z2','z3','z4','z5','z6',...
-                            'z7','z8','z9','z10'}},sampleRate,[]);
-        tc{2}.setName('Zernike');
-        tcc = ConfigColl(tc);
-
-        % Perform Analysis (Commented to since data already saved)
-         results =Analysis.RunAnalysisForAllNeurons(trial,tcc,0);
-
-        % Save results
-            resStruct =FitResult.CellArrayToStructure(results);
-            filename = ['PlaceCellAnimal' num2str(n) 'Results'];
-            save(filename,'resStruct');
-    end
-end
-
    
-
- %% Analyze All Cells 
-numAnimals=2;
-CompilingHelpFile=1;
-if(~CompilingHelpFile)
-

View Summary Statistics

Note the Zernike Polynomials yield better fits in terms of decreased KS Statistics (less deviation from the 45 degree line), reduced AIC and reduced BIC across the majority of cells and for both animals

clear Summary;
-numAnimals =2;
-
-for n=1:numAnimals
-    resData=load(strcat('PlaceCellAnimal',num2str(n),'Results.mat'));
-    results = FitResult.fromStructure(resData.resStruct);
-    Summary{n} = FitResSummary(results);
-%     Summary{n}.plotSummary;
-end
-
%% View Summary Statistics
-% Note the Zernike Polynomials yield better fits in terms of decreased KS
-% Statistics (less deviation from the 45 degree line), reduced AIC and
-% reduced BIC across the majority of cells and for both animals
-clear Summary;
-numAnimals =2;
- 
-for n=1:numAnimals
-    resData=load(strcat('PlaceCellAnimal',num2str(n),'Results.mat'));
-    results = FitResult.fromStructure(resData.resStruct);
-    Summary{n} = FitResSummary(results);
-%     Summary{n}.plotSummary;
-end    
-    resData=load(strcat('PlaceCellAnimal',num2str(n),'Results.mat'));
-    results = FitResult.fromStructure(resData.resStruct);
-    Summary{n} = FitResSummary(results);
-%     Summary{n}.plotSummary;
-end    
-
close all;
-scrsz = get(0,'ScreenSize');
-h=figure('OuterPosition',[scrsz(3)*.1 scrsz(4)*.1 scrsz(3)*.6 scrsz(4)*.5]);
-subplot(1,3,1);
-maxLength = max([Summary{1}.numNeurons,Summary{2}.numNeurons]);
-dKS = nan(maxLength, 2);
-dKS(1:Summary{1}.numNeurons,1) = (Summary{1}.KSStats(:,1)-Summary{1}.KSStats(:,2)) ;
-dKS(1:Summary{2}.numNeurons,2) = (Summary{2}.KSStats(:,1)-Summary{2}.KSStats(:,2)) ;
-
-boxplot(dKS ,{'Animal 1', 'Animal 2'},'labelorientation','inline');
-title('\Delta KS Statistic','FontWeight','bold','FontSize',14,...
-    'FontName','Arial');
-
-
-subplot(1,3,2);
-dAIC = nan(maxLength, 2);
-dAIC(1:Summary{1}.numNeurons,1) = Summary{1}.getDiffAIC(1);
-dAIC(1:Summary{2}.numNeurons,2) = Summary{2}.getDiffAIC(1);
-
-boxplot(dAIC ,{'Animal 1', 'Animal 2'},'labelorientation','inline');
-title('\Delta AIC','FontWeight','bold','FontSize',14,'FontName','Arial');
-
-
-subplot(1,3,3);
-dBIC = nan(maxLength, 2);
-dBIC(1:Summary{1}.numNeurons,1) = Summary{1}.getDiffBIC(1);
-dBIC(1:Summary{2}.numNeurons,2) = Summary{2}.getDiffBIC(1);
-
-boxplot(dBIC ,{'Animal 1', 'Animal 2'},'labelorientation','inline'); %ylabel('\Delta BIC'); %xticklabel_rotate([],45,[],'Fontsize',6);
-title('\Delta BIC','FontWeight','bold','FontSize',14,'FontName','Arial');
-
-%  close all;
-
%%
-close all;
-scrsz = get(0,'ScreenSize');
-h=figure('OuterPosition',[scrsz(3)*.1 scrsz(4)*.1 scrsz(3)*.6 scrsz(4)*.5]);
-subplot(1,3,1);
-maxLength = max([Summary{1}.numNeurons,Summary{2}.numNeurons]);
-dKS = nan(maxLength, 2);
-dKS(1:Summary{1}.numNeurons,1) = (Summary{1}.KSStats(:,1)-Summary{1}.KSStats(:,2)) ;
-dKS(1:Summary{2}.numNeurons,2) = (Summary{2}.KSStats(:,1)-Summary{2}.KSStats(:,2)) ;
-
-boxplot(dKS ,{'Animal 1', 'Animal 2'},'labelorientation','inline'); 
-title('\Delta KS Statistic','FontWeight','bold','FontSize',14,...
-    'FontName','Arial');
-                  
-
-subplot(1,3,2);
-dAIC = nan(maxLength, 2);
-dAIC(1:Summary{1}.numNeurons,1) = Summary{1}.getDiffAIC(1);
-dAIC(1:Summary{2}.numNeurons,2) = Summary{2}.getDiffAIC(1);
-
-boxplot(dAIC ,{'Animal 1', 'Animal 2'},'labelorientation','inline'); 
-title('\Delta AIC','FontWeight','bold','FontSize',14,'FontName','Arial');
-                  
-
-subplot(1,3,3); 
-dBIC = nan(maxLength, 2);
-dBIC(1:Summary{1}.numNeurons,1) = Summary{1}.getDiffBIC(1);
-dBIC(1:Summary{2}.numNeurons,2) = Summary{2}.getDiffBIC(1);
-
-boxplot(dBIC ,{'Animal 1', 'Animal 2'},'labelorientation','inline'); %ylabel('\Delta BIC'); %xticklabel_rotate([],45,[],'Fontsize',6);
-title('\Delta BIC','FontWeight','bold','FontSize',14,'FontName','Arial');
-

Visualize the results

close all;
-% Define a grid
-[x_new,y_new]=meshgrid(-1:.01:1); %define new x and y
-y_new = flipud(y_new); x_new = fliplr(x_new);
-[theta_new,r_new] = cart2pol(x_new,y_new);
-
-%Data for the gaussian fit
-newData{1} =ones(size(x_new));
-newData{2} =x_new; newData{3} =y_new;
-newData{4} =x_new.^2; newData{5} =y_new.^2;
-newData{6} =x_new.*y_new;
-
-
-% Zernike polynomials only defined on the unit disk
-idx = r_new<=1;
-zpoly = cell(1,10);
-cnt=0;
-for l=0:3
-   for m=-l:l
-       if(~any(mod(l-m,2)))
-        cnt = cnt+1;
-        temp = nan(size(x_new));
-        temp(idx) = zernfun(l,m,r_new(idx),theta_new(idx),'norm');
-        zpoly{cnt} = temp;
-       end
-   end
-end
-
-
-
-for n=1:numAnimals
-
-    clear lambdaGaussian lambdaZernike;
-    load(strcat('PlaceCellDataAnimal',num2str(n),'.mat'));
-    resData=load(strcat('PlaceCellAnimal',num2str(n),'Results.mat'));
-    results = FitResult.fromStructure(resData.resStruct);
-
-    for i=1:length(neuron)
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-
-
-
-
-    % Plot the receptive fields
-    for i=1:length(neuron)
-        % 3d plot of an example place field
-
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-                tb=annotation(h4,'textbox',...
-                    [0.283261904761904 0.928571428571418 ...
-                    0.392857142857143 0.0595238095238095],...
-                    'String',{['Gaussian Place Fields - Animal#' ...
-                    num2str(n)]},'FitBoxToText','on','Fontsize',11,...
-                    'FontName','Arial','FontWeight','bold','LineStyle',...
-                    'none','HorizontalAlignment','center'); hold on;
-            end
-            subplot(7,7,i);
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-                annotation(h6,'textbox',...
-                    [0.283261904761904 0.928571428571418 ...
-                    0.392857142857143 0.0595238095238095],...
-                    'String',{['Gaussian Place Fields - Animal#' ...
-                    num2str(n)]},'FitBoxToText','on','Fontsize',11,...
-                    'FontName','Arial','FontWeight','bold','LineStyle',...
-                    'none','HorizontalAlignment','center'); hold on;
-            end
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-                annotation(h5,'textbox',...
-                    [0.303261904761904 0.928571428571418 ...
-                    0.392857142857143 0.0595238095238095],...
-                    'String',{['Zernike Place Fields - Animal#' ...
-                    num2str(n)]},'FitBoxToText','on','Fontsize',11,...
-                    'FontName','Arial','FontWeight','bold','LineStyle','none'); hold on;
-
-            end
-            subplot(7,7,i);
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-               annotation(h7,'textbox',...
-                    [0.303261904761904 0.928571428571418 ...
-                    0.392857142857143 0.0595238095238095],...
-                    'String',{['Zernike Place Fields - Animal#' ...
-                    num2str(n)]},'FitBoxToText','on','Fontsize',11,...
-                    'FontName','Arial','FontWeight','bold','LineStyle',...
-                    'none','HorizontalAlignment','center'); hold on;
-            end
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square;
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-
-
-end
-
-%  close all;
-    
-
-%% Visualize the results 
-close all;
-% Define a grid 
-[x_new,y_new]=meshgrid(-1:.01:1); %define new x and y
-y_new = flipud(y_new); x_new = fliplr(x_new);
-[theta_new,r_new] = cart2pol(x_new,y_new);
-
-%Data for the gaussian fit 
-newData{1} =ones(size(x_new));
-newData{2} =x_new; newData{3} =y_new;
-newData{4} =x_new.^2; newData{5} =y_new.^2;
-newData{6} =x_new.*y_new;
-
-
-% Zernike polynomials only defined on the unit disk
-idx = r_new<=1;
-zpoly = cell(1,10);
-cnt=0;
-for l=0:3
-   for m=-l:l 
-       if(~any(mod(l-m,2)))
-        cnt = cnt+1;
-        temp = nan(size(x_new));
-        temp(idx) = zernfun(l,m,r_new(idx),theta_new(idx),'norm');
-        zpoly{cnt} = temp;
-       end
-   end
-end
-   for m=-l:l 
-       if(~any(mod(l-m,2)))
-        cnt = cnt+1;
-        temp = nan(size(x_new));
-        temp(idx) = zernfun(l,m,r_new(idx),theta_new(idx),'norm');
-        zpoly{cnt} = temp;
-       end
-   end
-       if(~any(mod(l-m,2)))
-   end
-       if(~any(mod(l-m,2)))
-        cnt = cnt+1;
-        temp = nan(size(x_new));
-        temp(idx) = zernfun(l,m,r_new(idx),theta_new(idx),'norm');
-        zpoly{cnt} = temp;
-       end
-   end
-end
-   for m=-l:l 
-       if(~any(mod(l-m,2)))
-        cnt = cnt+1;
-        temp = nan(size(x_new));
-        temp(idx) = zernfun(l,m,r_new(idx),theta_new(idx),'norm');
-        zpoly{cnt} = temp;
-       end
-   end
-       if(~any(mod(l-m,2)))
-   end
-       if(~any(mod(l-m,2)))
-        cnt = cnt+1;
-        temp = nan(size(x_new));
-        temp(idx) = zernfun(l,m,r_new(idx),theta_new(idx),'norm');
-        zpoly{cnt} = temp;
-       end
-   end
-       if(~any(mod(l-m,2)))
-   end
-       if(~any(mod(l-m,2)))
-        cnt = cnt+1;
-        temp = nan(size(x_new));
-        temp(idx) = zernfun(l,m,r_new(idx),theta_new(idx),'norm');
-        zpoly{cnt} = temp;
-       end
-   end
-end
-   for m=-l:l 
-       if(~any(mod(l-m,2)))
-        cnt = cnt+1;
-        temp = nan(size(x_new));
-        temp(idx) = zernfun(l,m,r_new(idx),theta_new(idx),'norm');
-        zpoly{cnt} = temp;
-       end
-   end
-       if(~any(mod(l-m,2)))
-   end
-       if(~any(mod(l-m,2)))
-        cnt = cnt+1;
-        temp = nan(size(x_new));
-        temp(idx) = zernfun(l,m,r_new(idx),theta_new(idx),'norm');
-        zpoly{cnt} = temp;
-       end
-   end
-       if(~any(mod(l-m,2)))
-   end
-       if(~any(mod(l-m,2)))
-        cnt = cnt+1;
-        temp = nan(size(x_new));
-        temp(idx) = zernfun(l,m,r_new(idx),theta_new(idx),'norm');
-        zpoly{cnt} = temp;
-       end
-   end
-       if(~any(mod(l-m,2)))
-   end
-       if(~any(mod(l-m,2)))
-        cnt = cnt+1;
-        temp = nan(size(x_new));
-        temp(idx) = zernfun(l,m,r_new(idx),theta_new(idx),'norm');
-        zpoly{cnt} = temp;
-       end
-   end
-end
-
-
-
-for n=1:numAnimals 
-    
-    clear lambdaGaussian lambdaZernike;
-    load(strcat('PlaceCellDataAnimal',num2str(n),'.mat'));
-    resData=load(strcat('PlaceCellAnimal',num2str(n),'Results.mat'));
-    results = FitResult.fromStructure(resData.resStruct);
-    
-    for i=1:length(neuron)
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-
-   
-    
-    
-    % Plot the receptive fields
-    for i=1:length(neuron)
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-                tb=annotation(h4,'textbox',...
-                    [0.283261904761904 0.928571428571418 ...
-                    0.392857142857143 0.0595238095238095],...
-                    'String',{['Gaussian Place Fields - Animal#' ...
-                    num2str(n)]},'FitBoxToText','on','Fontsize',11,...
-                    'FontName','Arial','FontWeight','bold','LineStyle',...
-                    'none','HorizontalAlignment','center'); hold on;
-            end
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-                annotation(h5,'textbox',...
-                    [0.303261904761904 0.928571428571418 ...
-                    0.392857142857143 0.0595238095238095],...
-                    'String',{['Zernike Place Fields - Animal#' ...
-                    num2str(n)]},'FitBoxToText','on','Fontsize',11,...
-                    'FontName','Arial','FontWeight','bold','LineStyle','none'); hold on;
-                
-            end
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-            h4=figure(4);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-            h5=figure(5);
-            colormap('jet');
-            if(i==1)
-            subplot(7,7,i); 
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-
-  
-end
-    
-    clear lambdaGaussian lambdaZernike;
-    load(strcat('PlaceCellDataAnimal',num2str(n),'.mat'));
-    resData=load(strcat('PlaceCellAnimal',num2str(n),'Results.mat'));
-    results = FitResult.fromStructure(resData.resStruct);
-    
-    for i=1:length(neuron)
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-
-   
-    
-    
-    % Plot the receptive fields
-    for i=1:length(neuron)
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-                annotation(h6,'textbox',...
-                    [0.283261904761904 0.928571428571418 ...
-                    0.392857142857143 0.0595238095238095],...
-                    'String',{['Gaussian Place Fields - Animal#' ...
-                    num2str(n)]},'FitBoxToText','on','Fontsize',11,...
-                    'FontName','Arial','FontWeight','bold','LineStyle',...
-                    'none','HorizontalAlignment','center'); hold on;
-            end
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-               annotation(h7,'textbox',...
-                    [0.303261904761904 0.928571428571418 ...
-                    0.392857142857143 0.0595238095238095],...
-                    'String',{['Zernike Place Fields - Animal#' ...
-                    num2str(n)]},'FitBoxToText','on','Fontsize',11,...
-                    'FontName','Arial','FontWeight','bold','LineStyle',...
-                    'none','HorizontalAlignment','center'); hold on;
-            end
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-        % 3d plot of an example place field
-        
-
-        % 2d plot of all the cell's fields
-        if(n==1)
-        elseif(n==2)
-            h6=figure(6);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
-        axis square; set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-
-        if(n==1)
-        elseif(n==2)
-            h7=figure(7);
-            colormap('jet');
-            if(i==1)
-            subplot(6,7,i);
-        end
-        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
-        axis square; 
-        set(gca,'xtick',[],'ytick',[]);
-        set(gca, 'Box'         , 'off');
-    end
-
-  
-end
-
    clear lambdaGaussian lambdaZernike;
-    load(strcat('PlaceCellDataAnimal1.mat'));
-    resData=load(strcat('PlaceCellAnimal1Results.mat'));
-    results = FitResult.fromStructure(resData.resStruct);
-
-    for i=1:length(neuron)
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-
-
-
-%     h1=plot(x,y,'b');
-%     h2=plot(x,y,'g');
-    %
-    exampleCell = 25;
-%     figure(8);
-%     plot(x,y,'b',neuron{exampleCell}.xN,neuron{exampleCell}.yN,'r.');
-%     xlabel('x'); ylabel('y');
-%     title(['Animal#1, Cell#' num2str(exampleCell)]);
-%
-    close all;
-    h9=figure(9);
-    h_mesh = mesh(x_new,y_new,lambdaGaussian{exampleCell},'AlphaData',0);
-    get(h_mesh,'AlphaData');
-    set(h_mesh,'FaceAlpha',0.2,'EdgeAlpha',0.2,'EdgeColor','b');
-    hold on;
-    h_mesh = mesh(x_new,y_new,lambdaZernike{exampleCell},'AlphaData',0);
-    get(h_mesh,'AlphaData');
-    set(h_mesh,'FaceAlpha',0.2,'EdgeAlpha',0.2,'EdgeColor','g');
-
-
-%     h_legend=legend('\lambda_{Gaussian}','\lambda_{Zernike}');
-%     set(h_legend,'FontSize',20);
-    plot(x,y,neuron{exampleCell}.xN,neuron{exampleCell}.yN,'r.');
-    axis tight square;
-    xlabel('x position'); ylabel('y position');
-    title(['Animal#1, Cell#' num2str(exampleCell)],'FontWeight','bold',...
-        'Fontsize',12,'FontName','Arial');
-    hx=get(gca,'XLabel');  hy=get(gca,'YLabel');
-    set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-
-
-%%
-    clear lambdaGaussian lambdaZernike;
-    load(strcat('PlaceCellDataAnimal1.mat'));
-    resData=load(strcat('PlaceCellAnimal1Results.mat'));
-    results = FitResult.fromStructure(resData.resStruct);
-    
-    for i=1:length(neuron)
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-        % Evaluate our fits using the new data and the estimated parameters
-        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
-        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
-    end
-
-    
-    
-%     h1=plot(x,y,'b');
-%     h2=plot(x,y,'g');
-    %
-    exampleCell = 25;
-%     figure(8);
-%     plot(x,y,'b',neuron{exampleCell}.xN,neuron{exampleCell}.yN,'r.');
-%     xlabel('x'); ylabel('y'); 
-%     title(['Animal#1, Cell#' num2str(exampleCell)]);
-%     
-    close all;
-    h9=figure(9);
-    h_mesh = mesh(x_new,y_new,lambdaGaussian{exampleCell},'AlphaData',0);
-    get(h_mesh,'AlphaData');
-    set(h_mesh,'FaceAlpha',0.2,'EdgeAlpha',0.2,'EdgeColor','b');
-    hold on;
-    h_mesh = mesh(x_new,y_new,lambdaZernike{exampleCell},'AlphaData',0);
-    get(h_mesh,'AlphaData');
-    set(h_mesh,'FaceAlpha',0.2,'EdgeAlpha',0.2,'EdgeColor','g');
-
-    
-%     h_legend=legend('\lambda_{Gaussian}','\lambda_{Zernike}');
-%     set(h_legend,'FontSize',20);
-    plot(x,y,neuron{exampleCell}.xN,neuron{exampleCell}.yN,'r.');
-    axis tight square;
-    xlabel('x position'); ylabel('y position');
-    title(['Animal#1, Cell#' num2str(exampleCell)],'FontWeight','bold',...
-        'Fontsize',12,'FontName','Arial');
-    hx=get(gca,'XLabel');  hy=get(gca,'YLabel');
-    set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-

Example 5 - STIMULUS DECODING

In this example we show how to decode a univariate and a bivariate stimulus based on a point process observations using nSTAT. Even though due to the simulated nature of the data, we know the exact condition intensity function, we estimate the parameters before moving on to the decoding stage.

Generate the conditional Intensity Function

    close all; clear all;
-    delta = 0.001; Tmax = 1;
-    time = 0:delta:Tmax;
-    numRealizations = 20;
-    f=2; b1=randn(numRealizations,1);b0=log(10*delta)+randn(numRealizations,1);
-    x = sin(2*pi*f*time);
-    clear nst;
-    for i=1:numRealizations
-        expData = exp(b1(i)*x+b0(i));
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-            lambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-        else
-            tempLambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-            lambda = lambda.merge(tempLambda);
-        end
-
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1);
-        nst{i} = spikeColl.getNST(1);
-    end
-        spikeColl = nstColl(nst);scrsz = get(0,'ScreenSize');
-        h=figure('Position',[scrsz(3)*.1 scrsz(4)*.1 ...
-            scrsz(3)*.6 scrsz(4)*.8]);
-%         figure;
-        subplot(3,1,1); plot(time,x,'k');
-        set(gca,'xtick',[],'xtickLabel',[]); ylabel('Stimulus');
-            hx=get(gca,'XLabel');  hy=get(gca,'YLabel');
-            set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-            title('Driving Stimulus','FontWeight','bold',...
-                'FontSize',14,'FontName','Arial');
-        subplot(3,1,2); lambda.plot([],{{' ''k'',''Linewidth'',1'}});
-            legend off;
-            hy=ylabel('Firing Rate [spikes/sec]', 'Interpreter','none');
-            hx=xlabel('','Interpreter','none');
-            set([hx, hy],'FontName', 'Arial','FontSize',12,...
-                'FontWeight','bold');
-            set(gca,'xtickLabel',[]);
-            title('Conditional Intensity Functions','FontWeight',...
-                'bold','FontSize',14,'FontName','Arial');
-
-        subplot(3,1,3); spikeColl.plot;
-            set(gca,'ytick',0:10:numRealizations,'ytickLabel',...
-                0:10:numRealizations);
-            xlabel('time [s]','Interpreter','none');
-            ylabel('Cell Number','Interpreter','none');
-            hx=get(gca,'XLabel');  hy=get(gca,'YLabel');
-            set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-            title('Point Process Sample Paths','FontWeight',...
-                'bold','FontSize',14,'FontName','Arial');
-
-stim = Covariate(time,sin(2*pi*f*time),'Stimulus','time','s','V',{'stim'});
-baseline = Covariate(time,ones(length(time),1),'Baseline','time','s','',...
-                    {'constant'});
-
-%     close all;
-
-
-    
-%% Example 5 - STIMULUS DECODING
-% In this example we show how to decode a univariate and a bivariate
-% stimulus based on a point process observations using nSTAT. Even though 
-% due to the simulated nature of the data, we know the exact condition 
-% intensity function, we estimate the parameters before moving on to the 
-% decoding stage.
-%% Generate the conditional Intensity Function
-
-    close all; clear all;
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\private\evalmxdom.m' could not be
-cleared because it contains MATLAB code that is currently executing. 
-Warning: The file
-'C:\Users\Developer\Dropbox\GitHub\nSTAT\helpfiles\nSTATPaperExamples.m' could
-not be cleared because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\mdbpublish.m' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\publish.p' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\private\evalmxdom.m' could not be
-cleared because it contains MATLAB code that is currently executing. 
-Warning: The file
-'C:\Users\Developer\Dropbox\GitHub\nSTAT\helpfiles\nSTATPaperExamples.m' could
-not be cleared because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\mdbpublish.m' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\publish.p' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\private\evalmxdom.m' could not be
-cleared because it contains MATLAB code that is currently executing. 
-Warning: The file
-'C:\Users\Developer\Dropbox\GitHub\nSTAT\helpfiles\nSTATPaperExamples.m' could
-not be cleared because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\mdbpublish.m' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\publish.p' could not be cleared
-because it contains MATLAB code that is currently executing. 
-    delta = 0.001; Tmax = 1;
-    time = 0:delta:Tmax;
-    numRealizations = 20;
-    f=2; b1=randn(numRealizations,1);b0=log(10*delta)+randn(numRealizations,1);
-    x = sin(2*pi*f*time);
-    clear nst;
-    for i=1:numRealizations
-        expData = exp(b1(i)*x+b0(i)); 
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-            lambda = Covariate(time,lambdaData./delta, ...
-        end       
-        
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1); 
-        nst{i} = spikeColl.getNST(1);
-    end
-        expData = exp(b1(i)*x+b0(i)); 
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-        else 
-            tempLambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-            lambda = lambda.merge(tempLambda);
-        end       
-        
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1); 
-        nst{i} = spikeColl.getNST(1);
-    end
-        expData = exp(b1(i)*x+b0(i)); 
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-        else 
-            tempLambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-            lambda = lambda.merge(tempLambda);
-        end       
-        
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1); 
-        nst{i} = spikeColl.getNST(1);
-    end
-        expData = exp(b1(i)*x+b0(i)); 
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-        else 
-            tempLambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-            lambda = lambda.merge(tempLambda);
-        end       
-        
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1); 
-        nst{i} = spikeColl.getNST(1);
-    end
-        expData = exp(b1(i)*x+b0(i)); 
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-        else 
-            tempLambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-            lambda = lambda.merge(tempLambda);
-        end       
-        
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1); 
-        nst{i} = spikeColl.getNST(1);
-    end
-        expData = exp(b1(i)*x+b0(i)); 
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-        else 
-            tempLambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-            lambda = lambda.merge(tempLambda);
-        end       
-        
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1); 
-        nst{i} = spikeColl.getNST(1);
-    end
-        expData = exp(b1(i)*x+b0(i)); 
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-        else 
-            tempLambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-            lambda = lambda.merge(tempLambda);
-        end       
-        
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1); 
-        nst{i} = spikeColl.getNST(1);
-    end
-        expData = exp(b1(i)*x+b0(i)); 
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-        else 
-            tempLambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-            lambda = lambda.merge(tempLambda);
-        end       
-        
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1); 
-        nst{i} = spikeColl.getNST(1);
-    end
-        expData = exp(b1(i)*x+b0(i)); 
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-        else 
-            tempLambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-            lambda = lambda.merge(tempLambda);
-        end       
-        
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1); 
-        nst{i} = spikeColl.getNST(1);
-    end
-        expData = exp(b1(i)*x+b0(i)); 
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-        else 
-            tempLambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-            lambda = lambda.merge(tempLambda);
-        end       
-        
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1); 
-        nst{i} = spikeColl.getNST(1);
-    end
-        expData = exp(b1(i)*x+b0(i)); 
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-        else 
-            tempLambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-            lambda = lambda.merge(tempLambda);
-        end       
-        
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1); 
-        nst{i} = spikeColl.getNST(1);
-    end
-        expData = exp(b1(i)*x+b0(i)); 
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-        else 
-            tempLambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-            lambda = lambda.merge(tempLambda);
-        end       
-        
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1); 
-        nst{i} = spikeColl.getNST(1);
-    end
-        expData = exp(b1(i)*x+b0(i)); 
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-        else 
-            tempLambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-            lambda = lambda.merge(tempLambda);
-        end       
-        
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1); 
-        nst{i} = spikeColl.getNST(1);
-    end
-        expData = exp(b1(i)*x+b0(i)); 
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-        else 
-            tempLambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-            lambda = lambda.merge(tempLambda);
-        end       
-        
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1); 
-        nst{i} = spikeColl.getNST(1);
-    end
-        expData = exp(b1(i)*x+b0(i)); 
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-        else 
-            tempLambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-            lambda = lambda.merge(tempLambda);
-        end       
-        
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1); 
-        nst{i} = spikeColl.getNST(1);
-    end
-        expData = exp(b1(i)*x+b0(i)); 
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-        else 
-            tempLambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-            lambda = lambda.merge(tempLambda);
-        end       
-        
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1); 
-        nst{i} = spikeColl.getNST(1);
-    end
-        expData = exp(b1(i)*x+b0(i)); 
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-        else 
-            tempLambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-            lambda = lambda.merge(tempLambda);
-        end       
-        
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1); 
-        nst{i} = spikeColl.getNST(1);
-    end
-        expData = exp(b1(i)*x+b0(i)); 
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-        else 
-            tempLambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-            lambda = lambda.merge(tempLambda);
-        end       
-        
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1); 
-        nst{i} = spikeColl.getNST(1);
-    end
-        expData = exp(b1(i)*x+b0(i)); 
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-        else 
-            tempLambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-            lambda = lambda.merge(tempLambda);
-        end       
-        
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1); 
-        nst{i} = spikeColl.getNST(1);
-    end
-        expData = exp(b1(i)*x+b0(i)); 
-        lambdaData = expData./(1+expData);
-
-        if(i==1)
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-        else 
-            tempLambda = Covariate(time,lambdaData./delta, ...
-                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
-                {{' ''b'', ''LineWidth'' ,2'}});
-            lambda = lambda.merge(tempLambda);
-        end       
-        
-        spikeColl = CIF.simulateCIFByThinningFromLambda(...
-            lambda.getSubSignal(i),1); 
-        nst{i} = spikeColl.getNST(1);
-    end
-        spikeColl = nstColl(nst);scrsz = get(0,'ScreenSize');
-        h=figure('Position',[scrsz(3)*.1 scrsz(4)*.1 ...
-            scrsz(3)*.6 scrsz(4)*.8]);
-%         figure;
-        subplot(3,1,1); plot(time,x,'k'); 
-        set(gca,'xtick',[],'xtickLabel',[]); ylabel('Stimulus');
-            hx=get(gca,'XLabel');  hy=get(gca,'YLabel');
-            set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-            title('Driving Stimulus','FontWeight','bold',...
-                'FontSize',14,'FontName','Arial');
-        subplot(3,1,2); lambda.plot([],{{' ''k'',''Linewidth'',1'}}); 
-            legend off; 
-            hy=ylabel('Firing Rate [spikes/sec]', 'Interpreter','none');
-            hx=xlabel('','Interpreter','none');
-            set([hx, hy],'FontName', 'Arial','FontSize',12,...
-                'FontWeight','bold');
-            set(gca,'xtickLabel',[]);
-            title('Conditional Intensity Functions','FontWeight',...
-                'bold','FontSize',14,'FontName','Arial');
- 
-        subplot(3,1,3); spikeColl.plot; 
-            set(gca,'ytick',0:10:numRealizations,'ytickLabel',...
-                0:10:numRealizations); 
-            xlabel('time [s]','Interpreter','none');
-            ylabel('Cell Number','Interpreter','none');
-            hx=get(gca,'XLabel');  hy=get(gca,'YLabel');
-            set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-            title('Point Process Sample Paths','FontWeight',...
-                'bold','FontSize',14,'FontName','Arial');
-
-stim = Covariate(time,sin(2*pi*f*time),'Stimulus','time','s','V',{'stim'});
-baseline = Covariate(time,ones(length(time),1),'Baseline','time','s','',...
-
close all;
-
-clear lambdaCIF;
-spikeColl.resample(1/delta);
-dN=spikeColl.dataToMatrix;
-
-% Make noise according to the dynamic range of the stimulus
-Q=std(stim.data(2:end)-stim.data(1:end-1));
-Px0=.1; A=1;
-x0 = x(:,1); yT=x(:,end);
-Pi0 = eps*eye(size(x0,1),size(x0,1));
-PiT = eps*eye(size(x0,1),size(x0,1));
-
-
-[x_p, W_p, x_u, W_u] = DecodingAlgorithms.PPDecodeFilterLinear(A, ...
-    Q, dN',b0,b1','binomial',delta);
-%
-h=figure('Position',[scrsz(3)*.1 scrsz(4)*.1 scrsz(3)*.8 scrsz(4)*.6]);
-zVal=1.96;
-ciLower = min(x_u(1:end)-zVal*sqrt(squeeze(W_u(1:end)))',...
-    x_u(1:end)+zVal*sqrt(squeeze(W_u(1:end))'));
-ciUpper = max(x_u(1:end)-zVal*sqrt(squeeze(W_u(1:end)))',...
-    x_u(1:end)+zVal*sqrt(squeeze(W_u(1:end))'));
-
-estimatedStimulus = Covariate(time,x_u(1:end),'\hat{x}(t)','time','s','');
-CI= ConfidenceInterval(time,[ciLower', ciUpper'],'\hat{x}(t)','time','s','');
-estimatedStimulus.setConfInterval(CI);
-
-% hold all;
-% hEst=plot(time,x_u(1:end),'b','Linewidth',2); hold on;
-% plot(time, [ciUpper', ciLower'],'b');
-
-hEst = estimatedStimulus.plot([],{{' ''k'',''Linewidth'',4'}});
-hStim=stim.plot([],{{' ''b'',''Linewidth'',4'}});
-legend off;
-h_legend=legend([hEst(1) hStim],'Decoded','Actual');
-set(h_legend,'Interpreter','none');
-set(h_legend,'FontSize',22);
-title(['Decoded Stimulus +/- 95% CIs with ' num2str(numRealizations) ' cells'],...
-    'FontWeight','bold','Fontsize',22,'FontName','Arial');
-xlabel('time [s]','Interpreter','none');
-ylabel('Stimulus','Interpreter','none');
-hx=get(gca,'XLabel');  hy=get(gca,'YLabel');
-set([hx, hy],'FontName', 'Arial','FontSize',22,'FontWeight','bold');
-
                    {'constant'});
-
-%     close all;
-%%
-close all;
-
-clear lambdaCIF;
-spikeColl.resample(1/delta);
-dN=spikeColl.dataToMatrix;
-
-% Make noise according to the dynamic range of the stimulus
-Q=std(stim.data(2:end)-stim.data(1:end-1));
-Px0=.1; A=1;
-x0 = x(:,1); yT=x(:,end);
-Pi0 = eps*eye(size(x0,1),size(x0,1));
-PiT = eps*eye(size(x0,1),size(x0,1));
-
-
-[x_p, W_p, x_u, W_u] = DecodingAlgorithms.PPDecodeFilterLinear(A, ...
-    Q, dN',b0,b1','binomial',delta);
-%
-h=figure('Position',[scrsz(3)*.1 scrsz(4)*.1 scrsz(3)*.8 scrsz(4)*.6]);
-zVal=1.96;
-ciLower = min(x_u(1:end)-zVal*sqrt(squeeze(W_u(1:end)))',...
-    x_u(1:end)+zVal*sqrt(squeeze(W_u(1:end))'));
-ciUpper = max(x_u(1:end)-zVal*sqrt(squeeze(W_u(1:end)))',...
-    x_u(1:end)+zVal*sqrt(squeeze(W_u(1:end))'));
-
-estimatedStimulus = Covariate(time,x_u(1:end),'\hat{x}(t)','time','s','');
-CI= ConfidenceInterval(time,[ciLower', ciUpper'],'\hat{x}(t)','time','s','');
-estimatedStimulus.setConfInterval(CI);
-
-% hold all;            
-% hEst=plot(time,x_u(1:end),'b','Linewidth',2); hold on;
-% plot(time, [ciUpper', ciLower'],'b');
-
-hEst = estimatedStimulus.plot([],{{' ''k'',''Linewidth'',4'}});
-hStim=stim.plot([],{{' ''b'',''Linewidth'',4'}}); 
-legend off;
-h_legend=legend([hEst(1) hStim],'Decoded','Actual');
-set(h_legend,'Interpreter','none');
-set(h_legend,'FontSize',22);
-title(['Decoded Stimulus +/- 95% CIs with ' num2str(numRealizations) ' cells'],...
-    'FontWeight','bold','Fontsize',22,'FontName','Arial');
-xlabel('time [s]','Interpreter','none');
-ylabel('Stimulus','Interpreter','none');
-hx=get(gca,'XLabel');  hy=get(gca,'YLabel');
-set([hx, hy],'FontName', 'Arial','FontSize',22,'FontWeight','bold');
-

Example 5b - Arm reaching to target Simulation

See L. Srinivasan, U. T. Eden, A. S. Willsky, and E. N. Brown, "A state-space analysis for reconstruction of goal-directed movements using neural signals.," Neural computation, vol. 18, no. 10, pp. 2465?2494, Oct. 2006.

    close all;
-    clear all;
-    %Process noise covariance only drives the movement velocity
-    q=1e-4;
-    Q=diag([1e-12 1e-12 q q]);
-
-    delta = .001;        % Time increment
-    r=1e-6;   % in meters^2
-    p=1e-6;    % in meters^2/s^2
-    PiT=diag([r r p p]); % Uncertainty in the target state
-    Pi0=PiT;
-    T=2;                 % Reach Duration
-
-    x0 = [0;0;0;0];     % Initial Position and velocities (states)
-    xT = [-.35;.2; 0;0];% Final Target
-    time=0:delta:T;     % time vector
-
-    A=[1 0 delta 0    ; %State transition matrix
-       0 1 0     delta;
-       0 0 1     0    ;
-       0 0 0     1    ];
-
-    x=zeros(4,length(time));
-
-
-% Simulate a reach trajectory
-% Differs from reference by multiplication by delta instead of division so
-% that the velocity has units of meters per second
-    R=chol(Q);
-    L=chol(PiT);
-    for k=1:length(time)
-        if(k==1)
-            x(:,k)=x0;
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-    xT =x(:,end); % The target generated by the model
-    yT=xT;        % Assume we have observed the actual target position with uncertainty PiT
-
-    %Define Q according to the dynamic range of the movement above
-    Q=diag(var(diff(x,[],2),[],2))*100;
-
-    % Plot the movement trajectories and the hand path
-    scrsz = get(0,'ScreenSize');
-    fig1=figure('OuterPosition',[scrsz(3)*.1 scrsz(4)*.1 ...
-        scrsz(3)*.8 scrsz(4)*.8]);
-    %Plot The movement path
-    subplot(4,2,[1 3]);
-    plot(100*x(1,:),100*x(2,:),'k','Linewidth',2);
-    xlabel('X Position [cm]'); ylabel('Y Position [cm]');
-    hx=get(gca,'XLabel');  hy=get(gca,'YLabel');
-    set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-    title('Reach Path','FontWeight','bold','Fontsize',14,'FontName','Arial');
-    hold on;
-    axis([sort([100*x0(1)+5, 100*xT(1)-5]), sort([100*x0(2)-5, 100*xT(2)+5])]);
-    h1=plot(100*x(1,1),100*x(2,1),'bo','MarkerSize',14);
-    h2=plot(100*x(1,end),100*x(2,end),'ro','MarkerSize',14);
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); h1=plot(time,100*x(1,:),'k','Linewidth',2); hold on;
-    h2=plot(time,100*x(2,:),'k-.','Linewidth',2);
-    h_legend=legend([h1,h2],'x','y','Location','NorthEast');
-    set(h_legend,'FontSize',14)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)+.06 pos(2)+.01 pos(3:4)]);
-    hx=xlabel('time [s]'); hy=ylabel('Position [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-    % Plot the velocity profiles
-
-    subplot(4,2,7);
-    h1=plot(time,100*x(3,:),'k','Linewidth',2); hold on;
-    h2=plot(time,100*x(4,:),'k-.','Linewidth',2);
-    h_legend=legend([h1 h2],'v_x','v_y','Location','NorthEast');
-    xlabel('time [s]');
-    set(h_legend,'FontSize',14);
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)+.06 pos(2)+.01 pos(3:4)]);
-    hx=xlabel('time [s]'); hy=ylabel('Velocity [cm/s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-    %
-
-    gamma=0;
-    windowTimes=[0, 0.001];
-
-
-% Simulate neural responses
-    % logit(lambda_i*delta) = mu_i + b_x_i*v_x + b_y_i*v_y
-    % logit(lambda_i*delta) = X_i*beta_i;
-    numCells = 20;
-    bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi);
-    meanMu = log(10*delta); % baseline firing rate -10Hz
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1)
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst;
-    for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-             lambdaData = tempData;
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}});
-         legend off; hold all; % Plot the CIF
-
-
-
-    end
-    title('Neural Conditional Intensity Functions','FontWeight',...
-        'bold','Fontsize',14,'FontName','Arial');
-    hx=xlabel('time [s]','Interpreter','none');
-    hy=ylabel('Firing Rate [spikes/sec]','Interpreter','none');
-    set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    subplot(4,2,[2,4]); spikeColl.plot;
-    set(gca,'xtick',[],'xtickLabel',[]);
-    title('Neural Raster','FontWeight','bold','Fontsize',14,...
-        'FontName','Arial');
-    hx=xlabel('time [s]','Interpreter','none');
-    hy=ylabel('Cell Number','Interpreter','none');
-    set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-
-%     close all;
-
-
-    
-%% Example 5b - Arm reaching to target Simulation
-% See 
-% L. Srinivasan, U. T. Eden, A. S. Willsky, and E. N. Brown, 
-% "A state-space analysis for reconstruction of goal-directed movements 
-% using neural signals.," Neural computation, vol. 18, no. 10, pp. 2465?2494, Oct. 2006.
-
-    close all;
-    clear all;
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\private\evalmxdom.m' could not be
-cleared because it contains MATLAB code that is currently executing. 
-Warning: The file
-'C:\Users\Developer\Dropbox\GitHub\nSTAT\helpfiles\nSTATPaperExamples.m' could
-not be cleared because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\mdbpublish.m' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\publish.p' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\private\evalmxdom.m' could not be
-cleared because it contains MATLAB code that is currently executing. 
-Warning: The file
-'C:\Users\Developer\Dropbox\GitHub\nSTAT\helpfiles\nSTATPaperExamples.m' could
-not be cleared because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\mdbpublish.m' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\publish.p' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\private\evalmxdom.m' could not be
-cleared because it contains MATLAB code that is currently executing. 
-Warning: The file
-'C:\Users\Developer\Dropbox\GitHub\nSTAT\helpfiles\nSTATPaperExamples.m' could
-not be cleared because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\mdbpublish.m' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\publish.p' could not be cleared
-because it contains MATLAB code that is currently executing. 
-    %Process noise covariance only drives the movement velocity
-    q=1e-4;
-    Q=diag([1e-12 1e-12 q q]); 
-
-    delta = .001;        % Time increment
-    r=1e-6;   % in meters^2 
-    p=1e-6;    % in meters^2/s^2
-    PiT=diag([r r p p]); % Uncertainty in the target state
-    Pi0=PiT;
-    T=2;                 % Reach Duration
-
-    x0 = [0;0;0;0];     % Initial Position and velocities (states)
-    xT = [-.35;.2; 0;0];% Final Target
-    time=0:delta:T;     % time vector
-
-    A=[1 0 delta 0    ; %State transition matrix
-       0 1 0     delta;
-       0 0 1     0    ;
-       0 0 0     1    ];
-
-    x=zeros(4,length(time));
-
-
-% Simulate a reach trajectory
-% Differs from reference by multiplication by delta instead of division so
-% that the velocity has units of meters per second
-    R=chol(Q);
-    L=chol(PiT);
-    for k=1:length(time)
-        if(k==1)
-            x(:,k)=x0;
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-        if(k==1)
-        else
-             x(:,k)=A*x(:,k-1)+...
-                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
-                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
-            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
-        end
-
-    end
-    xT =x(:,end); % The target generated by the model
-    yT=xT;        % Assume we have observed the actual target position with uncertainty PiT
-
-    %Define Q according to the dynamic range of the movement above 
-    Q=diag(var(diff(x,[],2),[],2))*100;
-
-    % Plot the movement trajectories and the hand path
-    scrsz = get(0,'ScreenSize');
-    fig1=figure('OuterPosition',[scrsz(3)*.1 scrsz(4)*.1 ...
-        scrsz(3)*.8 scrsz(4)*.8]);
-    %Plot The movement path
-    subplot(4,2,[1 3]); 
-    plot(100*x(1,:),100*x(2,:),'k','Linewidth',2); 
-    xlabel('X Position [cm]'); ylabel('Y Position [cm]');
-    hx=get(gca,'XLabel');  hy=get(gca,'YLabel');
-    set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-    title('Reach Path','FontWeight','bold','Fontsize',14,'FontName','Arial');
-    hold on; 
-    axis([sort([100*x0(1)+5, 100*xT(1)-5]), sort([100*x0(2)-5, 100*xT(2)+5])]);
-    h1=plot(100*x(1,1),100*x(2,1),'bo','MarkerSize',14); 
-    h2=plot(100*x(1,end),100*x(2,end),'ro','MarkerSize',14); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); h1=plot(time,100*x(1,:),'k','Linewidth',2); hold on;
-    h2=plot(time,100*x(2,:),'k-.','Linewidth',2); 
-    h_legend=legend([h1,h2],'x','y','Location','NorthEast'); 
-    set(h_legend,'FontSize',14)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)+.06 pos(2)+.01 pos(3:4)]);
-    hx=xlabel('time [s]'); hy=ylabel('Position [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-    % Plot the velocity profiles 
-
-    subplot(4,2,7);   
-    h1=plot(time,100*x(3,:),'k','Linewidth',2); hold on;
-    h2=plot(time,100*x(4,:),'k-.','Linewidth',2); 
-    h_legend=legend([h1 h2],'v_x','v_y','Location','NorthEast'); 
-    xlabel('time [s]');
-    set(h_legend,'FontSize',14);
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)+.06 pos(2)+.01 pos(3:4)]);
-    hx=xlabel('time [s]'); hy=ylabel('Velocity [cm/s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-    %
-
-    gamma=0;
-    windowTimes=[0, 0.001];
-
-
-% Simulate neural responses
-    % logit(lambda_i*delta) = mu_i + b_x_i*v_x + b_y_i*v_y
-    % logit(lambda_i*delta) = X_i*beta_i;
-    numCells = 20; 
-    bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta); % baseline firing rate -10Hz
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst;
-    for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}}); 
-         legend off; hold all; % Plot the CIF
-         
-         
-         
-    end
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}}); 
-         legend off; hold all; % Plot the CIF
-         
-         
-         
-    end
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}}); 
-         legend off; hold all; % Plot the CIF
-         
-         
-         
-    end
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}}); 
-         legend off; hold all; % Plot the CIF
-         
-         
-         
-    end
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}}); 
-         legend off; hold all; % Plot the CIF
-         
-         
-         
-    end
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}}); 
-         legend off; hold all; % Plot the CIF
-         
-         
-         
-    end
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}}); 
-         legend off; hold all; % Plot the CIF
-         
-         
-         
-    end
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}}); 
-         legend off; hold all; % Plot the CIF
-         
-         
-         
-    end
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}}); 
-         legend off; hold all; % Plot the CIF
-         
-         
-         
-    end
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}}); 
-         legend off; hold all; % Plot the CIF
-         
-         
-         
-    end
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}}); 
-         legend off; hold all; % Plot the CIF
-         
-         
-         
-    end
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}}); 
-         legend off; hold all; % Plot the CIF
-         
-         
-         
-    end
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}}); 
-         legend off; hold all; % Plot the CIF
-         
-         
-         
-    end
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}}); 
-         legend off; hold all; % Plot the CIF
-         
-         
-         
-    end
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}}); 
-         legend off; hold all; % Plot the CIF
-         
-         
-         
-    end
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}}); 
-         legend off; hold all; % Plot the CIF
-         
-         
-         
-    end
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}}); 
-         legend off; hold all; % Plot the CIF
-         
-         
-         
-    end
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}}); 
-         legend off; hold all; % Plot the CIF
-         
-         
-         
-    end
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}}); 
-         legend off; hold all; % Plot the CIF
-         
-         
-         
-    end
-         tempData  = exp(dataMat*coeffs(i,:)');
-
-         if(strcmp(fitType,'poisson'))
-         else
-            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
-             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-         subplot(4,2,[6 8]);
-         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}}); 
-         legend off; hold all; % Plot the CIF
-         
-         
-         
-    end
-    title('Neural Conditional Intensity Functions','FontWeight',...
-        'bold','Fontsize',14,'FontName','Arial');
-    hx=xlabel('time [s]','Interpreter','none'); 
-    hy=ylabel('Firing Rate [spikes/sec]','Interpreter','none');
-    set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-    
-    subplot(4,2,[2,4]); spikeColl.plot;
-    set(gca,'xtick',[],'xtickLabel',[]);
-    title('Neural Raster','FontWeight','bold','Fontsize',14,...
-        'FontName','Arial');
-    hx=xlabel('time [s]','Interpreter','none'); 
-    hy=ylabel('Cell Number','Interpreter','none');
-    set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-
close all;
-numExamples=20;
-scrsz = get(0,'ScreenSize');
-fig1=figure('OuterPosition',[scrsz(3)*.1 scrsz(4)*.1 ...
-    scrsz(3)*.6 scrsz(4)*.9]);
-for k=1:numExamples
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi);
-    meanMu = log(10*delta);  % baseline firing rate
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1)
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-
-
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-            lambdaData = tempData;
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-        subplot(4,2,1:4);h1=plot(100*x(1,:),100*x(2,:),'k','LineWidth',3);
-        hold on;
-        axis([sort([100*x0(1)+5, 100*xT(1)-5]), ...
-            sort([100*x0(2)-5, 100*xT(2)+5])]);
-        title('Estimated vs. Actual Reach Paths',...
-            'FontWeight','bold','Fontsize',12,'FontName','Arial');
-    end
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g');
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10);
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5);
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b');
-    h3=plot(time,100*x_uf(1,:)','g');
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6);
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b');
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7);
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8);
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b');
-    h3=plot(time,100*x_uf(4,:)','g');
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-
-end
-
-%     close all;
-
-%     close all;
-
-    
-%%
-close all;
-numExamples=20;
-scrsz = get(0,'ScreenSize');
-fig1=figure('OuterPosition',[scrsz(3)*.1 scrsz(4)*.1 ...
-    scrsz(3)*.6 scrsz(4)*.9]);
-for k=1:numExamples
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-    
-    
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-    
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g'); 
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); 
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b'); 
-    h3=plot(time,100*x_uf(1,:)','g'); 
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6); 
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b'); 
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]'); 
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7); 
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8); 
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b'); 
-    h3=plot(time,100*x_uf(4,:)','g'); 
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
- 
-end
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-    
-    
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-    
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g'); 
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); 
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b'); 
-    h3=plot(time,100*x_uf(1,:)','g'); 
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6); 
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b'); 
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]'); 
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7); 
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8); 
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b'); 
-    h3=plot(time,100*x_uf(4,:)','g'); 
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
- 
-end
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-    
-    
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-    
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g'); 
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); 
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b'); 
-    h3=plot(time,100*x_uf(1,:)','g'); 
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6); 
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b'); 
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]'); 
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7); 
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8); 
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b'); 
-    h3=plot(time,100*x_uf(4,:)','g'); 
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
- 
-end
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-    
-    
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-    
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g'); 
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); 
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b'); 
-    h3=plot(time,100*x_uf(1,:)','g'); 
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6); 
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b'); 
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]'); 
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7); 
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8); 
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b'); 
-    h3=plot(time,100*x_uf(4,:)','g'); 
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
- 
-end
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-    
-    
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-    
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g'); 
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); 
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b'); 
-    h3=plot(time,100*x_uf(1,:)','g'); 
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6); 
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b'); 
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]'); 
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7); 
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8); 
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b'); 
-    h3=plot(time,100*x_uf(4,:)','g'); 
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
- 
-end
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-    
-    
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-    
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g'); 
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); 
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b'); 
-    h3=plot(time,100*x_uf(1,:)','g'); 
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6); 
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b'); 
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]'); 
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7); 
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8); 
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b'); 
-    h3=plot(time,100*x_uf(4,:)','g'); 
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
- 
-end
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-    
-    
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-    
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g'); 
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); 
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b'); 
-    h3=plot(time,100*x_uf(1,:)','g'); 
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6); 
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b'); 
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]'); 
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7); 
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8); 
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b'); 
-    h3=plot(time,100*x_uf(4,:)','g'); 
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
- 
-end
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-    
-    
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-    
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g'); 
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); 
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b'); 
-    h3=plot(time,100*x_uf(1,:)','g'); 
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6); 
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b'); 
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]'); 
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7); 
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8); 
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b'); 
-    h3=plot(time,100*x_uf(4,:)','g'); 
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
- 
-end
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-    
-    
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-    
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g'); 
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); 
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b'); 
-    h3=plot(time,100*x_uf(1,:)','g'); 
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6); 
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b'); 
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]'); 
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7); 
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8); 
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b'); 
-    h3=plot(time,100*x_uf(4,:)','g'); 
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
- 
-end
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-    
-    
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-    
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g'); 
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); 
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b'); 
-    h3=plot(time,100*x_uf(1,:)','g'); 
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6); 
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b'); 
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]'); 
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7); 
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8); 
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b'); 
-    h3=plot(time,100*x_uf(4,:)','g'); 
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
- 
-end
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-    
-    
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-    
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g'); 
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); 
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b'); 
-    h3=plot(time,100*x_uf(1,:)','g'); 
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6); 
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b'); 
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]'); 
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7); 
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8); 
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b'); 
-    h3=plot(time,100*x_uf(4,:)','g'); 
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
- 
-end
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-    
-    
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-    
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g'); 
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); 
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b'); 
-    h3=plot(time,100*x_uf(1,:)','g'); 
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6); 
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b'); 
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]'); 
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7); 
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8); 
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b'); 
-    h3=plot(time,100*x_uf(4,:)','g'); 
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
- 
-end
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-    
-    
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-    
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g'); 
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); 
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b'); 
-    h3=plot(time,100*x_uf(1,:)','g'); 
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6); 
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b'); 
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]'); 
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7); 
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8); 
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b'); 
-    h3=plot(time,100*x_uf(4,:)','g'); 
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
- 
-end
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-    
-    
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-    
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g'); 
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); 
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b'); 
-    h3=plot(time,100*x_uf(1,:)','g'); 
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6); 
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b'); 
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]'); 
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7); 
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8); 
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b'); 
-    h3=plot(time,100*x_uf(4,:)','g'); 
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
- 
-end
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-    
-    
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-    
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g'); 
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); 
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b'); 
-    h3=plot(time,100*x_uf(1,:)','g'); 
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6); 
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b'); 
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]'); 
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7); 
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8); 
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b'); 
-    h3=plot(time,100*x_uf(4,:)','g'); 
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
- 
-end
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-    
-    
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-    
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g'); 
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); 
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b'); 
-    h3=plot(time,100*x_uf(1,:)','g'); 
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6); 
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b'); 
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]'); 
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7); 
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8); 
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b'); 
-    h3=plot(time,100*x_uf(4,:)','g'); 
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
- 
-end
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-    
-    
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-    
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g'); 
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); 
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b'); 
-    h3=plot(time,100*x_uf(1,:)','g'); 
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6); 
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b'); 
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]'); 
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7); 
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8); 
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b'); 
-    h3=plot(time,100*x_uf(4,:)','g'); 
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
- 
-end
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-    
-    
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-    
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g'); 
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); 
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b'); 
-    h3=plot(time,100*x_uf(1,:)','g'); 
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6); 
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b'); 
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]'); 
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7); 
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8); 
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b'); 
-    h3=plot(time,100*x_uf(4,:)','g'); 
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
- 
-end
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-    
-    
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-    
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g'); 
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); 
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b'); 
-    h3=plot(time,100*x_uf(1,:)','g'); 
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6); 
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b'); 
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]'); 
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7); 
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8); 
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b'); 
-    h3=plot(time,100*x_uf(4,:)','g'); 
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
- 
-end
-     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
-    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
-    phiMaxNorm = (phiMax+pi)./(2*pi); 
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-
-    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
-    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
-    fitType='binomial';
-    clear nst lambda;
-    
-    
-    for i=1:numCells
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-        tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'poisson'))
-         else
-             % Conditional Intensity Function for ith cell
-            lambdaData = tempData./(1+tempData); 
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
-         lambda{i}=lambda{i}.resample(1/delta);
-         
-         % Generate CIF representation in case we want to use the symbolic
-         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
-         % generate one realization for each cell
-         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1); 
-         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
-         nst{i}.setName(num2str(i));              % give each cell a unique name
-        
-    end
-
-    % Plot the neural raster across all the cells
-    spikeColl = nstColl(nst); % Create a neural spike train collection
-
-    % Based on the temporal resolution defined by delta, bin the data and get
-    % a matrix representation of the neural firing
-    dN=spikeColl.dataToMatrix';
-    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
-                % general we should pick delta small enough so that there is
-                % only one spike per bin
-
-    [C,N]   = size(dN); % N time samples, C cells
-
-    beta=[zeros(2,numCells);  bCoeffs'];
-
-    
-    %Use the Goal Directed Movement Version of the Point Process adaptive
-    %Filter
-    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
-
-    %Use the Free Movement Version of the Point Process adaptive
-    %Filter
-    [x_pf, W_pf, x_uf, W_uf] = ...
-        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
-        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
-
-
-    if(k==numExamples)
-        subplot(4,2,1:4);h1=plot(100*x(1,:),100*x(2,:),'k','LineWidth',3); 
-        hold on;
-        axis([sort([100*x0(1)+5, 100*xT(1)-5]), ...
-            sort([100*x0(2)-5, 100*xT(2)+5])]);
-        title('Estimated vs. Actual Reach Paths',...
-            'FontWeight','bold','Fontsize',12,'FontName','Arial');
-    end
-    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
-    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g'); 
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
-    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-    subplot(4,2,5); 
-    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(1,:)','b'); 
-    h3=plot(time,100*x_uf(1,:)','g'); 
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,6); 
-    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(2,:)','b'); 
-    h3=plot(time,100*x_uf(2,:)','g');
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]'); 
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
-
-    subplot(4,2,7); 
-    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(3,:)','b');
-    h3=plot(time,100*x_uf(3,:)','g');
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    subplot(4,2,8); 
-    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*x_u(4,:)','b'); 
-    h3=plot(time,100*x_uf(4,:)','g'); 
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
- 
-end
-

Experiment 6 - Hybrid Point Process Filter Example

NOTE THIS EXAMPLE WAS NOT INCLUDED IN THE FINAL VERSION OF THE PAPER This example is based on an implementation of the Hybrid Point Process filter described in General-purpose filter design for neural prosthetic devices by Srinivasan L, Eden UT, Mitter SK, Brown EN in J Neurophysiol. 2007 Oct, 98(4):2456-75.

Problem Statement

Suppose that a process of interest can be modeled as consisting of several discrete states where the evolution of the system under each state can be modeled as a linear state space model. The observations of both the state and the continuous dynamics are not direct, but rather observed through how the continuous and discrete states affect the firing of a population of neurons. The goal of the hybrid filter is to estimate both the continuous dynamics and the underlying system state from only the neural population firing (point process observations).

To illustrate the use of this filter, we consider a reaching task. We assume two underlying system states s=1="Not Moving"=NM and s=2="Moving"=M. Under the "Not Moving" the position of the arm remain constant, whereas in the "Moving" state, the position and velocities evolved based on the arm acceleration that is modeled as a gaussian white noise process.

Under both the "Moving" and "Not Moving" states, the arm evolution state vector is

$${\bf{x}} = {[x,y,{v_x},{v_y},{a_x},{a_y}]^T}$$

Generated Simulated Arm Reach

clear all;
-close all;
-delta=0.001;
-Tmax=2;
-time=0:delta:Tmax;
-A{2} = [1 0 delta 0     delta^2/2   0;
-        0 1 0     delta 0           delta^2/2;
-        0 0 1     0     delta       0;
-        0 0 0     1     0           delta;
-        0 0 0     0     1           0;
-        0 0 0     0     0           1];
-
-A{1} = [1 0 0 0 0 0;
-        0 1 0 0 0 0;
-        0 0 0 0 0 0;
-        0 0 0 0 0 0;
-        0 0 0 0 0 0;
-        0 0 0 0 0 0];
-A{1} = [1 0;
-        0 1];
-
-Px0{2} =1e-6*eye(6,6);
-Px0{1} =1e-6*eye(2,2);
-
-minCovVal = 1e-12;
-covVal = 1e-3;
-
-
-
-Q{2}=[minCovVal     0   0     0   0       0;
-      0       minCovVal 0     0   0       0;
-      0       0   minCovVal   0   0       0;
-      0       0   0     minCovVal 0       0;
-      0       0   0     0   covVal      0;
-      0       0   0     0   0       covVal];
-
-Q{1}=minCovVal*eye(2,2);
-
-mstate = zeros(1,length(time));
-ind{1}=1:2;
-ind{2}=1:6;
-
-% Acceleration model
-X=zeros(max([size(A{1},1),size(A{2},1)]),length(time));
-p_ij = [.998 .002;
-        .001 .999];
-
-for i = 1:length(time)
-
-    if(i==1)
-        mstate(i) = 1;
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       else
-           if(mstate(i-1)==1)
-               mstate(i) = 2;
-           else
-               mstate(i) = 1;
-           end
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-
-%     close all;
-%% Experiment 6 - Hybrid Point Process Filter Example 
-% NOTE THIS EXAMPLE WAS NOT INCLUDED IN THE FINAL VERSION OF THE PAPER
-% This example is based on an implementation of the Hybrid Point Process
-% filter described in _General-purpose filter design for neural prosthetic 
-% devices_ by Srinivasan L, Eden UT, Mitter SK, Brown EN in J Neurophysiol.
-% 2007 Oct, 98(4):2456-75. 
-%
-
-%% Problem Statement
-% Suppose that a process of interest can be modeled as consisting of
-% several discrete states where the evolution of the system under each
-% state can be modeled as a linear state space model. The observations of
-% both the state and the continuous dynamics are not direct, but rather
-% observed through how the continuous and discrete states affect the firing
-% of a population of neurons. The goal of the hybrid filter is to estimate
-% both the continuous dynamics and the underlying system state from only
-% the neural population firing (point process observations).
-%
-% To illustrate the use of this filter, we consider a reaching task. We
-% assume two underlying system states s=1="Not Moving"=NM and s=2="Moving"=M.
-% Under the "Not Moving" the position of the arm remain constant,
-% whereas in the "Moving" state, the position and velocities evolved based
-% on the arm acceleration that is modeled as a gaussian white noise
-% process.
-%
-% Under both the "Moving" and "Not Moving" states, the arm evolution state
-% vector is 
-%%
-% 
-% $${\bf{x}} = {[x,y,{v_x},{v_y},{a_x},{a_y}]^T}$$
-% 
-
-%% Generated Simulated Arm Reach
-
-clear all;
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\private\evalmxdom.m' could not be
-cleared because it contains MATLAB code that is currently executing. 
-Warning: The file
-'C:\Users\Developer\Dropbox\GitHub\nSTAT\helpfiles\nSTATPaperExamples.m' could
-not be cleared because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\mdbpublish.m' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\publish.p' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\private\evalmxdom.m' could not be
-cleared because it contains MATLAB code that is currently executing. 
-Warning: The file
-'C:\Users\Developer\Dropbox\GitHub\nSTAT\helpfiles\nSTATPaperExamples.m' could
-not be cleared because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\mdbpublish.m' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\publish.p' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\private\evalmxdom.m' could not be
-cleared because it contains MATLAB code that is currently executing. 
-Warning: The file
-'C:\Users\Developer\Dropbox\GitHub\nSTAT\helpfiles\nSTATPaperExamples.m' could
-not be cleared because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\mdbpublish.m' could not be cleared
-because it contains MATLAB code that is currently executing. 
-Warning: The file 'C:\Program
-Files\MATLAB\R2017a\toolbox\matlab\codetools\publish.p' could not be cleared
-because it contains MATLAB code that is currently executing. 
-close all;
-delta=0.001;
-Tmax=2;
-time=0:delta:Tmax;
-A{2} = [1 0 delta 0     delta^2/2   0;
-        0 1 0     delta 0           delta^2/2;
-        0 0 1     0     delta       0;
-        0 0 0     1     0           delta;
-        0 0 0     0     1           0;
-        0 0 0     0     0           1];
-        
-A{1} = [1 0 0 0 0 0;
-        0 1 0 0 0 0;
-        0 0 0 0 0 0;
-        0 0 0 0 0 0;
-        0 0 0 0 0 0;
-        0 0 0 0 0 0];
-A{1} = [1 0;
-        0 1];
-            
-Px0{2} =1e-6*eye(6,6);
-Px0{1} =1e-6*eye(2,2);
-
-minCovVal = 1e-12;
-covVal = 1e-3; 
-
-
-
-Q{2}=[minCovVal     0   0     0   0       0;
-      0       minCovVal 0     0   0       0;
-      0       0   minCovVal   0   0       0;
-      0       0   0     minCovVal 0       0;
-      0       0   0     0   covVal      0;
-      0       0   0     0   0       covVal];
-
-Q{1}=minCovVal*eye(2,2);
-
-mstate = zeros(1,length(time));
-ind{1}=1:2;
-ind{2}=1:6;
-
-% Acceleration model
-X=zeros(max([size(A{1},1),size(A{2},1)]),length(time));
-p_ij = [.998 .002; 
-        .001 .999];
-
-for i = 1:length(time)
-    
-    if(i==1)
-        mstate(i) = 1;
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-       else
-           if(mstate(i-1)==1)
-               mstate(i) = 2;
-           end
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
-    end
-
-end
-    
-    if(i==1)
-    else
-       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
-            mstate(i) = mstate(i-1);
-       end
-    end
-    st = mstate(i);
-    R=chol(Q{st});
-    if(i<length(time))
-
-end
-
%save paperHybridFilterExample time Tmax delta mstate X p_ij ind A Q Px0
-load paperHybridFilterExample;
-Q{1}=minCovVal*eye(2,2);
-numCells=40;
-close all;
-scrsz = get(0,'ScreenSize');
-fig1=figure('OuterPosition',[scrsz(3)*.1 scrsz(4)*.1 ...
-    scrsz(3)*.8 scrsz(4)*.9]);
-subplot(4,2,[1 3]);
-plot(100*X(1,:),100*X(2,:),'k','Linewidth',2); hx=xlabel('X [cm]');
-hy=ylabel('Y [cm]');  hold on;
-set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-title('Reach Path','FontWeight','bold','Fontsize',14,'FontName','Arial');
-hold on;
-h1=plot(100*X(1,1),100*X(2,1),'bo','MarkerSize',16);
-h2=plot(100*X(1,end),100*X(2,end),'ro','MarkerSize',16);
-legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-
-subplot(4,2,[6 8]);
-plot(time,mstate,'k','Linewidth',2); axis tight; v=axis;
-axis([v(1) v(2) 0 3]);
-hx=xlabel('time [s]'); hy=ylabel('state');
-set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-set(gca,'YTick',[1 2],'YTickLabel',{'N','M'})
-title('Discrete Movement State','FontWeight','bold','Fontsize',14,...
-    'FontName','Arial');
-
-subplot(4,2,5);
-h1=plot(time,100*X(1,1:end),'k','Linewidth',2); hold on;
-h2=plot(time,100*X(2,1:end),'k-.','Linewidth',2);
-hx=xlabel('time [s]'); hy=ylabel('Position [cm]');
-set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-h_legend=legend([h1,h2],'x','y','Location','NorthEast');
-set(h_legend,'FontSize',14)
-pos = get(h_legend,'position');
-set(h_legend, 'position',[pos(1)+.06 pos(2)+.01 pos(3:4)]);
-
-
-subplot(4,2,7);
-h1=plot(time,100*X(3,1:end),'k','Linewidth',2); hold on;
-h2=plot(time,100*X(4,1:end),'k-.','Linewidth',2);
-hx=xlabel('time [s]'); hy=ylabel('Velocity [cm/s]');
-set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-h_legend=legend([h1,h2],'v_{x}','v_{y}','Location','NorthEast');
-set(h_legend,'FontSize',14)
-pos = get(h_legend,'position');
-set(h_legend, 'position',[pos(1)+.06 pos(2)+.01 pos(3:4)]);
-
-meanMu = log(10*delta);  % baseline firing rate
-MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1)
-coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-    0*randn(numCells,2)];
-%Add realization by thinning with history
-dataMat = [ones(size(X,2),1),X(:,1:end)'];
-% Generate M1 cells
-clear lambda tempSpikeColl lambdaCIF n;
-fitType ='binomial';
-% matlabpool open;
- for i=1:numCells
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     else
-        lambdaData = tempData;
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));
- end
- spikeColl = nstColl(n);
- subplot(4,2,[2 4]);
-spikeColl.plot;
-set(gca,'xtick',[],'xtickLabel',[],'ytickLabel',[]);
-title('Neural Raster','FontWeight','bold','Fontsize',14,'FontName','Arial');
-hx=xlabel('time [s]','Interpreter','none');
-hy=ylabel('Cell Number','Interpreter','none');
-set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-
-% close all;
-
%%
-%save paperHybridFilterExample time Tmax delta mstate X p_ij ind A Q Px0
-load paperHybridFilterExample;
-Q{1}=minCovVal*eye(2,2);
-numCells=40;
-close all;
-scrsz = get(0,'ScreenSize');
-fig1=figure('OuterPosition',[scrsz(3)*.1 scrsz(4)*.1 ...
-    scrsz(3)*.8 scrsz(4)*.9]);
-subplot(4,2,[1 3]);
-plot(100*X(1,:),100*X(2,:),'k','Linewidth',2); hx=xlabel('X [cm]'); 
-hy=ylabel('Y [cm]');  hold on;
-set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-title('Reach Path','FontWeight','bold','Fontsize',14,'FontName','Arial');
-hold on; 
-h1=plot(100*X(1,1),100*X(2,1),'bo','MarkerSize',16); 
-h2=plot(100*X(1,end),100*X(2,end),'ro','MarkerSize',16); 
-legend([h1 h2],'Start','Finish','Location','NorthEast');
-
-
-
-subplot(4,2,[6 8]);
-plot(time,mstate,'k','Linewidth',2); axis tight; v=axis; 
-axis([v(1) v(2) 0 3]);
-hx=xlabel('time [s]'); hy=ylabel('state');
-set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-set(gca,'YTick',[1 2],'YTickLabel',{'N','M'})
-title('Discrete Movement State','FontWeight','bold','Fontsize',14,...
-    'FontName','Arial');
-
-subplot(4,2,5);
-h1=plot(time,100*X(1,1:end),'k','Linewidth',2); hold on;
-h2=plot(time,100*X(2,1:end),'k-.','Linewidth',2);
-hx=xlabel('time [s]'); hy=ylabel('Position [cm]');
-set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-h_legend=legend([h1,h2],'x','y','Location','NorthEast'); 
-set(h_legend,'FontSize',14)
-pos = get(h_legend,'position');
-set(h_legend, 'position',[pos(1)+.06 pos(2)+.01 pos(3:4)]);
-
-
-subplot(4,2,7);
-h1=plot(time,100*X(3,1:end),'k','Linewidth',2); hold on;
-h2=plot(time,100*X(4,1:end),'k-.','Linewidth',2);
-hx=xlabel('time [s]'); hy=ylabel('Velocity [cm/s]');
-set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-h_legend=legend([h1,h2],'v_{x}','v_{y}','Location','NorthEast'); 
-set(h_legend,'FontSize',14)
-pos = get(h_legend,'position');
-set(h_legend, 'position',[pos(1)+.06 pos(2)+.01 pos(3:4)]);
-
-meanMu = log(10*delta);  % baseline firing rate 
-MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-    0*randn(numCells,2)];
-%Add realization by thinning with history
-dataMat = [ones(size(X,2),1),X(:,1:end)'];
-% Generate M1 cells
-clear lambda tempSpikeColl lambdaCIF n;
-fitType ='binomial';
-% matlabpool open;
- for i=1:numCells
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
-     tempData  = exp(dataMat*coeffs(i,:)');
-     if(strcmp(fitType,'binomial'));
-        lambdaData = tempData./(1+tempData);
-     end
-     lambda{i}=Covariate(time,lambdaData./delta, ...
-         '\Lambda(t)','time','s','spikes/sec',...
-         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-     maxTimeRes = 0.001;
-     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-     n{i} = tempSpikeColl{i}.getNST(1);
-     n{i}.setName(num2str(i));    
- end
- spikeColl = nstColl(n);
- subplot(4,2,[2 4]);
-spikeColl.plot;
-set(gca,'xtick',[],'xtickLabel',[],'ytickLabel',[]);
-title('Neural Raster','FontWeight','bold','Fontsize',14,'FontName','Arial');
-hx=xlabel('time [s]','Interpreter','none'); 
-hy=ylabel('Cell Number','Interpreter','none');
-set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
-

Simulate Neural Firing

We simulate a population of neurons that fire in response to the movement velocity (x and y coorinates)

%Use the data to estimate the process noise for the moving case and
-%non-moving case
-
-nonMovingInd = intersect(find(X(5,:)==0),find(X(6,:)==0));
-movingInd=setdiff(1:size(X,2),nonMovingInd);
-Q{2}=diag(var(diff(X(:,movingInd),[],2),[],2));
-Q{2}(1:4,1:4)=0;
-varNV=diag(var(diff(X(:,nonMovingInd),[],2),[],2));
-Q{1} = varNV(1:2,1:2);
-close all; clear S_est X_est MU_est S_estNT X_estNT MU_estNT;
-numExamples = 20;
-numCells=40;
-scrsz = get(0,'ScreenSize');
-fig1=figure('OuterPosition',[scrsz(3)*.1 scrsz(4)*.1 ...
-    scrsz(3)*.9 scrsz(4)*.9]);
-
-for n=1:numExamples
-    meanMu = log(10*delta);  % baseline firing rate
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1)
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-        0*randn(numCells,2)];
-
-
-
-    %Add realization by thinning with history
-    dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         else
-            lambdaData = tempData;
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));
-     end
-
-    % Decode the x-y trajectory
-
-    % Enforce that the maximum time resolution is delta
-    spikeColl = nstColl(nst);
-    spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix;
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
-
-    % Starting states are equally probable
-    Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
-    x0{1} = X(ind{1},1);
-    yT{1} = X(ind{1},end);
-    Pi0    = Px0;
-    PiT{1} = 1e-9*eye(size(x0{1},1),size(x0{1},1));
-
-    x0{2} = X(ind{2},1);
-    yT{2} = X(ind{2},end);
-    PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
-
-
-    % Run the Hybrid Point Process Filter
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-
-    %Store the results for computing relevant statistics later
-    X_estAll(:,:,n) = X_est;
-    X_estNTAll(:,:,n) = X_estNT;
-    S_estAll(n,:)=S_est;
-    S_estNTAll(n,:)=S_estNT;
-    MU_estAll(:,:,n)=MU_est;
-    MU_estNTAll(:,:,n) = MU_estNT;
-
-
-    %State Estimate
-    subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis;
-    axis([v(1) v(2) 0.5 2.5]);
-
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
-    subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
-    axis([min(time) max(time) 0 1.1]);
-
-    %The movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.');
-
-    %X-Position
-    subplot(4,3,8);
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.');
-    h3=plot(time,100*X_estNT(1,:)','g-.');
-
-    %Y-Position
-    subplot(4,3,9);
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.');
-    h3=plot(time,100*X_estNT(2,:)','g-.');
-
-    %X-Velocity
-    subplot(4,3,11);
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
-
-    subplot(4,3,12);
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.');
-    h3=plot(time,100*X_estNT(4,:)','g-.');
-
-
-
-
-end
-
-%
-%     Save all the example Data
-%     save Experiment6ReachExamples X_estAll X_estNTAll S_estAll ...
-%           S_estNTAll MU_estAll MU_estNTAll;
-%
-%     load Experiment6ReachExamples;
-
-    % Mean Discrete State Estimate
-    subplot(4,3,[1 4]);
-    hold all;
-    plot(time,mstate,'k','LineWidth',3);
-    plot(time,mean(S_estAll),'b','LineWidth',3);
-    plot(time,mean(S_estNTAll),'g','LineWidth',3);
-    set(gca,'xtick',[],'YTick',[1 2.1],'YTickLabel',{'N','M'});
-    hy=ylabel('state'); hx=xlabel('time [s]');
-    set([hy hx],'FontName', 'Arial','FontSize',10,'FontWeight','bold',...
-        'Interpreter','none');
-    title('Estimated vs. Actual State','FontWeight','bold','Fontsize',...
-        12,'FontName','Arial');
-
-
-
-
-   % Mean State Movement State Probability
-    subplot(4,3,[7 10]);
-    plot(time, mean(squeeze(MU_estAll(2,:,:)),2),'b','LineWidth',3);
-    hold on;
-    plot(time,mean(squeeze(MU_estNTAll(2,:,:)),2),'g','LineWidth',3);
-    hold on;
-    axis([min(time) max(time) 0 1.1]);
-    hx=xlabel('time [s]'); hy=ylabel('P(s(t)=M | data)');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Probability of State','FontWeight','bold','Fontsize',12,...
-        'FontName','Arial');
-
-    % Mean movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    mXestAll=mean(100*X_estAll,3);
-    mXestNTAll=mean(100*X_estNTAll,3);
-    plot(mXestAll(1,:),mXestAll(2,:),'b','Linewidth',3);
-    plot(mXestNTAll(1,:),mXestNTAll(2,:),'g','Linewidth',3);
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-
-    h1=plot(100*X(1,1),100*X(2,1),'bo','MarkerSize',14); hold on;
-    h2=plot(100*X(1,end),100*X(2,end),'ro','MarkerSize',14);
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-    title('Estimated vs. Actual Reach Path','FontWeight','bold',...
-        'Fontsize',12,'FontName','Arial');
-
-
-    % Mean X-Positon
-    subplot(4,3,8);
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,mXestAll(1,:),'b','LineWidth',3); hold on;
-    h3=plot(time,mXestNTAll(1,:),'g','LineWidth',3); hold on;
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    % Mean Y-Position
-    subplot(4,3,9);
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,mXestAll(2,:),'b','LineWidth',3); hold on;
-    h3=plot(time,mXestNTAll(2,:),'g','LineWidth',3); hold on;
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.40 pos(2)+.51 pos(3:4)]);
-
-    % Mean X-Velocity
-    subplot(4,3,11);
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,mXestAll(3,:),'b','LineWidth',3); hold on;
-    h3=plot(time,mXestNTAll(3,:),'g','LineWidth',3); hold on;
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-    % Mean Y-Velocity
-    subplot(4,3,12);
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,mXestAll(4,:),'b','LineWidth',3); hold on;
-    h3=plot(time,mXestNTAll(4,:),'g','LineWidth',3); hold on;
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
-
-% close all;
- 
-%% Simulate Neural Firing
-% We simulate a population of neurons that fire in response to the movement
-% velocity (x and y coorinates) 
-
-%Use the data to estimate the process noise for the moving case and
-%non-moving case
-
-nonMovingInd = intersect(find(X(5,:)==0),find(X(6,:)==0));
-movingInd=setdiff(1:size(X,2),nonMovingInd);
-Q{2}=diag(var(diff(X(:,movingInd),[],2),[],2));
-Q{2}(1:4,1:4)=0;
-varNV=diag(var(diff(X(:,nonMovingInd),[],2),[],2));
-Q{1} = varNV(1:2,1:2);
-close all; clear S_est X_est MU_est S_estNT X_estNT MU_estNT;
-numExamples = 20;
-numCells=40;
-scrsz = get(0,'ScreenSize');
-fig1=figure('OuterPosition',[scrsz(3)*.1 scrsz(4)*.1 ...
-    scrsz(3)*.9 scrsz(4)*.9]);
-
-for n=1:numExamples
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-        0*randn(numCells,2)];
-
-
-
-    %Add realization by thinning with history
-    dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-
-    % Decode the x-y trajectory
-
-    % Enforce that the maximum time resolution is delta
-    spikeColl = nstColl(nst);
-    spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix; 
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
-
-    % Starting states are equally probable
-    Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
-    x0{1} = X(ind{1},1);
-    yT{1} = X(ind{1},end);
-    Pi0    = Px0;
-    PiT{1} = 1e-9*eye(size(x0{1},1),size(x0{1},1));
-
-    x0{2} = X(ind{2},1);
-    yT{2} = X(ind{2},end);
-    PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
-
-
-    % Run the Hybrid Point Process Filter 
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-    
-    %Store the results for computing relevant statistics later
-    X_estAll(:,:,n) = X_est;
-    X_estNTAll(:,:,n) = X_estNT;
-    S_estAll(n,:)=S_est;
-    S_estNTAll(n,:)=S_estNT;
-    MU_estAll(:,:,n)=MU_est;
-    MU_estNTAll(:,:,n) = MU_estNT;
-    
-
-    %State Estimate
-    subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis; 
-    axis([v(1) v(2) 0.5 2.5]); 
-
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
-    subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
-    axis([min(time) max(time) 0 1.1]);
-
-    %The movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.'); 
-    
-    %X-Position
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.'); 
-    h3=plot(time,100*X_estNT(1,:)','g-.'); 
-
-    %Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.'); 
-    h3=plot(time,100*X_estNT(2,:)','g-.'); 
-
-    %X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
-
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.'); 
-    h3=plot(time,100*X_estNT(4,:)','g-.');
-
-    
-    
-
-end
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-        0*randn(numCells,2)];
-
-
-
-    %Add realization by thinning with history
-    dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-
-    % Decode the x-y trajectory
-
-    % Enforce that the maximum time resolution is delta
-    spikeColl = nstColl(nst);
-    spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix; 
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
-
-    % Starting states are equally probable
-    Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
-    x0{1} = X(ind{1},1);
-    yT{1} = X(ind{1},end);
-    Pi0    = Px0;
-    PiT{1} = 1e-9*eye(size(x0{1},1),size(x0{1},1));
-
-    x0{2} = X(ind{2},1);
-    yT{2} = X(ind{2},end);
-    PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
-
-
-    % Run the Hybrid Point Process Filter 
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-    
-    %Store the results for computing relevant statistics later
-    X_estAll(:,:,n) = X_est;
-    X_estNTAll(:,:,n) = X_estNT;
-    S_estAll(n,:)=S_est;
-    S_estNTAll(n,:)=S_estNT;
-    MU_estAll(:,:,n)=MU_est;
-    MU_estNTAll(:,:,n) = MU_estNT;
-    
-
-    %State Estimate
-    subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis; 
-    axis([v(1) v(2) 0.5 2.5]); 
-
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
-    subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
-    axis([min(time) max(time) 0 1.1]);
-
-    %The movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.'); 
-    
-    %X-Position
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.'); 
-    h3=plot(time,100*X_estNT(1,:)','g-.'); 
-
-    %Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.'); 
-    h3=plot(time,100*X_estNT(2,:)','g-.'); 
-
-    %X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
-
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.'); 
-    h3=plot(time,100*X_estNT(4,:)','g-.');
-
-    
-    
-
-end
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-        0*randn(numCells,2)];
-
-
-
-    %Add realization by thinning with history
-    dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-
-    % Decode the x-y trajectory
-
-    % Enforce that the maximum time resolution is delta
-    spikeColl = nstColl(nst);
-    spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix; 
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
-
-    % Starting states are equally probable
-    Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
-    x0{1} = X(ind{1},1);
-    yT{1} = X(ind{1},end);
-    Pi0    = Px0;
-    PiT{1} = 1e-9*eye(size(x0{1},1),size(x0{1},1));
-
-    x0{2} = X(ind{2},1);
-    yT{2} = X(ind{2},end);
-    PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
-
-
-    % Run the Hybrid Point Process Filter 
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-    
-    %Store the results for computing relevant statistics later
-    X_estAll(:,:,n) = X_est;
-    X_estNTAll(:,:,n) = X_estNT;
-    S_estAll(n,:)=S_est;
-    S_estNTAll(n,:)=S_estNT;
-    MU_estAll(:,:,n)=MU_est;
-    MU_estNTAll(:,:,n) = MU_estNT;
-    
-
-    %State Estimate
-    subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis; 
-    axis([v(1) v(2) 0.5 2.5]); 
-
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
-    subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
-    axis([min(time) max(time) 0 1.1]);
-
-    %The movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.'); 
-    
-    %X-Position
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.'); 
-    h3=plot(time,100*X_estNT(1,:)','g-.'); 
-
-    %Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.'); 
-    h3=plot(time,100*X_estNT(2,:)','g-.'); 
-
-    %X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
-
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.'); 
-    h3=plot(time,100*X_estNT(4,:)','g-.');
-
-    
-    
-
-end
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-        0*randn(numCells,2)];
-
-
-
-    %Add realization by thinning with history
-    dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-
-    % Decode the x-y trajectory
-
-    % Enforce that the maximum time resolution is delta
-    spikeColl = nstColl(nst);
-    spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix; 
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
-
-    % Starting states are equally probable
-    Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
-    x0{1} = X(ind{1},1);
-    yT{1} = X(ind{1},end);
-    Pi0    = Px0;
-    PiT{1} = 1e-9*eye(size(x0{1},1),size(x0{1},1));
-
-    x0{2} = X(ind{2},1);
-    yT{2} = X(ind{2},end);
-    PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
-
-
-    % Run the Hybrid Point Process Filter 
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-    
-    %Store the results for computing relevant statistics later
-    X_estAll(:,:,n) = X_est;
-    X_estNTAll(:,:,n) = X_estNT;
-    S_estAll(n,:)=S_est;
-    S_estNTAll(n,:)=S_estNT;
-    MU_estAll(:,:,n)=MU_est;
-    MU_estNTAll(:,:,n) = MU_estNT;
-    
-
-    %State Estimate
-    subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis; 
-    axis([v(1) v(2) 0.5 2.5]); 
-
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
-    subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
-    axis([min(time) max(time) 0 1.1]);
-
-    %The movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.'); 
-    
-    %X-Position
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.'); 
-    h3=plot(time,100*X_estNT(1,:)','g-.'); 
-
-    %Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.'); 
-    h3=plot(time,100*X_estNT(2,:)','g-.'); 
-
-    %X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
-
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.'); 
-    h3=plot(time,100*X_estNT(4,:)','g-.');
-
-    
-    
-
-end
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-        0*randn(numCells,2)];
-
-
-
-    %Add realization by thinning with history
-    dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-
-    % Decode the x-y trajectory
-
-    % Enforce that the maximum time resolution is delta
-    spikeColl = nstColl(nst);
-    spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix; 
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
-
-    % Starting states are equally probable
-    Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
-    x0{1} = X(ind{1},1);
-    yT{1} = X(ind{1},end);
-    Pi0    = Px0;
-    PiT{1} = 1e-9*eye(size(x0{1},1),size(x0{1},1));
-
-    x0{2} = X(ind{2},1);
-    yT{2} = X(ind{2},end);
-    PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
-
-
-    % Run the Hybrid Point Process Filter 
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-    
-    %Store the results for computing relevant statistics later
-    X_estAll(:,:,n) = X_est;
-    X_estNTAll(:,:,n) = X_estNT;
-    S_estAll(n,:)=S_est;
-    S_estNTAll(n,:)=S_estNT;
-    MU_estAll(:,:,n)=MU_est;
-    MU_estNTAll(:,:,n) = MU_estNT;
-    
-
-    %State Estimate
-    subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis; 
-    axis([v(1) v(2) 0.5 2.5]); 
-
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
-    subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
-    axis([min(time) max(time) 0 1.1]);
-
-    %The movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.'); 
-    
-    %X-Position
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.'); 
-    h3=plot(time,100*X_estNT(1,:)','g-.'); 
-
-    %Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.'); 
-    h3=plot(time,100*X_estNT(2,:)','g-.'); 
-
-    %X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
-
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.'); 
-    h3=plot(time,100*X_estNT(4,:)','g-.');
-
-    
-    
-
-end
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-        0*randn(numCells,2)];
-
-
-
-    %Add realization by thinning with history
-    dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-
-    % Decode the x-y trajectory
-
-    % Enforce that the maximum time resolution is delta
-    spikeColl = nstColl(nst);
-    spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix; 
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
-
-    % Starting states are equally probable
-    Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
-    x0{1} = X(ind{1},1);
-    yT{1} = X(ind{1},end);
-    Pi0    = Px0;
-    PiT{1} = 1e-9*eye(size(x0{1},1),size(x0{1},1));
-
-    x0{2} = X(ind{2},1);
-    yT{2} = X(ind{2},end);
-    PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
-
-
-    % Run the Hybrid Point Process Filter 
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-    
-    %Store the results for computing relevant statistics later
-    X_estAll(:,:,n) = X_est;
-    X_estNTAll(:,:,n) = X_estNT;
-    S_estAll(n,:)=S_est;
-    S_estNTAll(n,:)=S_estNT;
-    MU_estAll(:,:,n)=MU_est;
-    MU_estNTAll(:,:,n) = MU_estNT;
-    
-
-    %State Estimate
-    subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis; 
-    axis([v(1) v(2) 0.5 2.5]); 
-
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
-    subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
-    axis([min(time) max(time) 0 1.1]);
-
-    %The movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.'); 
-    
-    %X-Position
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.'); 
-    h3=plot(time,100*X_estNT(1,:)','g-.'); 
-
-    %Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.'); 
-    h3=plot(time,100*X_estNT(2,:)','g-.'); 
-
-    %X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
-
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.'); 
-    h3=plot(time,100*X_estNT(4,:)','g-.');
-
-    
-    
-
-end
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-        0*randn(numCells,2)];
-
-
-
-    %Add realization by thinning with history
-    dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-
-    % Decode the x-y trajectory
-
-    % Enforce that the maximum time resolution is delta
-    spikeColl = nstColl(nst);
-    spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix; 
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
-
-    % Starting states are equally probable
-    Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
-    x0{1} = X(ind{1},1);
-    yT{1} = X(ind{1},end);
-    Pi0    = Px0;
-    PiT{1} = 1e-9*eye(size(x0{1},1),size(x0{1},1));
-
-    x0{2} = X(ind{2},1);
-    yT{2} = X(ind{2},end);
-    PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
-
-
-    % Run the Hybrid Point Process Filter 
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-    
-    %Store the results for computing relevant statistics later
-    X_estAll(:,:,n) = X_est;
-    X_estNTAll(:,:,n) = X_estNT;
-    S_estAll(n,:)=S_est;
-    S_estNTAll(n,:)=S_estNT;
-    MU_estAll(:,:,n)=MU_est;
-    MU_estNTAll(:,:,n) = MU_estNT;
-    
-
-    %State Estimate
-    subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis; 
-    axis([v(1) v(2) 0.5 2.5]); 
-
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
-    subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
-    axis([min(time) max(time) 0 1.1]);
-
-    %The movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.'); 
-    
-    %X-Position
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.'); 
-    h3=plot(time,100*X_estNT(1,:)','g-.'); 
-
-    %Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.'); 
-    h3=plot(time,100*X_estNT(2,:)','g-.'); 
-
-    %X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
-
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.'); 
-    h3=plot(time,100*X_estNT(4,:)','g-.');
-
-    
-    
-
-end
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-        0*randn(numCells,2)];
-
-
-
-    %Add realization by thinning with history
-    dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-
-    % Decode the x-y trajectory
-
-    % Enforce that the maximum time resolution is delta
-    spikeColl = nstColl(nst);
-    spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix; 
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
-
-    % Starting states are equally probable
-    Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
-    x0{1} = X(ind{1},1);
-    yT{1} = X(ind{1},end);
-    Pi0    = Px0;
-    PiT{1} = 1e-9*eye(size(x0{1},1),size(x0{1},1));
-
-    x0{2} = X(ind{2},1);
-    yT{2} = X(ind{2},end);
-    PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
-
-
-    % Run the Hybrid Point Process Filter 
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-    
-    %Store the results for computing relevant statistics later
-    X_estAll(:,:,n) = X_est;
-    X_estNTAll(:,:,n) = X_estNT;
-    S_estAll(n,:)=S_est;
-    S_estNTAll(n,:)=S_estNT;
-    MU_estAll(:,:,n)=MU_est;
-    MU_estNTAll(:,:,n) = MU_estNT;
-    
-
-    %State Estimate
-    subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis; 
-    axis([v(1) v(2) 0.5 2.5]); 
-
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
-    subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
-    axis([min(time) max(time) 0 1.1]);
-
-    %The movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.'); 
-    
-    %X-Position
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.'); 
-    h3=plot(time,100*X_estNT(1,:)','g-.'); 
-
-    %Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.'); 
-    h3=plot(time,100*X_estNT(2,:)','g-.'); 
-
-    %X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
-
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.'); 
-    h3=plot(time,100*X_estNT(4,:)','g-.');
-
-    
-    
-
-end
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-        0*randn(numCells,2)];
-
-
-
-    %Add realization by thinning with history
-    dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-
-    % Decode the x-y trajectory
-
-    % Enforce that the maximum time resolution is delta
-    spikeColl = nstColl(nst);
-    spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix; 
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
-
-    % Starting states are equally probable
-    Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
-    x0{1} = X(ind{1},1);
-    yT{1} = X(ind{1},end);
-    Pi0    = Px0;
-    PiT{1} = 1e-9*eye(size(x0{1},1),size(x0{1},1));
-
-    x0{2} = X(ind{2},1);
-    yT{2} = X(ind{2},end);
-    PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
-
-
-    % Run the Hybrid Point Process Filter 
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-    
-    %Store the results for computing relevant statistics later
-    X_estAll(:,:,n) = X_est;
-    X_estNTAll(:,:,n) = X_estNT;
-    S_estAll(n,:)=S_est;
-    S_estNTAll(n,:)=S_estNT;
-    MU_estAll(:,:,n)=MU_est;
-    MU_estNTAll(:,:,n) = MU_estNT;
-    
-
-    %State Estimate
-    subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis; 
-    axis([v(1) v(2) 0.5 2.5]); 
-
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
-    subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
-    axis([min(time) max(time) 0 1.1]);
-
-    %The movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.'); 
-    
-    %X-Position
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.'); 
-    h3=plot(time,100*X_estNT(1,:)','g-.'); 
-
-    %Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.'); 
-    h3=plot(time,100*X_estNT(2,:)','g-.'); 
-
-    %X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
-
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.'); 
-    h3=plot(time,100*X_estNT(4,:)','g-.');
-
-    
-    
-
-end
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-        0*randn(numCells,2)];
-
-
-
-    %Add realization by thinning with history
-    dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-
-    % Decode the x-y trajectory
-
-    % Enforce that the maximum time resolution is delta
-    spikeColl = nstColl(nst);
-    spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix; 
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
-
-    % Starting states are equally probable
-    Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
-    x0{1} = X(ind{1},1);
-    yT{1} = X(ind{1},end);
-    Pi0    = Px0;
-    PiT{1} = 1e-9*eye(size(x0{1},1),size(x0{1},1));
-
-    x0{2} = X(ind{2},1);
-    yT{2} = X(ind{2},end);
-    PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
-
-
-    % Run the Hybrid Point Process Filter 
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-    
-    %Store the results for computing relevant statistics later
-    X_estAll(:,:,n) = X_est;
-    X_estNTAll(:,:,n) = X_estNT;
-    S_estAll(n,:)=S_est;
-    S_estNTAll(n,:)=S_estNT;
-    MU_estAll(:,:,n)=MU_est;
-    MU_estNTAll(:,:,n) = MU_estNT;
-    
-
-    %State Estimate
-    subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis; 
-    axis([v(1) v(2) 0.5 2.5]); 
-
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
-    subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
-    axis([min(time) max(time) 0 1.1]);
-
-    %The movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.'); 
-    
-    %X-Position
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.'); 
-    h3=plot(time,100*X_estNT(1,:)','g-.'); 
-
-    %Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.'); 
-    h3=plot(time,100*X_estNT(2,:)','g-.'); 
-
-    %X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
-
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.'); 
-    h3=plot(time,100*X_estNT(4,:)','g-.');
-
-    
-    
-
-end
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-        0*randn(numCells,2)];
-
-
-
-    %Add realization by thinning with history
-    dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-
-    % Decode the x-y trajectory
-
-    % Enforce that the maximum time resolution is delta
-    spikeColl = nstColl(nst);
-    spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix; 
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
-
-    % Starting states are equally probable
-    Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
-    x0{1} = X(ind{1},1);
-    yT{1} = X(ind{1},end);
-    Pi0    = Px0;
-    PiT{1} = 1e-9*eye(size(x0{1},1),size(x0{1},1));
-
-    x0{2} = X(ind{2},1);
-    yT{2} = X(ind{2},end);
-    PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
-
-
-    % Run the Hybrid Point Process Filter 
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-    
-    %Store the results for computing relevant statistics later
-    X_estAll(:,:,n) = X_est;
-    X_estNTAll(:,:,n) = X_estNT;
-    S_estAll(n,:)=S_est;
-    S_estNTAll(n,:)=S_estNT;
-    MU_estAll(:,:,n)=MU_est;
-    MU_estNTAll(:,:,n) = MU_estNT;
-    
-
-    %State Estimate
-    subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis; 
-    axis([v(1) v(2) 0.5 2.5]); 
-
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
-    subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
-    axis([min(time) max(time) 0 1.1]);
-
-    %The movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.'); 
-    
-    %X-Position
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.'); 
-    h3=plot(time,100*X_estNT(1,:)','g-.'); 
-
-    %Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.'); 
-    h3=plot(time,100*X_estNT(2,:)','g-.'); 
-
-    %X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
-
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.'); 
-    h3=plot(time,100*X_estNT(4,:)','g-.');
-
-    
-    
-
-end
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-        0*randn(numCells,2)];
-
-
-
-    %Add realization by thinning with history
-    dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-
-    % Decode the x-y trajectory
-
-    % Enforce that the maximum time resolution is delta
-    spikeColl = nstColl(nst);
-    spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix; 
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
-
-    % Starting states are equally probable
-    Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
-    x0{1} = X(ind{1},1);
-    yT{1} = X(ind{1},end);
-    Pi0    = Px0;
-    PiT{1} = 1e-9*eye(size(x0{1},1),size(x0{1},1));
-
-    x0{2} = X(ind{2},1);
-    yT{2} = X(ind{2},end);
-    PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
-
-
-    % Run the Hybrid Point Process Filter 
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-    
-    %Store the results for computing relevant statistics later
-    X_estAll(:,:,n) = X_est;
-    X_estNTAll(:,:,n) = X_estNT;
-    S_estAll(n,:)=S_est;
-    S_estNTAll(n,:)=S_estNT;
-    MU_estAll(:,:,n)=MU_est;
-    MU_estNTAll(:,:,n) = MU_estNT;
-    
-
-    %State Estimate
-    subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis; 
-    axis([v(1) v(2) 0.5 2.5]); 
-
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
-    subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
-    axis([min(time) max(time) 0 1.1]);
-
-    %The movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.'); 
-    
-    %X-Position
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.'); 
-    h3=plot(time,100*X_estNT(1,:)','g-.'); 
-
-    %Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.'); 
-    h3=plot(time,100*X_estNT(2,:)','g-.'); 
-
-    %X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
-
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.'); 
-    h3=plot(time,100*X_estNT(4,:)','g-.');
-
-    
-    
-
-end
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-        0*randn(numCells,2)];
-
-
-
-    %Add realization by thinning with history
-    dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-
-    % Decode the x-y trajectory
-
-    % Enforce that the maximum time resolution is delta
-    spikeColl = nstColl(nst);
-    spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix; 
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
-
-    % Starting states are equally probable
-    Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
-    x0{1} = X(ind{1},1);
-    yT{1} = X(ind{1},end);
-    Pi0    = Px0;
-    PiT{1} = 1e-9*eye(size(x0{1},1),size(x0{1},1));
-
-    x0{2} = X(ind{2},1);
-    yT{2} = X(ind{2},end);
-    PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
-
-
-    % Run the Hybrid Point Process Filter 
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-    
-    %Store the results for computing relevant statistics later
-    X_estAll(:,:,n) = X_est;
-    X_estNTAll(:,:,n) = X_estNT;
-    S_estAll(n,:)=S_est;
-    S_estNTAll(n,:)=S_estNT;
-    MU_estAll(:,:,n)=MU_est;
-    MU_estNTAll(:,:,n) = MU_estNT;
-    
-
-    %State Estimate
-    subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis; 
-    axis([v(1) v(2) 0.5 2.5]); 
-
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
-    subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
-    axis([min(time) max(time) 0 1.1]);
-
-    %The movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.'); 
-    
-    %X-Position
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.'); 
-    h3=plot(time,100*X_estNT(1,:)','g-.'); 
-
-    %Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.'); 
-    h3=plot(time,100*X_estNT(2,:)','g-.'); 
-
-    %X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
-
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.'); 
-    h3=plot(time,100*X_estNT(4,:)','g-.');
-
-    
-    
-
-end
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-        0*randn(numCells,2)];
-
-
-
-    %Add realization by thinning with history
-    dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-
-    % Decode the x-y trajectory
-
-    % Enforce that the maximum time resolution is delta
-    spikeColl = nstColl(nst);
-    spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix; 
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
-
-    % Starting states are equally probable
-    Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
-    x0{1} = X(ind{1},1);
-    yT{1} = X(ind{1},end);
-    Pi0    = Px0;
-    PiT{1} = 1e-9*eye(size(x0{1},1),size(x0{1},1));
-
-    x0{2} = X(ind{2},1);
-    yT{2} = X(ind{2},end);
-    PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
-
-
-    % Run the Hybrid Point Process Filter 
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-    
-    %Store the results for computing relevant statistics later
-    X_estAll(:,:,n) = X_est;
-    X_estNTAll(:,:,n) = X_estNT;
-    S_estAll(n,:)=S_est;
-    S_estNTAll(n,:)=S_estNT;
-    MU_estAll(:,:,n)=MU_est;
-    MU_estNTAll(:,:,n) = MU_estNT;
-    
-
-    %State Estimate
-    subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis; 
-    axis([v(1) v(2) 0.5 2.5]); 
-
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
-    subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
-    axis([min(time) max(time) 0 1.1]);
-
-    %The movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.'); 
-    
-    %X-Position
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.'); 
-    h3=plot(time,100*X_estNT(1,:)','g-.'); 
-
-    %Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.'); 
-    h3=plot(time,100*X_estNT(2,:)','g-.'); 
-
-    %X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
-
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.'); 
-    h3=plot(time,100*X_estNT(4,:)','g-.');
-
-    
-    
-
-end
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-        0*randn(numCells,2)];
-
-
-
-    %Add realization by thinning with history
-    dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-
-    % Decode the x-y trajectory
-
-    % Enforce that the maximum time resolution is delta
-    spikeColl = nstColl(nst);
-    spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix; 
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
-
-    % Starting states are equally probable
-    Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
-    x0{1} = X(ind{1},1);
-    yT{1} = X(ind{1},end);
-    Pi0    = Px0;
-    PiT{1} = 1e-9*eye(size(x0{1},1),size(x0{1},1));
-
-    x0{2} = X(ind{2},1);
-    yT{2} = X(ind{2},end);
-    PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
-
-
-    % Run the Hybrid Point Process Filter 
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-    
-    %Store the results for computing relevant statistics later
-    X_estAll(:,:,n) = X_est;
-    X_estNTAll(:,:,n) = X_estNT;
-    S_estAll(n,:)=S_est;
-    S_estNTAll(n,:)=S_estNT;
-    MU_estAll(:,:,n)=MU_est;
-    MU_estNTAll(:,:,n) = MU_estNT;
-    
-
-    %State Estimate
-    subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis; 
-    axis([v(1) v(2) 0.5 2.5]); 
-
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
-    subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
-    axis([min(time) max(time) 0 1.1]);
-
-    %The movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.'); 
-    
-    %X-Position
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.'); 
-    h3=plot(time,100*X_estNT(1,:)','g-.'); 
-
-    %Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.'); 
-    h3=plot(time,100*X_estNT(2,:)','g-.'); 
-
-    %X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
-
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.'); 
-    h3=plot(time,100*X_estNT(4,:)','g-.');
-
-    
-    
-
-end
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-        0*randn(numCells,2)];
-
-
-
-    %Add realization by thinning with history
-    dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-
-    % Decode the x-y trajectory
-
-    % Enforce that the maximum time resolution is delta
-    spikeColl = nstColl(nst);
-    spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix; 
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
-
-    % Starting states are equally probable
-    Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
-    x0{1} = X(ind{1},1);
-    yT{1} = X(ind{1},end);
-    Pi0    = Px0;
-    PiT{1} = 1e-9*eye(size(x0{1},1),size(x0{1},1));
-
-    x0{2} = X(ind{2},1);
-    yT{2} = X(ind{2},end);
-    PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
-
-
-    % Run the Hybrid Point Process Filter 
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-    
-    %Store the results for computing relevant statistics later
-    X_estAll(:,:,n) = X_est;
-    X_estNTAll(:,:,n) = X_estNT;
-    S_estAll(n,:)=S_est;
-    S_estNTAll(n,:)=S_estNT;
-    MU_estAll(:,:,n)=MU_est;
-    MU_estNTAll(:,:,n) = MU_estNT;
-    
-
-    %State Estimate
-    subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis; 
-    axis([v(1) v(2) 0.5 2.5]); 
-
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
-    subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
-    axis([min(time) max(time) 0 1.1]);
-
-    %The movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.'); 
-    
-    %X-Position
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.'); 
-    h3=plot(time,100*X_estNT(1,:)','g-.'); 
-
-    %Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.'); 
-    h3=plot(time,100*X_estNT(2,:)','g-.'); 
-
-    %X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
-
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.'); 
-    h3=plot(time,100*X_estNT(4,:)','g-.');
-
-    
-    
-
-end
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-        0*randn(numCells,2)];
-
-
-
-    %Add realization by thinning with history
-    dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
+        % Data sampled at 30 Hz but just to be sure
+        delta=min(diff(time));
+        sampleRate = round(1/delta);
+        % Define Covariates for the analysis
+        baseline = Covariate(time,ones(length(x),1),'Baseline','time','s','',...
+                            {'mu'});
+        zernike  = Covariate(time,z,'Zernike','time','s','m',{'z1','z2','z3',...
+                            'z4','z5','z6','z7','z8','z9','z10'});
+        gaussian = Covariate(time,[x y x.^2 y.^2 x.*y],'Gaussian','time',...
+                            's','m',{'x','y','x^2','y^2','x*y'});
+        covarColl = CovColl({baseline,gaussian,zernike});
 
-    % Decode the x-y trajectory
+        % Create the trial structure
+        spikeColl = nstColl(nst);
+        trial     = Trial(spikeColl,covarColl);
 
-    % Enforce that the maximum time resolution is delta
-    spikeColl = nstColl(nst);
-    spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix; 
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
 
-    % Starting states are equally probable
-    Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
-    x0{1} = X(ind{1},1);
-    yT{1} = X(ind{1},end);
-    Pi0    = Px0;
-    PiT{1} = 1e-9*eye(size(x0{1},1),size(x0{1},1));
+        % Define how we want to analyze the data
+        tc{1} = TrialConfig({{'Baseline','mu'},{'Gaussian',...
+                            'x','y','x^2','y^2','x*y'}},sampleRate,[]);
+        tc{1}.setName('Gaussian');
+        tc{2} = TrialConfig({{'Zernike' 'z1','z2','z3','z4','z5','z6',...
+                            'z7','z8','z9','z10'}},sampleRate,[]);
+        tc{2}.setName('Zernike');
+        tcc = ConfigColl(tc);
 
-    x0{2} = X(ind{2},1);
-    yT{2} = X(ind{2},end);
-    PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
+        % Perform Analysis (Commented to since data already saved)
+         results =Analysis.RunAnalysisForAllNeurons(trial,tcc,0);
 
+        % Save results
+            resStruct =FitResult.CellArrayToStructure(results);
+            filename = fullfile(dataDir,['PlaceCellAnimal' num2str(n) 'Results']);
+            save(filename,'resStruct');
+    end
+end
+
+

View Summary Statistics

+

Note the Zernike Polynomials yield better fits in terms of decreased KS Statistics (less deviation from the 45 degree line), reduced AIC and reduced BIC across the majority of cells and for both animals

+
clear Summary;
+numAnimals =2;
 
-    % Run the Hybrid Point Process Filter 
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-    
-    %Store the results for computing relevant statistics later
-    X_estAll(:,:,n) = X_est;
-    X_estNTAll(:,:,n) = X_estNT;
-    S_estAll(n,:)=S_est;
-    S_estNTAll(n,:)=S_estNT;
-    MU_estAll(:,:,n)=MU_est;
-    MU_estNTAll(:,:,n) = MU_estNT;
-    
+for n=1:numAnimals
+    resData = load(fullfile(dataDir,['PlaceCellAnimal' num2str(n) 'Results.mat']));
+    results = FitResult.fromStructure(resData.resStruct);
+    Summary{n} = FitResSummary(results);
+%     Summary{n}.plotSummary;
+end
+
+
close all;
+scrsz = get(0,'ScreenSize');
+h=figure('OuterPosition',[scrsz(3)*.1 scrsz(4)*.1 scrsz(3)*.6 scrsz(4)*.5]);
+subplot(1,3,1);
+maxLength = max([Summary{1}.numNeurons,Summary{2}.numNeurons]);
+dKS = nan(maxLength, 2);
+dKS(1:Summary{1}.numNeurons,1) = (Summary{1}.KSStats(:,1)-Summary{1}.KSStats(:,2)) ;
+dKS(1:Summary{2}.numNeurons,2) = (Summary{2}.KSStats(:,1)-Summary{2}.KSStats(:,2)) ;
 
-    %State Estimate
-    subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis; 
-    axis([v(1) v(2) 0.5 2.5]); 
+boxplot(dKS ,{'Animal 1', 'Animal 2'},'labelorientation','inline');
+title('\Delta KS Statistic','FontWeight','bold','FontSize',14,...
+    'FontName','Arial');
 
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
-    subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
-    axis([min(time) max(time) 0 1.1]);
 
-    %The movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.'); 
-    
-    %X-Position
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.'); 
-    h3=plot(time,100*X_estNT(1,:)','g-.'); 
+subplot(1,3,2);
+dAIC = nan(maxLength, 2);
+dAIC(1:Summary{1}.numNeurons,1) = Summary{1}.getDiffAIC(1);
+dAIC(1:Summary{2}.numNeurons,2) = Summary{2}.getDiffAIC(1);
 
-    %Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.'); 
-    h3=plot(time,100*X_estNT(2,:)','g-.'); 
+boxplot(dAIC ,{'Animal 1', 'Animal 2'},'labelorientation','inline');
+title('\Delta AIC','FontWeight','bold','FontSize',14,'FontName','Arial');
 
-    %X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
 
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.'); 
-    h3=plot(time,100*X_estNT(4,:)','g-.');
+subplot(1,3,3);
+dBIC = nan(maxLength, 2);
+dBIC(1:Summary{1}.numNeurons,1) = Summary{1}.getDiffBIC(1);
+dBIC(1:Summary{2}.numNeurons,2) = Summary{2}.getDiffBIC(1);
 
-    
-    
+boxplot(dBIC ,{'Animal 1', 'Animal 2'},'labelorientation','inline'); %ylabel('\Delta BIC'); %xticklabel_rotate([],45,[],'Fontsize',6);
+title('\Delta BIC','FontWeight','bold','FontSize',14,'FontName','Arial');
+
+%  close all;
+
+

Visualize the results

+
close all;
+% Define a grid
+[x_new,y_new]=meshgrid(-1:.01:1); %define new x and y
+y_new = flipud(y_new); x_new = fliplr(x_new);
+[theta_new,r_new] = cart2pol(x_new,y_new);
+
+%Data for the gaussian fit
+newData{1} =ones(size(x_new));
+newData{2} =x_new; newData{3} =y_new;
+newData{4} =x_new.^2; newData{5} =y_new.^2;
+newData{6} =x_new.*y_new;
+
+
+% Zernike polynomials only defined on the unit disk
+idx = r_new<=1;
+zpoly = cell(1,10);
+cnt=0;
+for l=0:3
+   for m=-l:l
+       if(~any(mod(l-m,2)))
+        cnt = cnt+1;
+        temp = nan(size(x_new));
+        temp(idx) = zernfun(l,m,r_new(idx),theta_new(idx),'norm');
+        zpoly{cnt} = temp;
+       end
+   end
+end
+
+
+
+for n=1:numAnimals
+
+    clear lambdaGaussian lambdaZernike;
+    load(fullfile(placeCellDataDir,['PlaceCellDataAnimal' num2str(n) '.mat']));
+    resData = load(fullfile(dataDir,['PlaceCellAnimal' num2str(n) 'Results.mat']));
+    results = FitResult.fromStructure(resData.resStruct);
+
+    for i=1:length(neuron)
+        % Evaluate our fits using the new data and the estimated parameters
+        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
+        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
+    end
+
+
+
+
+    % Plot the receptive fields
+    for i=1:length(neuron)
+        % 3d plot of an example place field
 
-end
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-        0*randn(numCells,2)];
 
+        % 2d plot of all the cell's fields
+        if(n==1)
+            h4=figure(4);
+            colormap('jet');
+            if(i==1)
+                tb=annotation(h4,'textbox',...
+                    [0.283261904761904 0.928571428571418 ...
+                    0.392857142857143 0.0595238095238095],...
+                    'String',{['Gaussian Place Fields - Animal#' ...
+                    num2str(n)]},'FitBoxToText','on','Fontsize',11,...
+                    'FontName','Arial','FontWeight','bold','LineStyle',...
+                    'none','HorizontalAlignment','center'); hold on;
+            end
+            subplot(7,7,i);
+        elseif(n==2)
+            h6=figure(6);
+            colormap('jet');
+            if(i==1)
+                annotation(h6,'textbox',...
+                    [0.283261904761904 0.928571428571418 ...
+                    0.392857142857143 0.0595238095238095],...
+                    'String',{['Gaussian Place Fields - Animal#' ...
+                    num2str(n)]},'FitBoxToText','on','Fontsize',11,...
+                    'FontName','Arial','FontWeight','bold','LineStyle',...
+                    'none','HorizontalAlignment','center'); hold on;
+            end
+            subplot(6,7,i);
+        end
+        pcolor(x_new,y_new,lambdaGaussian{i}), shading interp
+        axis square; set(gca,'xtick',[],'ytick',[]);
+        set(gca, 'Box'         , 'off');
 
+        if(n==1)
+            h5=figure(5);
+            colormap('jet');
+            if(i==1)
+                annotation(h5,'textbox',...
+                    [0.303261904761904 0.928571428571418 ...
+                    0.392857142857143 0.0595238095238095],...
+                    'String',{['Zernike Place Fields - Animal#' ...
+                    num2str(n)]},'FitBoxToText','on','Fontsize',11,...
+                    'FontName','Arial','FontWeight','bold','LineStyle','none'); hold on;
 
-    %Add realization by thinning with history
-    dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
+            end
+            subplot(7,7,i);
+        elseif(n==2)
+            h7=figure(7);
+            colormap('jet');
+            if(i==1)
+               annotation(h7,'textbox',...
+                    [0.303261904761904 0.928571428571418 ...
+                    0.392857142857143 0.0595238095238095],...
+                    'String',{['Zernike Place Fields - Animal#' ...
+                    num2str(n)]},'FitBoxToText','on','Fontsize',11,...
+                    'FontName','Arial','FontWeight','bold','LineStyle',...
+                    'none','HorizontalAlignment','center'); hold on;
+            end
+            subplot(6,7,i);
+        end
+        pcolor(x_new,y_new,lambdaZernike{i}), shading interp
+        axis square;
+        set(gca,'xtick',[],'ytick',[]);
+        set(gca, 'Box'         , 'off');
+    end
 
-    % Decode the x-y trajectory
 
-    % Enforce that the maximum time resolution is delta
-    spikeColl = nstColl(nst);
-    spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix; 
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
+end
+
+
    clear lambdaGaussian lambdaZernike;
+    load(fullfile(placeCellDataDir,'PlaceCellDataAnimal1.mat'));
+    resData = load(fullfile(dataDir,'PlaceCellAnimal1Results.mat'));
+    results = FitResult.fromStructure(resData.resStruct);
 
-    % Starting states are equally probable
-    Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
-    x0{1} = X(ind{1},1);
-    yT{1} = X(ind{1},end);
-    Pi0    = Px0;
-    PiT{1} = 1e-9*eye(size(x0{1},1),size(x0{1},1));
+    for i=1:length(neuron)
+        % Evaluate our fits using the new data and the estimated parameters
+        lambdaGaussian{i} = results{i}.evalLambda(1,newData);
+        lambdaZernike{i} =  results{i}.evalLambda(2,zpoly);
+    end
 
-    x0{2} = X(ind{2},1);
-    yT{2} = X(ind{2},end);
-    PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
 
 
-    % Run the Hybrid Point Process Filter 
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-    
-    %Store the results for computing relevant statistics later
-    X_estAll(:,:,n) = X_est;
-    X_estNTAll(:,:,n) = X_estNT;
-    S_estAll(n,:)=S_est;
-    S_estNTAll(n,:)=S_estNT;
-    MU_estAll(:,:,n)=MU_est;
-    MU_estNTAll(:,:,n) = MU_estNT;
-    
+%     h1=plot(x,y,'b');
+%     h2=plot(x,y,'g');
+    %
+    exampleCell = 25;
+%     figure(8);
+%     plot(x,y,'b',neuron{exampleCell}.xN,neuron{exampleCell}.yN,'r.');
+%     xlabel('x'); ylabel('y');
+%     title(['Animal#1, Cell#' num2str(exampleCell)]);
+%
+    close all;
+    h9=figure(9);
+    h_mesh = mesh(x_new,y_new,lambdaGaussian{exampleCell},'AlphaData',0);
+    get(h_mesh,'AlphaData');
+    set(h_mesh,'FaceAlpha',0.2,'EdgeAlpha',0.2,'EdgeColor','b');
+    hold on;
+    h_mesh = mesh(x_new,y_new,lambdaZernike{exampleCell},'AlphaData',0);
+    get(h_mesh,'AlphaData');
+    set(h_mesh,'FaceAlpha',0.2,'EdgeAlpha',0.2,'EdgeColor','g');
 
-    %State Estimate
-    subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis; 
-    axis([v(1) v(2) 0.5 2.5]); 
 
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
-    subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
-    axis([min(time) max(time) 0 1.1]);
+%     h_legend=legend('\lambda_{Gaussian}','\lambda_{Zernike}');
+%     set(h_legend,'FontSize',20);
+    plot(x,y,neuron{exampleCell}.xN,neuron{exampleCell}.yN,'r.');
+    axis tight square;
+    xlabel('x position'); ylabel('y position');
+    title(['Animal#1, Cell#' num2str(exampleCell)],'FontWeight','bold',...
+        'Fontsize',12,'FontName','Arial');
+    hx=get(gca,'XLabel');  hy=get(gca,'YLabel');
+    set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
+
+

Example 5 - STIMULUS DECODING

+

In this example we show how to decode a univariate and a bivariate stimulus based on a point process observations using nSTAT. Even though due to the simulated nature of the data, we know the exact condition intensity function, we estimate the parameters before moving on to the decoding stage.

+

Generate the conditional Intensity Function

+
    close all; clear all;
+    [dataDir,mEPSCDir,explicitStimulusDir,psthDir,placeCellDataDir] = ...
+        getPaperDataDirs();
+    delta = 0.001; Tmax = 1;
+    time = 0:delta:Tmax;
+    numRealizations = 20;
+    f=2; b1=randn(numRealizations,1);b0=log(10*delta)+randn(numRealizations,1);
+    x = sin(2*pi*f*time);
+    clear nst;
+    for i=1:numRealizations
+        expData = exp(b1(i)*x+b0(i));
+        lambdaData = expData./(1+expData);
+
+        if(i==1)
+            lambda = Covariate(time,lambdaData./delta, ...
+                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
+                {{' ''b'', ''LineWidth'' ,2'}});
+        else
+            tempLambda = Covariate(time,lambdaData./delta, ...
+                '\Lambda(t)','time','s','spikes/sec',{'\lambda_{1}'},...
+                {{' ''b'', ''LineWidth'' ,2'}});
+            lambda = lambda.merge(tempLambda);
+        end
+
+        spikeColl = CIF.simulateCIFByThinningFromLambda(...
+            lambda.getSubSignal(i),1);
+        nst{i} = spikeColl.getNST(1);
+    end
+        spikeColl = nstColl(nst);scrsz = get(0,'ScreenSize');
+        h=figure('Position',[scrsz(3)*.1 scrsz(4)*.1 ...
+            scrsz(3)*.6 scrsz(4)*.8]);
+%         figure;
+        subplot(3,1,1); plot(time,x,'k');
+        set(gca,'xtick',[],'xtickLabel',[]); ylabel('Stimulus');
+            hx=get(gca,'XLabel');  hy=get(gca,'YLabel');
+            set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
+            title('Driving Stimulus','FontWeight','bold',...
+                'FontSize',14,'FontName','Arial');
+        subplot(3,1,2); lambda.plot([],{{' ''k'',''Linewidth'',1'}});
+            legend off;
+            hy=ylabel('Firing Rate [spikes/sec]', 'Interpreter','none');
+            hx=xlabel('','Interpreter','none');
+            set([hx, hy],'FontName', 'Arial','FontSize',12,...
+                'FontWeight','bold');
+            set(gca,'xtickLabel',[]);
+            title('Conditional Intensity Functions','FontWeight',...
+                'bold','FontSize',14,'FontName','Arial');
+
+        subplot(3,1,3); spikeColl.plot;
+            set(gca,'ytick',0:10:numRealizations,'ytickLabel',...
+                0:10:numRealizations);
+            xlabel('time [s]','Interpreter','none');
+            ylabel('Cell Number','Interpreter','none');
+            hx=get(gca,'XLabel');  hy=get(gca,'YLabel');
+            set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
+            title('Point Process Sample Paths','FontWeight',...
+                'bold','FontSize',14,'FontName','Arial');
+
+stim = Covariate(time,sin(2*pi*f*time),'Stimulus','time','s','V',{'stim'});
+baseline = Covariate(time,ones(length(time),1),'Baseline','time','s','',...
+                    {'constant'});
+
+%     close all;
+
+
close all;
+
+clear lambdaCIF;
+spikeColl.resample(1/delta);
+dN=spikeColl.dataToMatrix;
+
+% Make noise according to the dynamic range of the stimulus
+Q=std(stim.data(2:end)-stim.data(1:end-1));
+Px0=.1; A=1;
+x0 = x(:,1); yT=x(:,end);
+Pi0 = eps*eye(size(x0,1),size(x0,1));
+PiT = eps*eye(size(x0,1),size(x0,1));
+
+
+[x_p, W_p, x_u, W_u] = DecodingAlgorithms.PPDecodeFilterLinear(A, ...
+    Q, dN',b0,b1','binomial',delta);
+%
+h=figure('Position',[scrsz(3)*.1 scrsz(4)*.1 scrsz(3)*.8 scrsz(4)*.6]);
+zVal=1.96;
+ciLower = min(x_u(1:end)-zVal*sqrt(squeeze(W_u(1:end)))',...
+    x_u(1:end)+zVal*sqrt(squeeze(W_u(1:end))'));
+ciUpper = max(x_u(1:end)-zVal*sqrt(squeeze(W_u(1:end)))',...
+    x_u(1:end)+zVal*sqrt(squeeze(W_u(1:end))'));
+
+estimatedStimulus = Covariate(time,x_u(1:end),'\hat{x}(t)','time','s','');
+CI= ConfidenceInterval(time,[ciLower', ciUpper'],'\hat{x}(t)','time','s','');
+estimatedStimulus.setConfInterval(CI);
+
+% hold all;
+% hEst=plot(time,x_u(1:end),'b','Linewidth',2); hold on;
+% plot(time, [ciUpper', ciLower'],'b');
+
+hEst = estimatedStimulus.plot([],{{' ''k'',''Linewidth'',4'}});
+hStim=stim.plot([],{{' ''b'',''Linewidth'',4'}});
+legend off;
+h_legend=legend([hEst(1) hStim],'Decoded','Actual');
+set(h_legend,'Interpreter','none');
+set(h_legend,'FontSize',22);
+title(['Decoded Stimulus +/- 95% CIs with ' num2str(numRealizations) ' cells'],...
+    'FontWeight','bold','Fontsize',22,'FontName','Arial');
+xlabel('time [s]','Interpreter','none');
+ylabel('Stimulus','Interpreter','none');
+hx=get(gca,'XLabel');  hy=get(gca,'YLabel');
+set([hx, hy],'FontName', 'Arial','FontSize',22,'FontWeight','bold');
+
+

Example 5b - Arm reaching to target Simulation

+

See L. Srinivasan, U. T. Eden, A. S. Willsky, and E. N. Brown, "A state-space analysis for reconstruction of goal-directed movements using neural signals.," Neural computation, vol. 18, no. 10, pp. 2465?2494, Oct. 2006.

+
    close all;
+    clear all;
+    [dataDir,mEPSCDir,explicitStimulusDir,psthDir,placeCellDataDir] = ...
+        getPaperDataDirs();
+    %Process noise covariance only drives the movement velocity
+    q=1e-4;
+    Q=diag([1e-12 1e-12 q q]);
+
+    delta = .001;        % Time increment
+    r=1e-6;   % in meters^2
+    p=1e-6;    % in meters^2/s^2
+    PiT=diag([r r p p]); % Uncertainty in the target state
+    Pi0=PiT;
+    T=2;                 % Reach Duration
+
+    x0 = [0;0;0;0];     % Initial Position and velocities (states)
+    xT = [-.35;.2; 0;0];% Final Target
+    time=0:delta:T;     % time vector
+
+    A=[1 0 delta 0    ; %State transition matrix
+       0 1 0     delta;
+       0 0 1     0    ;
+       0 0 0     1    ];
+
+    x=zeros(4,length(time));
+
+
+% Simulate a reach trajectory
+% Differs from reference by multiplication by delta instead of division so
+% that the velocity has units of meters per second
+    R=chol(Q);
+    L=chol(PiT);
+    for k=1:length(time)
+        if(k==1)
+            x(:,k)=x0;
+        else
+             x(:,k)=A*x(:,k-1)+...
+                 delta/(2)*(pi/T)^2*cos(pi*time(k)/T)*[0;0;...
+                 xT(1)-x0(1);xT(2)-x0(2)]; %Reach to target model
+            %x(:,k)=A*x(:,k-1)+R*randn(size(x,1),1); %Random walk model
+        end
+
+    end
+    xT =x(:,end); % The target generated by the model
+    yT=xT;        % Assume we have observed the actual target position with uncertainty PiT
+
+    %Define Q according to the dynamic range of the movement above
+    Q=diag(var(diff(x,[],2),[],2))*100;
+
+    % Plot the movement trajectories and the hand path
+    scrsz = get(0,'ScreenSize');
+    fig1=figure('OuterPosition',[scrsz(3)*.1 scrsz(4)*.1 ...
+        scrsz(3)*.8 scrsz(4)*.8]);
+    %Plot The movement path
+    subplot(4,2,[1 3]);
+    plot(100*x(1,:),100*x(2,:),'k','Linewidth',2);
+    xlabel('X Position [cm]'); ylabel('Y Position [cm]');
+    hx=get(gca,'XLabel');  hy=get(gca,'YLabel');
+    set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
+    title('Reach Path','FontWeight','bold','Fontsize',14,'FontName','Arial');
+    hold on;
+    axis([sort([100*x0(1)+5, 100*xT(1)-5]), sort([100*x0(2)-5, 100*xT(2)+5])]);
+    h1=plot(100*x(1,1),100*x(2,1),'bo','MarkerSize',14);
+    h2=plot(100*x(1,end),100*x(2,end),'ro','MarkerSize',14);
+    legend([h1 h2],'Start','Finish','Location','NorthEast');
+
+
+    subplot(4,2,5); h1=plot(time,100*x(1,:),'k','Linewidth',2); hold on;
+    h2=plot(time,100*x(2,:),'k-.','Linewidth',2);
+    h_legend=legend([h1,h2],'x','y','Location','NorthEast');
+    set(h_legend,'FontSize',14)
+    pos = get(h_legend,'position');
+    set(h_legend, 'position',[pos(1)+.06 pos(2)+.01 pos(3:4)]);
+    hx=xlabel('time [s]'); hy=ylabel('Position [cm]');
+    set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
+    % Plot the velocity profiles
+
+    subplot(4,2,7);
+    h1=plot(time,100*x(3,:),'k','Linewidth',2); hold on;
+    h2=plot(time,100*x(4,:),'k-.','Linewidth',2);
+    h_legend=legend([h1 h2],'v_x','v_y','Location','NorthEast');
+    xlabel('time [s]');
+    set(h_legend,'FontSize',14);
+    pos = get(h_legend,'position');
+    set(h_legend, 'position',[pos(1)+.06 pos(2)+.01 pos(3:4)]);
+    hx=xlabel('time [s]'); hy=ylabel('Velocity [cm/s]');
+    set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
+    %
+
+    gamma=0;
+    windowTimes=[0, 0.001];
+
+
+% Simulate neural responses
+    % logit(lambda_i*delta) = mu_i + b_x_i*v_x + b_y_i*v_y
+    % logit(lambda_i*delta) = X_i*beta_i;
+    numCells = 20;
+    bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
+    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
+    phiMaxNorm = (phiMax+pi)./(2*pi);
+    meanMu = log(10*delta); % baseline firing rate -10Hz
+    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1)
 
-    %The movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.'); 
-    
-    %X-Position
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.'); 
-    h3=plot(time,100*X_estNT(1,:)','g-.'); 
+    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
+    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
+    fitType='binomial';
+    clear nst;
+    for i=1:numCells
+         tempData  = exp(dataMat*coeffs(i,:)');
 
-    %Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.'); 
-    h3=plot(time,100*X_estNT(2,:)','g-.'); 
+         if(strcmp(fitType,'poisson'))
+             lambdaData = tempData;
+         else
+            lambdaData = tempData./(1+tempData); % Conditional Intensity Function for ith cell
+         end
+         lambda{i}=Covariate(time,lambdaData./delta, ...
+             '\Lambda(t)','time','s','spikes/sec',...
+             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
+         lambda{i}=lambda{i}.resample(1/delta);
 
-    %X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
+         % Generate CIF representation in case we want to use the symbolic
+         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
+         lambdaCIF{i} = CIF([MuCoeffs(i) 0 0 bCoeffs(i,:)],...
+             {'1','x','y','vx','vy'},{'x','y','vx','vy'},fitType);
+         % generate one realization for each cell
+         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);          nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
+         nst{i}.setName(num2str(i));              % give each cell a unique name
+         subplot(4,2,[6 8]);
+         h2=lambda{i}.plot([],{{' ''k'', ''LineWidth'' ,.5'}});
+         legend off; hold all; % Plot the CIF
 
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.'); 
-    h3=plot(time,100*X_estNT(4,:)','g-.');
 
-    
-    
 
-end
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
-        0*randn(numCells,2)];
+    end
+    title('Neural Conditional Intensity Functions','FontWeight',...
+        'bold','Fontsize',14,'FontName','Arial');
+    hx=xlabel('time [s]','Interpreter','none');
+    hy=ylabel('Firing Rate [spikes/sec]','Interpreter','none');
+    set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
+    spikeColl = nstColl(nst); % Create a neural spike train collection
 
+    subplot(4,2,[2,4]); spikeColl.plot;
+    set(gca,'xtick',[],'xtickLabel',[]);
+    title('Neural Raster','FontWeight','bold','Fontsize',14,...
+        'FontName','Arial');
+    hx=xlabel('time [s]','Interpreter','none');
+    hy=ylabel('Cell Number','Interpreter','none');
+    set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
 
+%     close all;
+
+
close all;
+numExamples=20;
+scrsz = get(0,'ScreenSize');
+fig1=figure('OuterPosition',[scrsz(3)*.1 scrsz(4)*.1 ...
+    scrsz(3)*.6 scrsz(4)*.9]);
+for k=1:numExamples
+     bCoeffs=10*(rand(numCells,2)-.5);           % b_i = [b_x_i b_y_i] ~ U(-5, 5);
+    phiMax = atan2(bCoeffs(:,2),bCoeffs(:,1));  % Maximal firing direction of cell
+    phiMaxNorm = (phiMax+pi)./(2*pi);
+    meanMu = log(10*delta);  % baseline firing rate
+    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1)
 
-    %Add realization by thinning with history
-    dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
+    dataMat = [ones(length(time),1) x(3,:)' x(4,:)']; % design matrix: X (
+    coeffs = [MuCoeffs bCoeffs]; % coefficient vector: beta
+    fitType='binomial';
+    clear nst lambda;
+
+
+    for i=1:numCells
+        tempData  = exp(dataMat*coeffs(i,:)');
+         if(strcmp(fitType,'poisson'))
+            lambdaData = tempData;
+         else
+             % Conditional Intensity Function for ith cell
             lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
+         end
+         lambda{i}=Covariate(time,lambdaData./delta, ...
+             '\Lambda(t)','time','s','spikes/sec',...
+             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'' '}});
+         lambda{i}=lambda{i}.resample(1/delta);
 
-    % Decode the x-y trajectory
+         % Generate CIF representation in case we want to use the symbolic
+         % versions of the PPDecodeFilter (i.e. not PPDecodeFilterLinear
+         % generate one realization for each cell
+         tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1);
+         nst{i} = tempSpikeColl{i}.getNST(1);     % grab the realization
+         nst{i}.setName(num2str(i));              % give each cell a unique name
 
-    % Enforce that the maximum time resolution is delta
-    spikeColl = nstColl(nst);
-    spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix; 
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
+    end
 
-    % Starting states are equally probable
-    Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
-    x0{1} = X(ind{1},1);
-    yT{1} = X(ind{1},end);
-    Pi0    = Px0;
-    PiT{1} = 1e-9*eye(size(x0{1},1),size(x0{1},1));
+    % Plot the neural raster across all the cells
+    spikeColl = nstColl(nst); % Create a neural spike train collection
 
-    x0{2} = X(ind{2},1);
-    yT{2} = X(ind{2},end);
-    PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
+    % Based on the temporal resolution defined by delta, bin the data and get
+    % a matrix representation of the neural firing
+    dN=spikeColl.dataToMatrix';
+    dN(dN>1)=1; % more than one spike per bin will be treated as one spike. In
+                % general we should pick delta small enough so that there is
+                % only one spike per bin
 
+    [C,N]   = size(dN); % N time samples, C cells
 
-    % Run the Hybrid Point Process Filter 
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
-        coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-    
-    %Store the results for computing relevant statistics later
-    X_estAll(:,:,n) = X_est;
-    X_estNTAll(:,:,n) = X_estNT;
-    S_estAll(n,:)=S_est;
-    S_estNTAll(n,:)=S_estNT;
-    MU_estAll(:,:,n)=MU_est;
-    MU_estNTAll(:,:,n) = MU_estNT;
-    
+    beta=[zeros(2,numCells);  bCoeffs'];
 
-    %State Estimate
-    subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis; 
-    axis([v(1) v(2) 0.5 2.5]); 
 
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
-    subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
-    axis([min(time) max(time) 0 1.1]);
+    %Use the Goal Directed Movement Version of the Point Process adaptive
+    %Filter
+    [x_p, W_p, x_u, W_u,x_uT,W_uT,x_pT,W_pT] = ...
+        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
+        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0, Pi0, yT,PiT,0);
+
+    %Use the Free Movement Version of the Point Process adaptive
+    %Filter
+    [x_pf, W_pf, x_uf, W_uf] = ...
+        DecodingAlgorithms.PPDecodeFilterLinear(A, Q, dN,...
+        MuCoeffs,beta,fitType,delta,gamma,windowTimes,x0);
+
+
+    if(k==numExamples)
+        subplot(4,2,1:4);h1=plot(100*x(1,:),100*x(2,:),'k','LineWidth',3);
+        hold on;
+        axis([sort([100*x0(1)+5, 100*xT(1)-5]), ...
+            sort([100*x0(2)-5, 100*xT(2)+5])]);
+        title('Estimated vs. Actual Reach Paths',...
+            'FontWeight','bold','Fontsize',12,'FontName','Arial');
+    end
+    subplot(4,2,1:4);h2=plot(100*x_u(1,:)',100*x_u(2,:)','b'); hold all;
+    subplot(4,2,1:4);h3=plot(100*x_uf(1,:)',100*x_uf(2,:)','g');
+    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
+    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
+    h1=plot(100*x0(1),100*x0(2),'bo','MarkerSize',10); hold on;
+    h2=plot(100*xT(1),100*xT(2),'ro','MarkerSize',10);
+    legend([h1 h2],'Start','Finish','Location','NorthEast');
+
+
+    subplot(4,2,5);
+    h1=plot(time,100*x(1,:),'k','LineWidth',3); hold on;
+    h2=plot(time,100*x_u(1,:)','b');
+    h3=plot(time,100*x_uf(1,:)','g');
+    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
+    set(gca,'xtick',[],'xtickLabel',[]);
+    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
+    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
+
+    subplot(4,2,6);
+    h1=plot(time,100*x(2,:),'k','LineWidth',3); hold on;
+    h2=plot(time,100*x_u(2,:)','b');
+    h3=plot(time,100*x_uf(2,:)','g');
+    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
+        'PPAF','Location','SouthEast');
+    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]');
+    set(gca,'xtick',[],'xtickLabel',[]);
+    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
+    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
+    set(h_legend,'FontSize',10)
+    pos = get(h_legend,'position');
+    set(h_legend, 'position',[pos(1)-.63 pos(2)+.23 pos(3:4)]);
+
+    subplot(4,2,7);
+    h1=plot(time,100*x(3,:),'k','LineWidth',3); hold on;
+    h2=plot(time,100*x_u(3,:)','b');
+    h3=plot(time,100*x_uf(3,:)','g');
+    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
+    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
+    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
+
+    subplot(4,2,8);
+    h1=plot(time,100*x(4,:),'k','LineWidth',3); hold on;
+    h2=plot(time,100*x_u(4,:)','b');
+    h3=plot(time,100*x_uf(4,:)','g');
+    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
+    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
+    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
+
+
+end
+
+%     close all;
+
+

Experiment 6 - Hybrid Point Process Filter Example

+

NOTE THIS EXAMPLE WAS NOT INCLUDED IN THE FINAL VERSION OF THE PAPER This example is based on an implementation of the Hybrid Point Process filter described in General-purpose filter design for neural prosthetic devices by Srinivasan L, Eden UT, Mitter SK, Brown EN in J Neurophysiol. 2007 Oct, 98(4):2456-75.

+

Problem Statement

+

Suppose that a process of interest can be modeled as consisting of several discrete states where the evolution of the system under each state can be modeled as a linear state space model. The observations of both the state and the continuous dynamics are not direct, but rather observed through how the continuous and discrete states affect the firing of a population of neurons. The goal of the hybrid filter is to estimate both the continuous dynamics and the underlying system state from only the neural population firing (point process observations).

+

To illustrate the use of this filter, we consider a reaching task. We assume two underlying system states s=1="Not Moving"=NM and s=2="Moving"=M. Under the "Not Moving" the position of the arm remain constant, whereas in the "Moving" state, the position and velocities evolved based on the arm acceleration that is modeled as a gaussian white noise process.

+

Under both the "Moving" and "Not Moving" states, the arm evolution state vector is

+

+$${\bf{x}} = {[x,y,{v_x},{v_y},{a_x},{a_y}]^T}$$

+

Generated Simulated Arm Reach

+
clear all;
+[dataDir,mEPSCDir,explicitStimulusDir,psthDir,placeCellDataDir] = ...
+    getPaperDataDirs();
+close all;
+delta=0.001;
+Tmax=2;
+time=0:delta:Tmax;
+A{2} = [1 0 delta 0     delta^2/2   0;
+        0 1 0     delta 0           delta^2/2;
+        0 0 1     0     delta       0;
+        0 0 0     1     0           delta;
+        0 0 0     0     1           0;
+        0 0 0     0     0           1];
+
+A{1} = [1 0 0 0 0 0;
+        0 1 0 0 0 0;
+        0 0 0 0 0 0;
+        0 0 0 0 0 0;
+        0 0 0 0 0 0;
+        0 0 0 0 0 0];
+A{1} = [1 0;
+        0 1];
+
+Px0{2} =1e-6*eye(6,6);
+Px0{1} =1e-6*eye(2,2);
+
+minCovVal = 1e-12;
+covVal = 1e-3;
+
+
+
+Q{2}=[minCovVal     0   0     0   0       0;
+      0       minCovVal 0     0   0       0;
+      0       0   minCovVal   0   0       0;
+      0       0   0     minCovVal 0       0;
+      0       0   0     0   covVal      0;
+      0       0   0     0   0       covVal];
+
+Q{1}=minCovVal*eye(2,2);
+
+mstate = zeros(1,length(time));
+ind{1}=1:2;
+ind{2}=1:6;
+
+% Acceleration model
+X=zeros(max([size(A{1},1),size(A{2},1)]),length(time));
+p_ij = [.998 .002;
+        .001 .999];
+
+for i = 1:length(time)
+
+    if(i==1)
+        mstate(i) = 1;
+    else
+       if(rand(1,1)<p_ij(mstate(i-1),mstate(i-1)))
+            mstate(i) = mstate(i-1);
+       else
+           if(mstate(i-1)==1)
+               mstate(i) = 2;
+           else
+               mstate(i) = 1;
+           end
+       end
+    end
+    st = mstate(i);
+    R=chol(Q{st});
+    if(i<length(time))
+        X(ind{st},i+1) = A{st}*X(ind{st},i) + R*randn(length(ind{st}),1);
+    end
+
+end
+
+
+%save paperHybridFilterExample time Tmax delta mstate X p_ij ind A Q Px0
+load(fullfile(fileparts(which('nSTATPaperExamples')),'paperHybridFilterExample.mat'));
+Q{1}=minCovVal*eye(2,2);
+numCells=40;
+close all;
+scrsz = get(0,'ScreenSize');
+fig1=figure('OuterPosition',[scrsz(3)*.1 scrsz(4)*.1 ...
+    scrsz(3)*.8 scrsz(4)*.9]);
+subplot(4,2,[1 3]);
+plot(100*X(1,:),100*X(2,:),'k','Linewidth',2); hx=xlabel('X [cm]');
+hy=ylabel('Y [cm]');  hold on;
+set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
+title('Reach Path','FontWeight','bold','Fontsize',14,'FontName','Arial');
+hold on;
+h1=plot(100*X(1,1),100*X(2,1),'bo','MarkerSize',16);
+h2=plot(100*X(1,end),100*X(2,end),'ro','MarkerSize',16);
+legend([h1 h2],'Start','Finish','Location','NorthEast');
+
+
+
+subplot(4,2,[6 8]);
+plot(time,mstate,'k','Linewidth',2); axis tight; v=axis;
+axis([v(1) v(2) 0 3]);
+hx=xlabel('time [s]'); hy=ylabel('state');
+set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
+set(gca,'YTick',[1 2],'YTickLabel',{'N','M'})
+title('Discrete Movement State','FontWeight','bold','Fontsize',14,...
+    'FontName','Arial');
+
+subplot(4,2,5);
+h1=plot(time,100*X(1,1:end),'k','Linewidth',2); hold on;
+h2=plot(time,100*X(2,1:end),'k-.','Linewidth',2);
+hx=xlabel('time [s]'); hy=ylabel('Position [cm]');
+set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
+h_legend=legend([h1,h2],'x','y','Location','NorthEast');
+set(h_legend,'FontSize',14)
+pos = get(h_legend,'position');
+set(h_legend, 'position',[pos(1)+.06 pos(2)+.01 pos(3:4)]);
 
-    %The movement path
-    subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.'); 
-    
-    %X-Position
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.'); 
-    h3=plot(time,100*X_estNT(1,:)','g-.'); 
 
-    %Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.'); 
-    h3=plot(time,100*X_estNT(2,:)','g-.'); 
+subplot(4,2,7);
+h1=plot(time,100*X(3,1:end),'k','Linewidth',2); hold on;
+h2=plot(time,100*X(4,1:end),'k-.','Linewidth',2);
+hx=xlabel('time [s]'); hy=ylabel('Velocity [cm/s]');
+set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
+h_legend=legend([h1,h2],'v_{x}','v_{y}','Location','NorthEast');
+set(h_legend,'FontSize',14)
+pos = get(h_legend,'position');
+set(h_legend, 'position',[pos(1)+.06 pos(2)+.01 pos(3:4)]);
 
-    %X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
+meanMu = log(10*delta);  % baseline firing rate
+MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1)
+coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
+    0*randn(numCells,2)];
+%Add realization by thinning with history
+dataMat = [ones(size(X,2),1),X(:,1:end)'];
+% Generate M1 cells
+clear lambda tempSpikeColl lambdaCIF n;
+fitType ='binomial';
+% matlabpool open;
+ for i=1:numCells
+     tempData  = exp(dataMat*coeffs(i,:)');
+     if(strcmp(fitType,'binomial'));
+        lambdaData = tempData./(1+tempData);
+     else
+        lambdaData = tempData;
+     end
+     lambda{i}=Covariate(time,lambdaData./delta, ...
+         '\Lambda(t)','time','s','spikes/sec',...
+         {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
+     maxTimeRes = 0.001;
+     tempSpikeColl{i} = CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
+     n{i} = tempSpikeColl{i}.getNST(1);
+     n{i}.setName(num2str(i));
+ end
+ spikeColl = nstColl(n);
+ subplot(4,2,[2 4]);
+spikeColl.plot;
+set(gca,'xtick',[],'xtickLabel',[],'ytickLabel',[]);
+title('Neural Raster','FontWeight','bold','Fontsize',14,'FontName','Arial');
+hx=xlabel('time [s]','Interpreter','none');
+hy=ylabel('Cell Number','Interpreter','none');
+set([hx, hy],'FontName', 'Arial','FontSize',12,'FontWeight','bold');
 
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.'); 
-    h3=plot(time,100*X_estNT(4,:)','g-.');
+% close all;
+
+

Simulate Neural Firing

+

We simulate a population of neurons that fire in response to the movement velocity (x and y coorinates)

+
+%Use the data to estimate the process noise for the moving case and
+%non-moving case
 
-    
-    
+nonMovingInd = intersect(find(X(5,:)==0),find(X(6,:)==0));
+movingInd=setdiff(1:size(X,2),nonMovingInd);
+Q{2}=diag(var(diff(X(:,movingInd),[],2),[],2));
+Q{2}(1:4,1:4)=0;
+varNV=diag(var(diff(X(:,nonMovingInd),[],2),[],2));
+Q{1} = varNV(1:2,1:2);
+close all; clear S_est X_est MU_est S_estNT X_estNT MU_estNT;
+numExamples = 20;
+numCells=40;
+scrsz = get(0,'ScreenSize');
+fig1=figure('OuterPosition',[scrsz(3)*.1 scrsz(4)*.1 ...
+    scrsz(3)*.9 scrsz(4)*.9]);
 
-end
-    meanMu = log(10*delta);  % baseline firing rate 
-    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1) 
-    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
+for n=1:numExamples
+    meanMu = log(10*delta);  % baseline firing rate
+    MuCoeffs = meanMu+randn(numCells,1);   % mu_i ~ G(meanMu,1)
+    coeffs = [MuCoeffs 0*randn(numCells,2) 10*(rand(numCells,2)-.5) ...
         0*randn(numCells,2)];
 
 
 
-    %Add realization by thinning with history
+    %Add realization by thinning with history
     dataMat = [ones(size(X,2),1),X(:,1:end)'];
-    % Generate M1 cells
-    clear lambda tempSpikeColl lambdaCIF nst;
-    fitType ='binomial';
-    % matlabpool open;
-     for i=1:numCells
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
-         tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
-            lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
-         maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
-             CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
-         nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
+    % Generate M1 cells
+    clear lambda tempSpikeColl lambdaCIF nst;
+    fitType ='binomial';
+    % matlabpool open;
+     for i=1:numCells
          tempData  = exp(dataMat*coeffs(i,:)');
-         if(strcmp(fitType,'binomial'))
+         if(strcmp(fitType,'binomial'))
             lambdaData = tempData./(1+tempData);
-         end
-         lambda{i}=Covariate(time,lambdaData./delta, ...
-             '\Lambda(t)','time','s','spikes/sec',...
-             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
+         else
+            lambdaData = tempData;
+         end
+         lambda{i}=Covariate(time,lambdaData./delta, ...
+             '\Lambda(t)','time','s','spikes/sec',...
+             {strcat('\lambda_{',num2str(i),'}')},{{' ''b'', ''LineWidth'' ,2'}});
          maxTimeRes = 0.001;
-         tempSpikeColl{i} = ...
+         tempSpikeColl{i} = ...
              CIF.simulateCIFByThinningFromLambda(lambda{i},1,[]);
          nst{i} = tempSpikeColl{i}.getNST(1);
-         nst{i}.setName(num2str(i));    
-     end
+         nst{i}.setName(num2str(i));
+     end
 
-    % Decode the x-y trajectory
+    % Decode the x-y trajectory
 
-    % Enforce that the maximum time resolution is delta
+    % Enforce that the maximum time resolution is delta
     spikeColl = nstColl(nst);
     spikeColl.resample(1/delta);
-    dN = spikeColl.dataToMatrix; 
-    dN(dN>1)=1; %Avoid more than 1 spike per bin.
+    dN = spikeColl.dataToMatrix;
+    dN(dN>1)=1; %Avoid more than 1 spike per bin.
 
-    % Starting states are equally probable
+    % Starting states are equally probable
     Mu0=.5*ones(size(p_ij,1),1);
-    clear x0 yT clear Pi0 PiT;
+    clear x0 yT clear Pi0 PiT;
     x0{1} = X(ind{1},1);
     yT{1} = X(ind{1},end);
     Pi0    = Px0;
@@ -79609,172 +2358,281 @@
     PiT{2} = 1e-9*eye(size(x0{2},1),size(x0{2},1));
 
 
-    % Run the Hybrid Point Process Filter 
-    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
+    % Run the Hybrid Point Process Filter
+    [S_est, X_est, W_est, MU_est, X_s, W_s,pNGivenS]=...
+        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
         coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0, yT,PiT);
-    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
-        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
+    [S_estNT, X_estNT, W_estNT, MU_estNT, X_sNT, W_sNT,pNGivenSNT]=...
+        DecodingAlgorithms.PPHybridFilterLinear(A, Q, p_ij,Mu0, dN',...
         coeffs(:,1),coeffs(:,2:end)',fitType,delta,[],[],x0,Pi0);
-    
-    %Store the results for computing relevant statistics later
+
+    %Store the results for computing relevant statistics later
     X_estAll(:,:,n) = X_est;
     X_estNTAll(:,:,n) = X_estNT;
     S_estAll(n,:)=S_est;
     S_estNTAll(n,:)=S_estNT;
     MU_estAll(:,:,n)=MU_est;
     MU_estNTAll(:,:,n) = MU_estNT;
-    
 
-    %State Estimate
+
+    %State Estimate
     subplot(4,3,[1 4]);
-    plot(time,mstate,'k','LineWidth',3); hold all;
-    plot(time,S_est,'b-.','Linewidth',.5);
-    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis; 
-    axis([v(1) v(2) 0.5 2.5]); 
+    plot(time,mstate,'k','LineWidth',3); hold all;
+    plot(time,S_est,'b-.','Linewidth',.5);
+    plot(time,S_estNT,'g-.','Linewidth',.5); axis tight; v=axis;
+    axis([v(1) v(2) 0.5 2.5]);
 
-    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
+    %Movement State Probability (Non-movement State probability is 1-Pr(Movement))
     subplot(4,3,[7 10]);
-    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
-    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
+    plot(time,MU_est(2,:),'b-.','Linewidth',.5);  hold on;
+    plot(time,MU_estNT(2,:),'g-.','Linewidth',.5);  hold on;
     axis([min(time) max(time) 0 1.1]);
 
-    %The movement path
+    %The movement path
     subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
-    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
-    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.'); 
-    
-    %X-Position
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(1,:)','b-.'); 
-    h3=plot(time,100*X_estNT(1,:)','g-.'); 
+    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
+    h2=plot(100*X_est(1,:)',100*X_est(2,:)','b-.'); hold all;
+    h3=plot(100*X_estNT(1,:)',100*X_estNT(2,:)','g-.');
+
+    %X-Position
+    subplot(4,3,8);
+    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
+    h2=plot(time,100*X_est(1,:)','b-.');
+    h3=plot(time,100*X_estNT(1,:)','g-.');
+
+    %Y-Position
+    subplot(4,3,9);
+    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
+    h2=plot(time,100*X_est(2,:)','b-.');
+    h3=plot(time,100*X_estNT(2,:)','g-.');
 
-    %Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(2,:)','b-.'); 
-    h3=plot(time,100*X_estNT(2,:)','g-.'); 
+    %X-Velocity
+    subplot(4,3,11);
+    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
+    h2=plot(time,100*X_est(3,:)','b-.');
+    h3=plot(time,100*X_estNT(3,:)','g-.');
+
+    subplot(4,3,12);
+    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
+    h2=plot(time,100*X_est(4,:)','b-.');
+    h3=plot(time,100*X_estNT(4,:)','g-.');
 
-    %X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(3,:)','b-.');
-    h3=plot(time,100*X_estNT(3,:)','g-.');
 
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,100*X_est(4,:)','b-.'); 
-    h3=plot(time,100*X_estNT(4,:)','g-.');
 
-    
-    
 
-end
+end
 
-%
-%     Save all the example Data
-%     save Experiment6ReachExamples X_estAll X_estNTAll S_estAll ...
-%           S_estNTAll MU_estAll MU_estNTAll;
-%  
-%     load Experiment6ReachExamples;
+%
+%     Save all the example Data
+%     save Experiment6ReachExamples X_estAll X_estNTAll S_estAll ...
+%           S_estNTAll MU_estAll MU_estNTAll;
+%
+%     load Experiment6ReachExamples;
 
-    % Mean Discrete State Estimate
+    % Mean Discrete State Estimate
     subplot(4,3,[1 4]);
-    hold all; 
-    plot(time,mstate,'k','LineWidth',3); 
-    plot(time,mean(S_estAll),'b','LineWidth',3); 
-    plot(time,mean(S_estNTAll),'g','LineWidth',3); 
-    set(gca,'xtick',[],'YTick',[1 2.1],'YTickLabel',{'N','M'});
-    hy=ylabel('state'); hx=xlabel('time [s]');
-    set([hy hx],'FontName', 'Arial','FontSize',10,'FontWeight','bold',...
-        'Interpreter','none');
-    title('Estimated vs. Actual State','FontWeight','bold','Fontsize',...
-        12,'FontName','Arial');
+    hold all;
+    plot(time,mstate,'k','LineWidth',3);
+    plot(time,mean(S_estAll),'b','LineWidth',3);
+    plot(time,mean(S_estNTAll),'g','LineWidth',3);
+    set(gca,'xtick',[],'YTick',[1 2.1],'YTickLabel',{'N','M'});
+    hy=ylabel('state'); hx=xlabel('time [s]');
+    set([hy hx],'FontName', 'Arial','FontSize',10,'FontWeight','bold',...
+        'Interpreter','none');
+    title('Estimated vs. Actual State','FontWeight','bold','Fontsize',...
+        12,'FontName','Arial');
+
+
 
-    
-    
 
-   % Mean State Movement State Probability
+   % Mean State Movement State Probability
     subplot(4,3,[7 10]);
-    plot(time, mean(squeeze(MU_estAll(2,:,:)),2),'b','LineWidth',3);  
-    hold on;
-    plot(time,mean(squeeze(MU_estNTAll(2,:,:)),2),'g','LineWidth',3);  
-    hold on;
+    plot(time, mean(squeeze(MU_estAll(2,:,:)),2),'b','LineWidth',3);
+    hold on;
+    plot(time,mean(squeeze(MU_estNTAll(2,:,:)),2),'g','LineWidth',3);
+    hold on;
     axis([min(time) max(time) 0 1.1]);
-    hx=xlabel('time [s]'); hy=ylabel('P(s(t)=M | data)');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Probability of State','FontWeight','bold','Fontsize',12,...
-        'FontName','Arial');
-    
-    % Mean movement path
+    hx=xlabel('time [s]'); hy=ylabel('P(s(t)=M | data)');
+    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
+    title('Probability of State','FontWeight','bold','Fontsize',12,...
+        'FontName','Arial');
+
+    % Mean movement path
     subplot(4,3,[2 3 5 6]);
-    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
+    h1=plot(100*X(1,:)',100*X(2,:)','k'); hold all;
     mXestAll=mean(100*X_estAll,3);
     mXestNTAll=mean(100*X_estNTAll,3);
-    plot(mXestAll(1,:),mXestAll(2,:),'b','Linewidth',3);
-    plot(mXestNTAll(1,:),mXestNTAll(2,:),'g','Linewidth',3);
-    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
+    plot(mXestAll(1,:),mXestAll(2,:),'b','Linewidth',3);
+    plot(mXestNTAll(1,:),mXestNTAll(2,:),'g','Linewidth',3);
+    hx=xlabel('x [cm]'); hy=ylabel('y [cm]');
+    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
 
-    h1=plot(100*X(1,1),100*X(2,1),'bo','MarkerSize',14); hold on;
-    h2=plot(100*X(1,end),100*X(2,end),'ro','MarkerSize',14); 
-    legend([h1 h2],'Start','Finish','Location','NorthEast');
-    title('Estimated vs. Actual Reach Path','FontWeight','bold',...
-        'Fontsize',12,'FontName','Arial');
+    h1=plot(100*X(1,1),100*X(2,1),'bo','MarkerSize',14); hold on;
+    h2=plot(100*X(1,end),100*X(2,end),'ro','MarkerSize',14);
+    legend([h1 h2],'Start','Finish','Location','NorthEast');
+    title('Estimated vs. Actual Reach Path','FontWeight','bold',...
+        'Fontsize',12,'FontName','Arial');
 
-   
-    % Mean X-Positon
-    subplot(4,3,8); 
-    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
-    h2=plot(time,mXestAll(1,:),'b','LineWidth',3); hold on;
-    h3=plot(time,mXestNTAll(1,:),'g','LineWidth',3); hold on;
-    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    
-    % Mean Y-Position
-    subplot(4,3,9); 
-    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
-    h2=plot(time,mXestAll(2,:),'b','LineWidth',3); hold on;
-    h3=plot(time,mXestNTAll(2,:),'g','LineWidth',3); hold on;
-    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
-        'PPAF','Location','SouthEast');
-    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]');
-    set(gca,'xtick',[],'xtickLabel',[]);
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
-    set(h_legend,'FontSize',10)
-    pos = get(h_legend,'position');
-    set(h_legend, 'position',[pos(1)-.40 pos(2)+.51 pos(3:4)]);
 
-    % Mean X-Velocity
-    subplot(4,3,11); 
-    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
-    h2=plot(time,mXestAll(3,:),'b','LineWidth',3); hold on;
-    h3=plot(time,mXestNTAll(3,:),'g','LineWidth',3); hold on;
-    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
+    % Mean X-Positon
+    subplot(4,3,8);
+    h1=plot(time,100*X(1,:),'k','LineWidth',3); hold on;
+    h2=plot(time,mXestAll(1,:),'b','LineWidth',3); hold on;
+    h3=plot(time,mXestNTAll(1,:),'g','LineWidth',3); hold on;
+    hy=ylabel('x(t) [cm]'); hx=xlabel('time [s]');
+    set(gca,'xtick',[],'xtickLabel',[]);
+    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
+    title('X Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
 
-    % Mean Y-Velocity
-    subplot(4,3,12); 
-    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
-    h2=plot(time,mXestAll(4,:),'b','LineWidth',3); hold on;
-    h3=plot(time,mXestNTAll(4,:),'g','LineWidth',3); hold on;
-    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
-    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
-    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
+    % Mean Y-Position
+    subplot(4,3,9);
+    h1=plot(time,100*X(2,:),'k','LineWidth',3); hold on;
+    h2=plot(time,mXestAll(2,:),'b','LineWidth',3); hold on;
+    h3=plot(time,mXestNTAll(2,:),'g','LineWidth',3); hold on;
+    h_legend=legend([h1(1) h2(1) h3(1)],'Actual','PPAF+Goal',...
+        'PPAF','Location','SouthEast');
+    hy=ylabel('y(t) [cm]'); hx=xlabel('time [s]');
+    set(gca,'xtick',[],'xtickLabel',[]);
+    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
+    title('Y Position','FontWeight','bold','Fontsize',12,'FontName','Arial');
+    set(h_legend,'FontSize',10)
+    pos = get(h_legend,'position');
+    set(h_legend, 'position',[pos(1)-.40 pos(2)+.51 pos(3:4)]);
+
+    % Mean X-Velocity
+    subplot(4,3,11);
+    h1=plot(time,100*X(3,:),'k','LineWidth',3); hold on;
+    h2=plot(time,mXestAll(3,:),'b','LineWidth',3); hold on;
+    h3=plot(time,mXestNTAll(3,:),'g','LineWidth',3); hold on;
+    hy=ylabel('v_{x}(t) [cm/s]'); hx=xlabel('time [s]');
+    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
+    title('X Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
+
+    % Mean Y-Velocity
+    subplot(4,3,12);
+    h1=plot(time,100*X(4,:),'k','LineWidth',3); hold on;
+    h2=plot(time,mXestAll(4,:),'b','LineWidth',3); hold on;
+    h3=plot(time,mXestNTAll(4,:),'g','LineWidth',3); hold on;
+    hy=ylabel('v_{y}(t) [cm/s]'); hx=xlabel('time [s]');
+    set([hx, hy],'FontName', 'Arial','FontSize',10,'FontWeight','bold');
+    title('Y Velocity','FontWeight','bold','Fontsize',12,'FontName','Arial');
+
+% Parity contract scalars for MATLAB/Python verification.
+parity = struct();
+if exist('numCells','var')
+    parity.num_cells = numCells;
+end
+if exist('numRealizations','var')
+    parity.num_realizations = numRealizations;
+end
+
+function [dataDir,mEPSCDir,explicitStimulusDir,psthDir,placeCellDataDir] = ...
+    getPaperDataDirs()
+% Resolve local data folders robustly when Live Editor executes from a temp
+% location (e.g., /private/var/.../T).
+candidateRoots = {};
+
+scriptPath = mfilename('fullpath');
+if ~isempty(scriptPath)
+    candidateRoots = appendCandidateRoot(candidateRoots, fileparts(fileparts(scriptPath)));
+end
+
+paperPath = which('nSTATPaperExamples');
+if ~isempty(paperPath)
+    candidateRoots = appendCandidateRoot(candidateRoots, fileparts(fileparts(paperPath)));
+end
+
+installPath = which('nSTAT_Install');
+if ~isempty(installPath)
+    candidateRoots = appendCandidateRoot(candidateRoots, fileparts(installPath));
+end
+
+try
+    activeFile = matlab.desktop.editor.getActiveFilename;
+catch
+    activeFile = '';
+end
+if ~isempty(activeFile)
+    candidateRoots = appendCandidateRoot(candidateRoots, fileparts(fileparts(activeFile)));
+end
+
+candidateRoots = appendCandidateRoot(candidateRoots, pwd);
+
+nSTATDir = '';
+for iRoot = 1:numel(candidateRoots)
+    candidateDataDir = fullfile(candidateRoots{iRoot}, 'data');
+    if exist(candidateDataDir, 'dir') == 7
+        nSTATDir = candidateRoots{iRoot};
+        break;
+    end
+end
+
+if isempty(nSTATDir)
+    error('nSTATPaperExamples:MissingInstallPath', ...
+        ['Could not resolve the nSTAT root path. Checked roots derived from ', ...
+         'mfilename, which(''nSTATPaperExamples''), which(''nSTAT_Install''), ', ...
+         'the active editor file, and pwd.']);
+end
+
+dataDir = fullfile(nSTATDir,'data');
+mEPSCDir = fullfile(dataDir,'mEPSCs');
+explicitStimulusDir = fullfile(dataDir,'Explicit Stimulus');
+psthDir = fullfile(dataDir,'PSTH');
+placeCellDataDir = fullfile(dataDir,'Place Cells');
+
+if exist(dataDir,'dir') ~= 7
+    error('nSTATPaperExamples:MissingDataDir', ...
+        'Could not find local nSTAT data folder at %s', dataDir);
+end
+end
+
+function roots = appendCandidateRoot(roots, startDir)
+if isempty(startDir)
+    return;
+end
 
-
\ No newline at end of file +--> + + diff --git a/helpfiles/nSTATPaperExamples.m b/helpfiles/nSTATPaperExamples.m index 0b37162..68590a1 100644 --- a/helpfiles/nSTATPaperExamples.m +++ b/helpfiles/nSTATPaperExamples.m @@ -1,8 +1,24 @@ -%% nSTAT J. Neuroscience Methods Paper Examples -% -% *Author*: Iahn Cajigas -% -% *Date*: 01/04/2012 +%% nSTAT J. Neuroscience Methods Paper Examples +% +% *Author*: Iahn Cajigas +% +% *Date*: 01/04/2012 +% +% Force command echo off so published output does not include repeated +% executed source lines. +echo off; +% +% Paper reference: +% +% * Cajigas I, Malik WQ, Brown EN. nSTAT: Open-source neural spike train +% analysis toolbox for Matlab. Journal of Neuroscience Methods 211: +% 245-264 (2012). +% * DOI: 10.1016/j.jneumeth.2012.08.009 +% * PMID: 22981419 +% +% Navigation: +% +% * %% Experiment 1 @@ -939,9 +955,10 @@ -%% Compare differences across trials -close all; - minTime=0; maxTime = Tmax; +%% Compare differences across trials +echo off; +close all; + minTime=0; maxTime = Tmax; % Generate the basis function so that the estimated effect can be plotted % at the same temporal resolution as the theoretical effect if(~isempty(numBasis)) diff --git a/helpfiles/nSTATPaperExamples.png b/helpfiles/nSTATPaperExamples.png index 43b3d5b..c863df9 100644 Binary files a/helpfiles/nSTATPaperExamples.png and b/helpfiles/nSTATPaperExamples.png differ diff --git a/helpfiles/nSTATPaperExamples_01.png b/helpfiles/nSTATPaperExamples_01.png index 3a25dba..f393ce6 100644 Binary files a/helpfiles/nSTATPaperExamples_01.png and b/helpfiles/nSTATPaperExamples_01.png differ diff --git a/helpfiles/nSTATPaperExamples_02.png b/helpfiles/nSTATPaperExamples_02.png index 4f9f4ed..300219e 100644 Binary files a/helpfiles/nSTATPaperExamples_02.png and b/helpfiles/nSTATPaperExamples_02.png differ diff --git a/helpfiles/nSTATPaperExamples_03.png b/helpfiles/nSTATPaperExamples_03.png index 4303f38..0add29c 100644 Binary files a/helpfiles/nSTATPaperExamples_03.png and b/helpfiles/nSTATPaperExamples_03.png differ diff --git a/helpfiles/nSTATPaperExamples_04.png b/helpfiles/nSTATPaperExamples_04.png index 66fa978..13635fe 100644 Binary files a/helpfiles/nSTATPaperExamples_04.png and b/helpfiles/nSTATPaperExamples_04.png differ diff --git a/helpfiles/nSTATPaperExamples_05.png b/helpfiles/nSTATPaperExamples_05.png index ec52a27..11f23ca 100644 Binary files a/helpfiles/nSTATPaperExamples_05.png and b/helpfiles/nSTATPaperExamples_05.png differ diff --git a/helpfiles/nSTATPaperExamples_06.png b/helpfiles/nSTATPaperExamples_06.png index 0f9625c..33a4e07 100644 Binary files a/helpfiles/nSTATPaperExamples_06.png and b/helpfiles/nSTATPaperExamples_06.png differ diff --git a/helpfiles/nSTATPaperExamples_07.png b/helpfiles/nSTATPaperExamples_07.png index b708887..e624b3b 100644 Binary files a/helpfiles/nSTATPaperExamples_07.png and b/helpfiles/nSTATPaperExamples_07.png differ diff --git a/helpfiles/nSTATPaperExamples_08.png b/helpfiles/nSTATPaperExamples_08.png index e5b425f..761cfb3 100644 Binary files a/helpfiles/nSTATPaperExamples_08.png and b/helpfiles/nSTATPaperExamples_08.png differ diff --git a/helpfiles/nSTATPaperExamples_09.png b/helpfiles/nSTATPaperExamples_09.png index 54ecc01..26096fb 100644 Binary files a/helpfiles/nSTATPaperExamples_09.png and b/helpfiles/nSTATPaperExamples_09.png differ diff --git a/helpfiles/nSTATPaperExamples_10.png b/helpfiles/nSTATPaperExamples_10.png index 80dc058..d020b26 100644 Binary files a/helpfiles/nSTATPaperExamples_10.png and b/helpfiles/nSTATPaperExamples_10.png differ diff --git a/helpfiles/nSTATPaperExamples_11.png b/helpfiles/nSTATPaperExamples_11.png index 1a23b37..d9836a5 100644 Binary files a/helpfiles/nSTATPaperExamples_11.png and b/helpfiles/nSTATPaperExamples_11.png differ diff --git a/helpfiles/nSTATPaperExamples_12.png b/helpfiles/nSTATPaperExamples_12.png index 5b13ce9..ca8ebcf 100644 Binary files a/helpfiles/nSTATPaperExamples_12.png and b/helpfiles/nSTATPaperExamples_12.png differ diff --git a/helpfiles/nSTATPaperExamples_13.png b/helpfiles/nSTATPaperExamples_13.png index c28e8f9..1920fc5 100644 Binary files a/helpfiles/nSTATPaperExamples_13.png and b/helpfiles/nSTATPaperExamples_13.png differ diff --git a/helpfiles/nSTATPaperExamples_14.png b/helpfiles/nSTATPaperExamples_14.png index 2b788cd..34ae43d 100644 Binary files a/helpfiles/nSTATPaperExamples_14.png and b/helpfiles/nSTATPaperExamples_14.png differ diff --git a/helpfiles/nSTATPaperExamples_15.png b/helpfiles/nSTATPaperExamples_15.png index f4b84a0..573dd23 100644 Binary files a/helpfiles/nSTATPaperExamples_15.png and b/helpfiles/nSTATPaperExamples_15.png differ diff --git a/helpfiles/nSTATPaperExamples_16.png b/helpfiles/nSTATPaperExamples_16.png index 7030c24..6c781f9 100644 Binary files a/helpfiles/nSTATPaperExamples_16.png and b/helpfiles/nSTATPaperExamples_16.png differ diff --git a/helpfiles/nSTATPaperExamples_17.png b/helpfiles/nSTATPaperExamples_17.png index 809e6ef..8cab6ea 100644 Binary files a/helpfiles/nSTATPaperExamples_17.png and b/helpfiles/nSTATPaperExamples_17.png differ diff --git a/helpfiles/nSTATPaperExamples_18.png b/helpfiles/nSTATPaperExamples_18.png index 4b394c7..58ca631 100644 Binary files a/helpfiles/nSTATPaperExamples_18.png and b/helpfiles/nSTATPaperExamples_18.png differ diff --git a/helpfiles/nSTATPaperExamples_19.png b/helpfiles/nSTATPaperExamples_19.png index e2597d3..5fbd90d 100644 Binary files a/helpfiles/nSTATPaperExamples_19.png and b/helpfiles/nSTATPaperExamples_19.png differ diff --git a/helpfiles/nSTATPaperExamples_20.png b/helpfiles/nSTATPaperExamples_20.png index a089a7c..2c7b49b 100644 Binary files a/helpfiles/nSTATPaperExamples_20.png and b/helpfiles/nSTATPaperExamples_20.png differ diff --git a/helpfiles/nSTATPaperExamples_21.png b/helpfiles/nSTATPaperExamples_21.png index 6a752b4..8f9ccc8 100644 Binary files a/helpfiles/nSTATPaperExamples_21.png and b/helpfiles/nSTATPaperExamples_21.png differ diff --git a/helpfiles/nSTATPaperExamples_22.png b/helpfiles/nSTATPaperExamples_22.png index 1f6ba5c..0c5dd46 100644 Binary files a/helpfiles/nSTATPaperExamples_22.png and b/helpfiles/nSTATPaperExamples_22.png differ diff --git a/helpfiles/nSTATPaperExamples_23.png b/helpfiles/nSTATPaperExamples_23.png index 5b8683e..500105a 100644 Binary files a/helpfiles/nSTATPaperExamples_23.png and b/helpfiles/nSTATPaperExamples_23.png differ diff --git a/helpfiles/nSTATPaperExamples_24.png b/helpfiles/nSTATPaperExamples_24.png index 1839ad0..5be88ae 100644 Binary files a/helpfiles/nSTATPaperExamples_24.png and b/helpfiles/nSTATPaperExamples_24.png differ diff --git a/helpfiles/nSTATPaperExamples_25.png b/helpfiles/nSTATPaperExamples_25.png index feb334d..db17bae 100644 Binary files a/helpfiles/nSTATPaperExamples_25.png and b/helpfiles/nSTATPaperExamples_25.png differ diff --git a/helpfiles/nSTATPaperExamples_eq12496553100641641814.png b/helpfiles/nSTATPaperExamples_eq12496553100641641814.png new file mode 100644 index 0000000..d5279de Binary files /dev/null and b/helpfiles/nSTATPaperExamples_eq12496553100641641814.png differ diff --git a/helpfiles/nSpikeTrainExamples.html b/helpfiles/nSpikeTrainExamples.html index b965d34..5cb888d 100644 --- a/helpfiles/nSpikeTrainExamples.html +++ b/helpfiles/nSpikeTrainExamples.html @@ -1,13 +1,18 @@ - - - - - Test the nspikeTrain Class

Test the nspikeTrain Class

Contents

Example 1: Using the nspikeTrain Class

Lets create some pseudo data and plot it.

spikeTimes = sort(rand(1,100))*1;
+  
+
+
+
+

Test the nspikeTrain Class

+ + +

Contents

+ +

Example 1: Using the nspikeTrain Class

+

Lets create some pseudo data and plot it.

+
spikeTimes = sort(rand(1,100))*1;
 spikeTimes = unique(round(spikeTimes*10000)./10000); %round off;
 nst=nspikeTrain(spikeTimes,'n1',.001,0,1);
 figure; nst.plot;
-

We can now change the signal representation of the nspikeTrain and see what effects it has.

100ms bins from 0 to 10 sec. Actual SignalObj representation of the nspikeTrain is not changed because are using getSigRep

figure; nst.resample(1/.1);
+
+

We can now change the signal representation of the nspikeTrain and see what effects it has.

+

100ms bins from 0 to 10 sec. Actual SignalObj representation of the nspikeTrain is not changed because are using getSigRep

+
figure; nst.resample(1/.1);
 nst.getSigRep.plot;
-

10ms bins from 0 to 10 sec. Actually changing the representation of the signal.

figure; nst.resample(1/.01);
+
+

10ms bins from 0 to 10 sec. Actually changing the representation of the signal.

+
figure; nst.resample(1/.01);
 nst.getSigRep.plot;
-

Get the largest binsize that still maintains a binary signal representation

figure; nst.resample(1/nst.getMaxBinSizeBinary);
+
+

Get the largest binsize that still maintains a binary signal representation

+
figure; nst.resample(1/nst.getMaxBinSizeBinary);
 nst.getSigRep.plot;
-
\ No newline at end of file +--> + + diff --git a/helpfiles/nSpikeTrainExamples.png b/helpfiles/nSpikeTrainExamples.png index 14473e8..37c9cbb 100644 Binary files a/helpfiles/nSpikeTrainExamples.png and b/helpfiles/nSpikeTrainExamples.png differ diff --git a/helpfiles/nSpikeTrainExamples_01.png b/helpfiles/nSpikeTrainExamples_01.png index c925f3f..99471a3 100644 Binary files a/helpfiles/nSpikeTrainExamples_01.png and b/helpfiles/nSpikeTrainExamples_01.png differ diff --git a/helpfiles/nSpikeTrainExamples_02.png b/helpfiles/nSpikeTrainExamples_02.png index 1cd324d..a579f85 100644 Binary files a/helpfiles/nSpikeTrainExamples_02.png and b/helpfiles/nSpikeTrainExamples_02.png differ diff --git a/helpfiles/nSpikeTrainExamples_03.png b/helpfiles/nSpikeTrainExamples_03.png index 3cc685a..cbe6706 100644 Binary files a/helpfiles/nSpikeTrainExamples_03.png and b/helpfiles/nSpikeTrainExamples_03.png differ diff --git a/helpfiles/nSpikeTrainExamples_04.png b/helpfiles/nSpikeTrainExamples_04.png index 68b0536..7a6f26b 100644 Binary files a/helpfiles/nSpikeTrainExamples_04.png and b/helpfiles/nSpikeTrainExamples_04.png differ diff --git a/helpfiles/nSpikeTrainExamples_05.png b/helpfiles/nSpikeTrainExamples_05.png new file mode 100644 index 0000000..c8f1d6f Binary files /dev/null and b/helpfiles/nSpikeTrainExamples_05.png differ diff --git a/helpfiles/nstCollExamples.html b/helpfiles/nstCollExamples.html index 70f89e4..a7ff4b2 100644 --- a/helpfiles/nstCollExamples.html +++ b/helpfiles/nstCollExamples.html @@ -1,13 +1,18 @@ - - - - - nstCollExamples

Contents

Test the nstColl Class

Create pseudo spike data and create a neural spike collection (nstColl).

close all; clear all;
+  
+
+
+
+

+ + +

Contents

+ +

Test the nstColl Class

+

Create pseudo spike data and create a neural spike collection (nstColl).

+
close all; clear all;
 for i=1:20
     spikeTimes = sort(rand(1,100))*1;
     nst{i}=nspikeTrain(spikeTimes,'',.1);
     nst{i}.setName(strcat('Neuron',num2str(i)));
 end
 spikeColl=nstColl(nst);
-

Plot the entire collection at once

figure; spikeColl.plot;
-

allow only nspikeTrains 1, 4, and 7 to be visible

spikeColl.setMask([1 4 7]);
+
+

Plot the entire collection at once

+
figure; spikeColl.plot;
+
+

allow only nspikeTrains 1, 4, and 7 to be visible

+
spikeColl.setMask([1 4 7]);
 figure; spikeColl.plot;
-

It is possible to obtain nspikeTrains from the collection;

figure;
+
+

It is possible to obtain nspikeTrains from the collection;

+
figure;
 n1=spikeColl.getNST(1);     %get the first nspikeTrain in the collection
 subplot(3,1,1); n1.plot;
 subplot(3,1,2); n1.getSigRep.plot; %plot current sigRep
@@ -83,7 +113,14 @@
 % get a SignalObj representation 1ms bins from 0 to 10 sec
 s1=n1.getSigRep(.001,0,1);
 subplot(3,1,3); s1.plot;
-
\ No newline at end of file +--> + + diff --git a/helpfiles/nstCollExamples.png b/helpfiles/nstCollExamples.png index f5e0166..7a255bc 100644 Binary files a/helpfiles/nstCollExamples.png and b/helpfiles/nstCollExamples.png differ diff --git a/helpfiles/nstCollExamples_01.png b/helpfiles/nstCollExamples_01.png index 02a10c6..bd1e626 100644 Binary files a/helpfiles/nstCollExamples_01.png and b/helpfiles/nstCollExamples_01.png differ diff --git a/helpfiles/nstCollExamples_02.png b/helpfiles/nstCollExamples_02.png index d6ee579..310be4b 100644 Binary files a/helpfiles/nstCollExamples_02.png and b/helpfiles/nstCollExamples_02.png differ diff --git a/helpfiles/nstCollExamples_03.png b/helpfiles/nstCollExamples_03.png index f413e5a..b8ea991 100644 Binary files a/helpfiles/nstCollExamples_03.png and b/helpfiles/nstCollExamples_03.png differ diff --git a/helpfiles/publish_all_helpfiles.m b/helpfiles/publish_all_helpfiles.m new file mode 100644 index 0000000..364e362 --- /dev/null +++ b/helpfiles/publish_all_helpfiles.m @@ -0,0 +1,151 @@ +function publish_all_helpfiles(varargin) +% publish_all_helpfiles Deterministically republish all nSTAT help HTML. +% +% This script stages help source files to avoid .mlx shadowing during +% publish, regenerates HTML with evalCode enabled, and refreshes MATLAB help +% search metadata for the toolbox. + +opts = parseOptions(varargin{:}); + +helpDir = fileparts(mfilename('fullpath')); +rootDir = fileparts(helpDir); +stagingDir = tempname; +outputDir = tempname; +mkdir(stagingDir); +mkdir(outputDir); +cleanupObj = onCleanup(@()cleanupTempDirs(stagingDir, outputDir)); +startDir = pwd; +restoreDir = onCleanup(@()cd(startDir)); %#ok + +copyfile(fullfile(helpDir, '*'), stagingDir); +removeStagedArtifacts(stagingDir); + +restoredefaultpath; +addpath(rootDir, '-begin'); +nSTAT_Install('RebuildDocSearch', false, 'CleanUserPathPrefs', false); +addpath(stagingDir, '-begin'); +cd(stagingDir); + +publishOptions = struct('outputDir', outputDir, 'format', 'html', 'evalCode', opts.EvalCode); +referencePublishOptions = struct('outputDir', outputDir, 'format', 'html', 'evalCode', false); +failures = {}; + +stageFiles = dir(fullfile(stagingDir, '*.m')); +for iFile = 1:numel(stageFiles) + [~, baseName] = fileparts(stageFiles(iFile).name); + if strcmpi(baseName, 'publish_all_helpfiles') + continue; + end + try + publish(baseName, publishOptions); + fprintf('Published help topic: %s\n', stageFiles(iFile).name); + catch ME + failures{end+1} = sprintf('%s :: %s', stageFiles(iFile).name, ME.message); %#ok + end +end + +rootReferenceFiles = {'Analysis.m', 'SignalObj.m', 'FitResult.m'}; +for iFile = 1:numel(rootReferenceFiles) + sourceFile = fullfile(rootDir, rootReferenceFiles{iFile}); + try + publish(sourceFile, referencePublishOptions); + fprintf('Published class reference: %s\n', rootReferenceFiles{iFile}); + catch ME + failures{end+1} = sprintf('%s :: %s', rootReferenceFiles{iFile}, ME.message); %#ok + end +end + +if ~isempty(failures) + fprintf(2, 'Publish failures (%d):\n', numel(failures)); + for i = 1:numel(failures) + fprintf(2, ' - %s\n', failures{i}); + end + error('nSTAT:PublishAllFailures', 'One or more help pages failed to publish.'); +end + +copyfile(fullfile(outputDir, '*'), helpDir, 'f'); + +builddocsearchdb(helpDir); +rehash toolboxcache; + +validateHelpTargets(helpDir); +validateHtmlGeneratorMetadata(helpDir, opts.ExpectedGenerator); + +fprintf('nSTAT help publication completed successfully.\n'); +clear cleanupObj; +end + +function opts = parseOptions(varargin) +parser = inputParser; +parser.FunctionName = 'publish_all_helpfiles'; +addParameter(parser, 'EvalCode', true, @(x)islogical(x) || isnumeric(x)); +addParameter(parser, 'ExpectedGenerator', 'MATLAB 25.2', @(x)ischar(x) || isstring(x)); +parse(parser, varargin{:}); + +opts.EvalCode = logical(parser.Results.EvalCode); +opts.ExpectedGenerator = char(parser.Results.ExpectedGenerator); +end + +function removeStagedArtifacts(stagingDir) +removePattern(stagingDir, '*.mlx'); +removePattern(stagingDir, '*.asv'); +removePattern(stagingDir, '*.bak'); +removePattern(stagingDir, 'temp.m'); +removePattern(stagingDir, 'publish_all_helpfiles.m'); +end + +function removePattern(stagingDir, pattern) +files = dir(fullfile(stagingDir, pattern)); +for i = 1:numel(files) + delete(fullfile(stagingDir, files(i).name)); +end +end + +function validateHelpTargets(helpDir) +helptocPath = fullfile(helpDir, 'helptoc.xml'); +if ~isfile(helptocPath) + error('nSTAT:MissingHelptoc', 'Missing helptoc.xml at %s', helptocPath); +end + +raw = fileread(helptocPath); +matches = regexp(raw, 'target="([^"]+)"', 'tokens'); +for i = 1:numel(matches) + target = matches{i}{1}; + if startsWith(target, 'http://') || startsWith(target, 'https://') + continue; + end + fullTarget = fullfile(helpDir, target); + if ~isfile(fullTarget) + error('nSTAT:MissingHelpTarget', ... + 'helptoc target is missing after publish: %s', fullTarget); + end +end +end + +function validateHtmlGeneratorMetadata(helpDir, expectedGenerator) +htmlFiles = dir(fullfile(helpDir, '*.html')); +for i = 1:numel(htmlFiles) + htmlPath = fullfile(helpDir, htmlFiles(i).name); + raw = fileread(htmlPath); + if isempty(regexp(raw, [' + continue; + end + if shouldExcludePath(relPath) + continue; + end + hasMatlabFiles = ~isempty(dir(fullfile(dirPath, '*.m'))); + hasClassFolders = ~isempty(dir(fullfile(dirPath, '@*'))); + hasPackageFolders = ~isempty(dir(fullfile(dirPath, '+*'))); + if hasMatlabFiles || hasClassFolders || hasPackageFolders + runtimePaths{end+1} = dirPath; %#ok + end +end +runtimePaths = unique(runtimePaths, 'stable'); +end + +function tf = shouldExcludePath(relPath) +if isempty(relPath) + tf = false; + return; +end +segments = strsplit(relPath, filesep); +segments = segments(~cellfun(@isempty, segments)); +if isempty(segments) + tf = false; + return; +end + +excludedExact = { ... + '.git', '.github', 'helpfiles', 'python', 'slprj', 'porting', ... + '__pycache__', '.pytest_cache', '.mypy_cache', '.vscode', '.idea'}; +tf = false; +for iSeg = 1:numel(segments) + seg = segments{iSeg}; + if startsWith(seg, '.') + tf = true; + return; + end + if any(strcmpi(seg, excludedExact)) + tf = true; + return; + end +end +end diff --git a/nspikeTrain.m b/nspikeTrain.m index b21268f..d2ef569 100644 --- a/nspikeTrain.m +++ b/nspikeTrain.m @@ -26,11 +26,11 @@ % methods % nSpikeTrain Examples % -% see also Covariate, SignalObj +% see also Covariate, SignalObj % % Reference page in Help browser -% doc nspikeTrain +% nspikeTrain Reference % diff --git a/nstColl.m b/nstColl.m index 45b6008..4bc2315 100644 --- a/nstColl.m +++ b/nstColl.m @@ -1,16 +1,16 @@ classdef nstColl < handle % NSTCOLL A collection of nspikeTrains % -% methods -% nstColl Examples -% -% see also CovColl, Covariate, SignalObj,nspikeTrain -% -% Reference page in Help browser -% doc nstColl +% methods +% nstColl Examples +% +% see also CovColl, Covariate, SignalObj,nspikeTrain +% +% Reference page in Help browser +% nstColl Reference % % nSTAT v1 Copyright (C) 2012 Masschusetts Institute of Technology diff --git a/porting/python_sync/README.md b/porting/python_sync/README.md new file mode 100644 index 0000000..6711796 --- /dev/null +++ b/porting/python_sync/README.md @@ -0,0 +1,56 @@ +# MATLAB to Python Sync Hub + +This directory is the canonical coordination point for keeping the Python +nSTAT implementation aligned with changes in the MATLAB toolbox. + +## Contents + +- `translation_map.yaml`: MATLAB-to-Python API parity map and status. +- `matlab_api_inventory.json`: inventory of public MATLAB classes/functions. +- `python_api_inventory.json`: inventory of Python APIs and adapters. +- `paper_example_mapping.yaml`: mapping between `helpfiles/nSTATPaperExamples.m` + sections and Python equivalents. +- `tools/compare_inventories.py`: parity report generator. +- `reports/`: generated comparison reports. + +## Update Workflow + +1. Scan MATLAB API surface. + +```bash +cd /Users/iahncajigas/Library/CloudStorage/Dropbox/Research/Matlab/nSTAT_currentRelease_Local +ls -1 *.m +``` + +2. Scan Python API surface. + +```bash +cd /Users/iahncajigas/Library/CloudStorage/Dropbox/Research/Matlab/nSTAT_currentRelease_Local +ls -1 python/nstat/*.py +``` + +3. Refresh inventories and translation map. + +- Update `matlab_api_inventory.json` for newly added/removed MATLAB files. +- Update `python_api_inventory.json` for newly added/removed Python modules. +- Update `translation_map.yaml` with parity status and notes. + +4. Run parity comparison and generate report. + +```bash +cd /Users/iahncajigas/Library/CloudStorage/Dropbox/Research/Matlab/nSTAT_currentRelease_Local +python3 porting/python_sync/tools/compare_inventories.py +``` + +5. Update paper-example parity table. + +- Edit `paper_example_mapping.yaml` when `helpfiles/nSTATPaperExamples.m` or + Python paper workflows change. +- Confirm each MATLAB paper section has a documented Python status + (`mapped`, `partial`, `missing`, or `not_applicable`). + +## Output Expectations + +- `reports/latest_comparison_report.json` should be regenerated whenever API + inventories change. +- `translation_map.yaml` should include every public MATLAB root `.m` file. diff --git a/porting/python_sync/matlab_api_inventory.json b/porting/python_sync/matlab_api_inventory.json new file mode 100644 index 0000000..00b8efa --- /dev/null +++ b/porting/python_sync/matlab_api_inventory.json @@ -0,0 +1,25 @@ +{ + "generated_on": "2026-02-27", + "repo_root": "/Users/iahncajigas/Library/CloudStorage/Dropbox/Research/Matlab/nSTAT_currentRelease_Local", + "public_api": [ + {"name": "Analysis", "kind": "class", "path": "Analysis.m"}, + {"name": "CIF", "kind": "class", "path": "CIF.m"}, + {"name": "ConfidenceInterval", "kind": "class", "path": "ConfidenceInterval.m"}, + {"name": "ConfigColl", "kind": "class", "path": "ConfigColl.m"}, + {"name": "CovColl", "kind": "class", "path": "CovColl.m"}, + {"name": "Covariate", "kind": "class", "path": "Covariate.m"}, + {"name": "DecodingAlgorithms", "kind": "class", "path": "DecodingAlgorithms.m"}, + {"name": "Events", "kind": "class", "path": "Events.m"}, + {"name": "FitResSummary", "kind": "class", "path": "FitResSummary.m"}, + {"name": "FitResult", "kind": "class", "path": "FitResult.m"}, + {"name": "History", "kind": "class", "path": "History.m"}, + {"name": "SignalObj", "kind": "class", "path": "SignalObj.m"}, + {"name": "Trial", "kind": "class", "path": "Trial.m"}, + {"name": "TrialConfig", "kind": "class", "path": "TrialConfig.m"}, + {"name": "nspikeTrain", "kind": "class", "path": "nspikeTrain.m"}, + {"name": "nstColl", "kind": "class", "path": "nstColl.m"}, + {"name": "getPaperDataDirs", "kind": "function", "path": "getPaperDataDirs.m"}, + {"name": "nSTAT_Install", "kind": "function", "path": "nSTAT_Install.m"}, + {"name": "nstatOpenHelpPage", "kind": "function", "path": "nstatOpenHelpPage.m"} + ] +} diff --git a/porting/python_sync/paper_example_mapping.yaml b/porting/python_sync/paper_example_mapping.yaml new file mode 100644 index 0000000..56e1d25 --- /dev/null +++ b/porting/python_sync/paper_example_mapping.yaml @@ -0,0 +1,44 @@ +version: 1 +source_matlab: helpfiles/nSTATPaperExamples.m +generated_on: 2026-02-27 +examples: + - matlab_section: "Experiment 1 - mEPSC constant/piecewise baseline" + matlab_anchor: "%% Experiment 1" + python_target: "python/nstat/paper_examples_full.py" + status: partial + notes: Core concepts are present; full parity for all sub-figures remains partial. + - matlab_section: "Experiment 2 - Explicit stimulus whisker data" + matlab_anchor: "%% Experiment 2" + python_target: "python/nstat/paper_examples.py" + status: mapped + notes: Primary stimulus-driven GLM workflow implemented. + - matlab_section: "Example 3 - PSTH Data" + matlab_anchor: "%% Example 3 - PSTH Data" + python_target: "python/nstat/paper_examples.py" + status: mapped + notes: PSTH workflow represented in Python examples. + - matlab_section: "Example 3b - SSGLM Example" + matlab_anchor: "%% Example 3b - SSGLM Example" + python_target: "python/nstat/paper_examples_full.py" + status: partial + notes: Structured approximation exists; additional model details may differ. + - matlab_section: "Example 4 - Hippocampal place cell" + matlab_anchor: "%% Example 4 - HIPPOCAMPAL PLACE CELL - RECEPTIVE FIELD ESTIMATION" + python_target: "python/nstat/paper_examples_full.py" + status: partial + notes: Data loading and receptive-field-oriented workflow exist with reduced scope. + - matlab_section: "Example 5 - Stimulus decoding" + matlab_anchor: "%% Example 5 - STIMULUS DECODING" + python_target: "python/nstat/paper_examples.py" + status: mapped + notes: Univariate and bivariate decoding primitives are available. + - matlab_section: "Example 5b - Arm reaching simulation" + matlab_anchor: "%% Example 5b - Arm reaching to target Simulation" + python_target: "python/nstat/paper_examples_full.py" + status: partial + notes: Full simulation parity is still in progress. + - matlab_section: "Experiment 6 - Hybrid point process filter" + matlab_anchor: "%% Experiment 6 - Hybrid Point Process Filter Example" + python_target: "python/nstat/paper_examples_full.py" + status: partial + notes: Hybrid filtering support is available but not fully feature-matched. diff --git a/porting/python_sync/python_api_inventory.json b/porting/python_sync/python_api_inventory.json new file mode 100644 index 0000000..bbd4d89 --- /dev/null +++ b/porting/python_sync/python_api_inventory.json @@ -0,0 +1,38 @@ +{ + "generated_on": "2026-02-27", + "package_root": "python/nstat", + "canonical_modules": [ + {"name": "nstat.analysis", "path": "python/nstat/analysis.py"}, + {"name": "nstat.cif", "path": "python/nstat/cif.py"}, + {"name": "nstat.confidence_interval", "path": "python/nstat/confidence_interval.py"}, + {"name": "nstat.core", "path": "python/nstat/core.py"}, + {"name": "nstat.datasets", "path": "python/nstat/datasets.py"}, + {"name": "nstat.decoding", "path": "python/nstat/decoding.py"}, + {"name": "nstat.decoding_algorithms", "path": "python/nstat/decoding_algorithms.py"}, + {"name": "nstat.events", "path": "python/nstat/events.py"}, + {"name": "nstat.fit", "path": "python/nstat/fit.py"}, + {"name": "nstat.glm", "path": "python/nstat/glm.py"}, + {"name": "nstat.history", "path": "python/nstat/history.py"}, + {"name": "nstat.nstat_install", "path": "python/nstat/nstat_install.py"}, + {"name": "nstat.paper_examples", "path": "python/nstat/paper_examples.py"}, + {"name": "nstat.paper_examples_full", "path": "python/nstat/paper_examples_full.py"}, + {"name": "nstat.signal", "path": "python/nstat/signal.py"}, + {"name": "nstat.simulation", "path": "python/nstat/simulation.py"}, + {"name": "nstat.simulators", "path": "python/nstat/simulators.py"}, + {"name": "nstat.spikes", "path": "python/nstat/spikes.py"}, + {"name": "nstat.trial", "path": "python/nstat/trial.py"} + ], + "compatibility_adapters": [ + {"name": "nstat.SignalObj", "path": "python/nstat/SignalObj.py"}, + {"name": "nstat.Covariate", "path": "python/nstat/Covariate.py"}, + {"name": "nstat.CovColl", "path": "python/nstat/CovColl.py"}, + {"name": "nstat.ConfigColl", "path": "python/nstat/ConfigColl.py"}, + {"name": "nstat.ConfidenceInterval", "path": "python/nstat/ConfidenceInterval.py"}, + {"name": "nstat.DecodingAlgorithms", "path": "python/nstat/DecodingAlgorithms.py"}, + {"name": "nstat.FitResult", "path": "python/nstat/FitResult.py"}, + {"name": "nstat.FitResSummary", "path": "python/nstat/FitResSummary.py"}, + {"name": "nstat.TrialConfig", "path": "python/nstat/TrialConfig.py"}, + {"name": "nstat.nspikeTrain", "path": "python/nstat/nspikeTrain.py"}, + {"name": "nstat.nstColl", "path": "python/nstat/nstColl.py"} + ] +} diff --git a/porting/python_sync/reports/latest_comparison_report.json b/porting/python_sync/reports/latest_comparison_report.json new file mode 100644 index 0000000..05c837c --- /dev/null +++ b/porting/python_sync/reports/latest_comparison_report.json @@ -0,0 +1,32 @@ +{ + "generated_on": "2026-02-27", + "inventory_files": { + "matlab": "porting/python_sync/matlab_api_inventory.json", + "python": "porting/python_sync/python_api_inventory.json", + "translation_map": "porting/python_sync/translation_map.yaml" + }, + "counts": { + "matlab_public_api": 19, + "python_canonical_modules": 19, + "python_adapter_modules": 11, + "translation_entries": 19, + "missing_translation_entries": 0, + "missing_python_targets": 0, + "partial_entries": 1, + "not_applicable_entries": 1 + }, + "missing_translation_entries": [], + "missing_python_targets": [], + "partial_entries": [ + { + "matlab": "getPaperDataDirs.m", + "notes": "Similar data-root resolution exists but not as standalone public API." + } + ], + "not_applicable_entries": [ + { + "matlab": "nstatOpenHelpPage.m", + "notes": "MATLAB help-browser utility has no Python equivalent." + } + ] +} diff --git a/porting/python_sync/tools/compare_inventories.py b/porting/python_sync/tools/compare_inventories.py new file mode 100755 index 0000000..7d4761a --- /dev/null +++ b/porting/python_sync/tools/compare_inventories.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +"""Compare MATLAB and Python API inventories for nSTAT parity tracking.""" + +from __future__ import annotations + +import json +import re +from dataclasses import dataclass +from datetime import date +from pathlib import Path + + +@dataclass +class TranslationEntry: + matlab: str + python_module: str | None + status: str + notes: str + + +def load_json(path: Path) -> dict: + with path.open("r", encoding="utf-8") as f: + return json.load(f) + + +def load_translation_map(path: Path) -> list[TranslationEntry]: + entries: list[TranslationEntry] = [] + if not path.is_file(): + return entries + + current: dict[str, str | None] | None = None + for raw_line in path.read_text(encoding="utf-8").splitlines(): + line = raw_line.rstrip() + stripped = line.strip() + + if stripped.startswith("- matlab:"): + if current is not None: + entries.append( + TranslationEntry( + matlab=str(current.get("matlab", "")).strip(), + python_module=normalize_none(current.get("python_module")), + status=str(current.get("status", "")).strip(), + notes=str(current.get("notes", "")).strip(), + ) + ) + current = {"matlab": stripped.split(":", 1)[1].strip()} + continue + + if current is None: + continue + + key_match = re.match(r"^([a-zA-Z_]+):\s*(.*)$", stripped) + if key_match: + key = key_match.group(1) + value = key_match.group(2).strip() + current[key] = value + + if current is not None: + entries.append( + TranslationEntry( + matlab=str(current.get("matlab", "")).strip(), + python_module=normalize_none(current.get("python_module")), + status=str(current.get("status", "")).strip(), + notes=str(current.get("notes", "")).strip(), + ) + ) + + return entries + + +def normalize_none(value: str | None) -> str | None: + if value is None: + return None + cleaned = value.strip().strip("\"") + if cleaned.lower() in {"null", "none", ""}: + return None + return cleaned + + +def module_suffix(module_name: str) -> str: + suffix = module_name.split(".")[-1] + return suffix.lower() + + +def main() -> None: + repo_root = Path(__file__).resolve().parents[3] + sync_root = repo_root / "porting" / "python_sync" + + matlab_inventory_path = sync_root / "matlab_api_inventory.json" + python_inventory_path = sync_root / "python_api_inventory.json" + translation_path = sync_root / "translation_map.yaml" + report_path = sync_root / "reports" / "latest_comparison_report.json" + + matlab_inventory = load_json(matlab_inventory_path) + python_inventory = load_json(python_inventory_path) + translation_entries = load_translation_map(translation_path) + + matlab_names = [entry["name"] for entry in matlab_inventory.get("public_api", [])] + translation_index = {entry.matlab: entry for entry in translation_entries} + + canonical_modules = [entry["name"] for entry in python_inventory.get("canonical_modules", [])] + adapter_modules = [entry["name"] for entry in python_inventory.get("compatibility_adapters", [])] + python_suffixes = {module_suffix(name) for name in canonical_modules + adapter_modules} + + missing_translation = [name for name in matlab_names if f"{name}.m" not in translation_index] + + missing_python_targets: list[dict[str, str]] = [] + partial_entries: list[dict[str, str]] = [] + not_applicable_entries: list[dict[str, str]] = [] + + for entry in translation_entries: + if entry.status == "partial": + partial_entries.append({"matlab": entry.matlab, "notes": entry.notes}) + elif entry.status == "not_applicable": + not_applicable_entries.append({"matlab": entry.matlab, "notes": entry.notes}) + + if entry.status == "not_applicable": + continue + if entry.python_module is None: + missing_python_targets.append( + { + "matlab": entry.matlab, + "reason": "python_module is null", + "status": entry.status, + } + ) + continue + suffix = module_suffix(entry.python_module) + if suffix not in python_suffixes: + missing_python_targets.append( + { + "matlab": entry.matlab, + "reason": f"python module suffix '{suffix}' not in Python inventories", + "status": entry.status, + } + ) + + report = { + "generated_on": str(date.today()), + "inventory_files": { + "matlab": str(matlab_inventory_path.relative_to(repo_root)), + "python": str(python_inventory_path.relative_to(repo_root)), + "translation_map": str(translation_path.relative_to(repo_root)), + }, + "counts": { + "matlab_public_api": len(matlab_names), + "python_canonical_modules": len(canonical_modules), + "python_adapter_modules": len(adapter_modules), + "translation_entries": len(translation_entries), + "missing_translation_entries": len(missing_translation), + "missing_python_targets": len(missing_python_targets), + "partial_entries": len(partial_entries), + "not_applicable_entries": len(not_applicable_entries), + }, + "missing_translation_entries": missing_translation, + "missing_python_targets": missing_python_targets, + "partial_entries": partial_entries, + "not_applicable_entries": not_applicable_entries, + } + + report_path.parent.mkdir(parents=True, exist_ok=True) + report_path.write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8") + print(f"Wrote comparison report: {report_path}") + + +if __name__ == "__main__": + main() diff --git a/porting/python_sync/translation_map.yaml b/porting/python_sync/translation_map.yaml new file mode 100644 index 0000000..f8502e5 --- /dev/null +++ b/porting/python_sync/translation_map.yaml @@ -0,0 +1,117 @@ +version: 1 +generated_on: 2026-02-27 +entries: + - matlab: Analysis.m + matlab_path: Analysis.m + python_module: nstat.analysis + python_path: python/nstat/analysis.py + status: mapped + notes: Core analysis class is implemented. + - matlab: CIF.m + matlab_path: CIF.m + python_module: nstat.cif + python_path: python/nstat/cif.py + status: mapped + notes: CIFModel and CIF compatibility alias are available. + - matlab: ConfidenceInterval.m + matlab_path: ConfidenceInterval.m + python_module: nstat.confidence_interval + python_path: python/nstat/confidence_interval.py + status: mapped + notes: Confidence interval object implemented with compatibility adapter. + - matlab: ConfigColl.m + matlab_path: ConfigColl.m + python_module: nstat.trial + python_path: python/nstat/trial.py + status: mapped + notes: ConfigCollection plus ConfigColl adapter. + - matlab: CovColl.m + matlab_path: CovColl.m + python_module: nstat.trial + python_path: python/nstat/trial.py + status: mapped + notes: CovariateCollection plus CovColl adapter. + - matlab: Covariate.m + matlab_path: Covariate.m + python_module: nstat.signal + python_path: python/nstat/signal.py + status: mapped + notes: Covariate object plus Covariate adapter. + - matlab: DecodingAlgorithms.m + matlab_path: DecodingAlgorithms.m + python_module: nstat.decoding_algorithms + python_path: python/nstat/decoding_algorithms.py + status: mapped + notes: Decoding algorithms implemented. + - matlab: Events.m + matlab_path: Events.m + python_module: nstat.events + python_path: python/nstat/events.py + status: mapped + notes: Events API implemented. + - matlab: FitResSummary.m + matlab_path: FitResSummary.m + python_module: nstat.fit + python_path: python/nstat/fit.py + status: mapped + notes: FitSummary/FitResSummary adapter available. + - matlab: FitResult.m + matlab_path: FitResult.m + python_module: nstat.fit + python_path: python/nstat/fit.py + status: mapped + notes: FitResult implemented with compatibility adapter. + - matlab: History.m + matlab_path: History.m + python_module: nstat.history + python_path: python/nstat/history.py + status: mapped + notes: History basis implementation present. + - matlab: SignalObj.m + matlab_path: SignalObj.m + python_module: nstat.signal + python_path: python/nstat/signal.py + status: mapped + notes: Signal class and SignalObj adapter available. + - matlab: Trial.m + matlab_path: Trial.m + python_module: nstat.trial + python_path: python/nstat/trial.py + status: mapped + notes: Trial API implemented. + - matlab: TrialConfig.m + matlab_path: TrialConfig.m + python_module: nstat.trial + python_path: python/nstat/trial.py + status: mapped + notes: TrialConfig implementation present. + - matlab: getPaperDataDirs.m + matlab_path: getPaperDataDirs.m + python_module: nstat.paper_examples + python_path: python/nstat/paper_examples.py + status: partial + notes: Similar data-root resolution exists but not as standalone public API. + - matlab: nSTAT_Install.m + matlab_path: nSTAT_Install.m + python_module: nstat.nstat_install + python_path: python/nstat/nstat_install.py + status: mapped + notes: Installation helper available in Python package. + - matlab: nspikeTrain.m + matlab_path: nspikeTrain.m + python_module: nstat.spikes + python_path: python/nstat/spikes.py + status: mapped + notes: SpikeTrain and nspikeTrain adapter available. + - matlab: nstColl.m + matlab_path: nstColl.m + python_module: nstat.spikes + python_path: python/nstat/spikes.py + status: mapped + notes: SpikeTrainCollection and nstColl adapter available. + - matlab: nstatOpenHelpPage.m + matlab_path: nstatOpenHelpPage.m + python_module: null + python_path: null + status: not_applicable + notes: MATLAB help-browser utility has no Python equivalent. diff --git a/python/README.md b/python/README.md new file mode 100644 index 0000000..f5116e1 --- /dev/null +++ b/python/README.md @@ -0,0 +1,41 @@ +# Python nSTAT + +Standalone Python port of the nSTAT toolbox, organized around core MATLAB-mirroring classes with a Pythonic API. + +## Core API + +- `nstat.signal`: `Signal`, `Covariate` +- `nstat.spikes`: `SpikeTrain`, `SpikeTrainCollection` +- `nstat.events`: `Events` +- `nstat.history`: `HistoryBasis` +- `nstat.trial`: `CovariateCollection`, `TrialConfig`, `ConfigCollection`, `Trial` +- `nstat.cif`: `CIFModel` +- `nstat.analysis`: `Analysis` +- `nstat.fit`: `FitResult`, `FitSummary` +- `nstat.decoding`: `DecoderSuite` +- `nstat.datasets`: `list_datasets`, `get_dataset_path`, `verify_checksums` + +MATLAB-style module names remain importable as compatibility adapters and emit `DeprecationWarning`. + +## Install + +```bash +cd python +python3 -m pip install -e . +``` + +## Run basic examples + +```bash +cd python +python3 examples/basic_data_workflow.py +python3 examples/fit_poisson_glm.py +python3 examples/simulate_population_psth.py +``` + +## Run tests + +```bash +cd python +python3 -m pytest +``` diff --git a/python/docs/api.rst b/python/docs/api.rst new file mode 100644 index 0000000..14736bf --- /dev/null +++ b/python/docs/api.rst @@ -0,0 +1,25 @@ +API Reference +============= + +Core modules: + +- ``nstat.signal`` +- ``nstat.spikes`` +- ``nstat.events`` +- ``nstat.history`` +- ``nstat.trial`` +- ``nstat.cif`` +- ``nstat.analysis`` +- ``nstat.fit`` +- ``nstat.decoding`` +- ``nstat.datasets`` + +Compatibility adapters: + +- ``nstat.SignalObj`` +- ``nstat.nspikeTrain`` +- ``nstat.FitResult`` +- ``nstat.FitResSummary`` +- ``nstat.ConfigColl`` +- ``nstat.CovColl`` +- ``nstat.TrialConfig`` diff --git a/python/docs/index.rst b/python/docs/index.rst new file mode 100644 index 0000000..c1cf8ec --- /dev/null +++ b/python/docs/index.rst @@ -0,0 +1,9 @@ +nSTAT Python Documentation +========================== + +Minimal documentation for the standalone Python nSTAT core package. + +.. toctree:: + :maxdepth: 2 + + api diff --git a/python/examples/README.md b/python/examples/README.md new file mode 100644 index 0000000..7c8cc22 --- /dev/null +++ b/python/examples/README.md @@ -0,0 +1,19 @@ +# Python nSTAT Examples + +## Basic examples + +```bash +cd python +python3 examples/basic_data_workflow.py +python3 examples/fit_poisson_glm.py +python3 examples/simulate_population_psth.py +``` + +## Paper-style example workflow + +```bash +cd python +python3 examples/nstat_paper_examples.py --repo-root .. +``` + +This mirrors key analyses described in the nSTAT paper using the bundled Python APIs. diff --git a/python/pyproject.toml b/python/pyproject.toml new file mode 100644 index 0000000..fc4f69c --- /dev/null +++ b/python/pyproject.toml @@ -0,0 +1,33 @@ +[build-system] +requires = ["setuptools>=68", "wheel"] +build-backend = "setuptools.build_meta" + +[project] +name = "nstat-toolbox" +version = "0.1.0" +description = "Python port of the nSTAT toolbox" +readme = "README.md" +requires-python = ">=3.10" +authors = [ + { name = "Cajigas Lab" } +] +dependencies = [ + "numpy>=1.24", + "scipy>=1.10", + "matplotlib>=3.7" +] + +[project.optional-dependencies] +dev = [ + "pytest>=8.0", +] + +[tool.setuptools.packages.find] +where = ["."] +include = ["nstat*"] + +[tool.setuptools.package-data] +"nstat.data" = ["manifest.json"] + +[project.scripts] +nstat-paper-examples = "nstat.paper_examples:main" diff --git a/tools/matlab/cleanup_user_path_prefs.m b/tools/matlab/cleanup_user_path_prefs.m new file mode 100644 index 0000000..6e094ea --- /dev/null +++ b/tools/matlab/cleanup_user_path_prefs.m @@ -0,0 +1,68 @@ +function removedEntries = cleanup_user_path_prefs(rootDir) +% cleanup_user_path_prefs Remove stale nSTAT Python-port paths from MATLAB path. +% +% Usage: +% cleanup_user_path_prefs +% cleanup_user_path_prefs(rootDir) +% removedEntries = cleanup_user_path_prefs(...) +% +% This utility removes stale path entries under the current nSTAT repository +% that point to deleted Python-port maintenance trees. + +if nargin < 1 || isempty(rootDir) + installPath = which('nSTAT_Install'); + if isempty(installPath) + error('nSTAT:InstallPathNotFound', ... + 'Could not resolve rootDir; provide rootDir explicitly.'); + end + rootDir = fileparts(installPath); +end + +pathEntries = strsplit(path, pathsep); +if isempty(pathEntries) + removedEntries = {}; + return; +end + +staleRoots = { + fullfile(rootDir, 'python', 'matlab_port') + fullfile(rootDir, 'python', 'notebooks') + fullfile(rootDir, 'python', 'reports') + fullfile(rootDir, 'python', 'tools') +}; + +toRemoveMask = false(size(pathEntries)); +for iEntry = 1:numel(pathEntries) + entry = pathEntries{iEntry}; + if isempty(entry) + continue; + end + for iRoot = 1:numel(staleRoots) + if startsWith(entry, staleRoots{iRoot}) + toRemoveMask(iEntry) = true; + break; + end + end + if startsWith(entry, fullfile(rootDir, 'python')) && contains(entry, '__pycache__') + toRemoveMask(iEntry) = true; + end +end + +removedEntries = unique(pathEntries(toRemoveMask), 'stable'); +removedEntries = removedEntries(cellfun(@(p)~isempty(p), removedEntries)); + +if ~isempty(removedEntries) + keepMask = true(size(pathEntries)); + for i = 1:numel(pathEntries) + if any(strcmp(pathEntries{i}, removedEntries)) + keepMask(i) = false; + end + end + filteredPath = strjoin(pathEntries(keepMask), pathsep); + path(filteredPath); + savepath; + fprintf('Removed %d stale MATLAB path entries.\n', numel(removedEntries)); +else + fprintf('No stale MATLAB path entries found for cleanup.\n'); +end +end diff --git a/tools/matlab/run_nstat_smoke.m b/tools/matlab/run_nstat_smoke.m new file mode 100644 index 0000000..149cb7c --- /dev/null +++ b/tools/matlab/run_nstat_smoke.m @@ -0,0 +1,148 @@ +%% run_nstat_smoke +% Automated smoke checks for core nSTAT MATLAB classes and path resolution. + +scriptPath = mfilename('fullpath'); +rootDir = fileparts(fileparts(fileparts(scriptPath))); + +restoredefaultpath; +addpath(rootDir, '-begin'); +nSTAT_Install('RebuildDocSearch', false, 'CleanUserPathPrefs', false); + +failures = {}; + +runCheck('SignalObj', @() checkSignalObj()); +runCheck('Covariate', @() checkCovariate()); +runCheck('ConfidenceInterval', @() checkConfidenceInterval()); +runCheck('Events', @() checkEvents()); +runCheck('History', @() checkHistory()); +runCheck('nspikeTrain', @() checknspikeTrain()); +runCheck('nstColl', @() checknstColl()); +runCheck('CovColl', @() checkCovColl()); +runCheck('TrialConfig', @() checkTrialConfig()); +runCheck('ConfigColl', @() checkConfigColl()); +runCheck('Trial', @() checkTrial()); +runCheck('Analysis API', @() checkAnalysisApi()); +runCheck('DecodingAlgorithms API', @() checkDecodingApi()); +runCheck('FitResult path resolution', @() checkFitResultPath(rootDir)); +runCheck('FitResSummary path resolution', @() checkFitResSummaryPath(rootDir)); +runCheck('nstatOpenHelpPage path resolution', @() checkOpenHelpPath(rootDir)); + +if ~isempty(failures) + fprintf(2, 'nSTAT smoke test failures (%d):\n', numel(failures)); + for i = 1:numel(failures) + fprintf(2, ' - %s\n', failures{i}); + end + error('nSTATSmoke:Failures', 'Smoke validation failed.'); +end + +fprintf('All nSTAT smoke checks passed.\n'); + +function runCheck(name, fn) + try + fn(); + fprintf('PASS %s\n', name); + catch ME + failures{end+1} = sprintf('%s :: %s', name, ME.message); %#ok + end +end + +function checkSignalObj() + t = (0:0.01:1)'; + y = sin(2*pi*t); + s = SignalObj(t, y, 'sig', 'time', 's', '', {'sig'}); + assert(isa(s, 'SignalObj')); +end + +function checkCovariate() + t = (0:0.01:1)'; + y = sin(2*pi*t); + c = Covariate(t, y, 'cov', 'time', 's', '', {'cov'}); + assert(isa(c, 'Covariate')); +end + +function checkConfidenceInterval() + t = (0:0.01:1)'; + y = sin(2*pi*t); + ci = ConfidenceInterval(t, [y-0.1, y+0.1], 'ci', 'time', 's', '', {'low','high'}); + assert(isa(ci, 'ConfidenceInterval')); +end + +function checkEvents() + e = Events([0.1, 0.2], {'start', 'stop'}); + assert(isa(e, 'Events')); +end + +function checkHistory() + h = History([0, 0.01, 0.05]); + assert(isa(h, 'History')); +end + +function checknspikeTrain() + nst = nspikeTrain([0.1, 0.2, 0.3], '1', 0.01, 0, 1); + assert(isa(nst, 'nspikeTrain')); +end + +function checknstColl() + nst = nspikeTrain([0.1, 0.2, 0.3], '1', 0.01, 0, 1); + coll = nstColl(nst); + assert(isa(coll, 'nstColl')); +end + +function checkCovColl() + t = (0:0.01:1)'; + y = sin(2*pi*t); + c = Covariate(t, y, 'cov', 'time', 's', '', {'cov'}); + coll = CovColl({c}); + assert(isa(coll, 'CovColl')); +end + +function checkTrialConfig() + tc = TrialConfig(); + assert(isa(tc, 'TrialConfig')); +end + +function checkConfigColl() + tc = TrialConfig(); + cc = ConfigColl({tc}); + assert(isa(cc, 'ConfigColl')); +end + +function checkTrial() + t = (0:0.01:1)'; + y = sin(2*pi*t); + cov = Covariate(t, y, 'cov', 'time', 's', '', {'cov'}); + covColl = CovColl({cov}); + nst = nspikeTrain([0.1, 0.2, 0.3], '1', 0.01, 0, 1); + nstCollection = nstColl(nst); + tr = Trial(nstCollection, covColl); + assert(isa(tr, 'Trial')); +end + +function checkAnalysisApi() + m = methods('Analysis'); + assert(~isempty(m)); +end + +function checkDecodingApi() + m = methods('DecodingAlgorithms'); + assert(~isempty(m)); +end + +function checkFitResultPath(rootDir) + allPaths = which('-all', 'FitResult'); + assert(~isempty(allPaths), 'FitResult not found on path'); + expected = fullfile(rootDir, 'FitResult.m'); + assert(strcmp(allPaths{1}, expected), 'Canonical FitResult.m is not first on path'); +end + +function checkFitResSummaryPath(rootDir) + resolved = which('FitResSummary'); + expected = fullfile(rootDir, 'FitResSummary.m'); + assert(strcmp(resolved, expected), 'FitResSummary did not resolve to root class file'); +end + +function checkOpenHelpPath(rootDir) + target = nstatOpenHelpPage('NeuralSpikeAnalysis_top.html', false); + expected = fullfile(rootDir, 'helpfiles', 'NeuralSpikeAnalysis_top.html'); + assert(strcmp(target, expected), 'nstatOpenHelpPage resolved unexpected path'); +end