diff --git a/matlab/dsge_likelihood.m b/matlab/dsge_likelihood.m
index 68574b40d47639aa64bfe4b3fa4facdcf7a92f1f..fbb2d1eab32148bd45b1813bcbe388a62591ca74 100644
--- a/matlab/dsge_likelihood.m
+++ b/matlab/dsge_likelihood.m
@@ -359,6 +359,7 @@ end
 
 diffuse_periods = 0;
 correlated_errors_have_been_checked = 0;
+singular_diffuse_filter = 0;
 switch DynareOptions.lik_init
   case 1% Standard initialization with the steady state of the state equation.
     if kalman_algo~=2
@@ -407,17 +408,17 @@ switch DynareOptions.lik_init
         diffuse_periods = length(tmp);
         if isinf(dLIK)
             % Go to univariate diffuse filter if singularity problem.
-            kalman_algo = 4;
+            singular_diffuse_filter = 1;
         end
     end
-    if (kalman_algo==4)
+    if singular_diffuse_filter || (kalman_algo==4)
         % Univariate Diffuse Kalman Filter
         if isequal(H,0)
-            H = zeros(nobs,1);
+            H1 = zeros(nobs,1);
             mmm = mm;
         else
             if all(all(abs(H-diag(diag(H)))<1e-14))% ie, the covariance matrix is diagonal...
-                H = diag(H);
+                H1 = diag(H);
                 mmm = mm; 
             else
                 Z = [Z, eye(pp)];
@@ -426,7 +427,7 @@ switch DynareOptions.lik_init
                 R = blkdiag(R,eye(pp));
                 Pstar = blkdiag(Pstar,H);
                 Pinf  = blckdiag(Pinf,zeros(pp));
-                H = zeros(nobs,1);
+                H1 = zeros(nobs,1);
                 mmm   = mm+pp;
             end
         end
@@ -439,7 +440,7 @@ switch DynareOptions.lik_init
                                                         Y, 1, size(Y,2), ...
                                                         zeros(mmm,1), Pinf, Pstar, ...
                                                         kalman_tol, riccati_tol, DynareOptions.presample, ...
-                                                        T,R,Q,H,Z,mmm,pp,rr);
+                                                        T,R,Q,H1,Z,mmm,pp,rr);
         diffuse_periods = length(tmp);
     end
   case 4% Start from the solution of the Riccati equation.
diff --git a/matlab/dsge_likelihood_hh.m b/matlab/dsge_likelihood_hh.m
index 9d91a44b9a0c5ac6d1a958018a8a4e295fe0b002..ff8c6d3837e3b846ac50ed1108bd3dd6978cb861 100644
--- a/matlab/dsge_likelihood_hh.m
+++ b/matlab/dsge_likelihood_hh.m
@@ -255,6 +255,7 @@ end
 
 diffuse_periods = 0;
 correlated_errors_have_been_checked = 0;
+singular_diffuse_filter = 0;
 switch DynareOptions.lik_init
   case 1% Standard initialization with the steady state of the state equation.
     if kalman_algo~=2
@@ -303,18 +304,17 @@ switch DynareOptions.lik_init
         diffuse_periods = length(dlik);
         if isinf(dLIK)
             % Go to univariate diffuse filter if singularity problem.
-            kalman_algo = 4;
-            singularity_flag = 1;
+            singular_diffuse_filter
         end
     end
-    if (kalman_algo==4)
+    if singular_diffuse_filter || (kalman_algo==4)
         % Univariate Diffuse Kalman Filter
         if isequal(H,0)
-            H = zeros(nobs,1);
+            H1 = zeros(nobs,1);
             mmm = mm;
         else
             if all(all(abs(H-diag(diag(H)))<1e-14))% ie, the covariance matrix is diagonal...
-                H = diag(H);
+                H1 = diag(H);
                 mmm = mm; 
             else
                 Z = [Z, eye(pp)];
@@ -323,18 +323,20 @@ switch DynareOptions.lik_init
                 R = blkdiag(R,eye(pp));
                 Pstar = blkdiag(Pstar,H);
                 Pinf  = blckdiag(Pinf,zeros(pp));
-                H = zeros(nobs,1);
+                H1 = zeros(nobs,1);
                 mmm   = mm+pp;
             end
         end
         % no need to test again for correlation elements
         correlated_errors_have_been_checked = 1;
         
-        [dLIK,dlik,a,Pstar] = univariate_kalman_filter_d(DynareDataset.missing.aindex,DynareDataset.missing.number_of_observations,DynareDataset.missing.no_more_missing_observations, ...
-                                                              Y, 1, size(Y,2), ...
-                                                              zeros(mmm,1), Pinf, Pstar, ...
-                                                              kalman_tol, riccati_tol, DynareOptions.presample, ...
-                                                              T,R,Q,H,Z,mmm,pp,rr);
+        [dLIK,dlik,a,Pstar] = univariate_kalman_filter_d(DynareDataset.missing.aindex,...
+                                                         DynareDataset.missing.number_of_observations,...
+                                                         DynareDataset.missing.no_more_missing_observations, ...
+                                                         Y, 1, size(Y,2), ...
+                                                         zeros(mmm,1), Pinf, Pstar, ...
+                                                         kalman_tol, riccati_tol, DynareOptions.presample, ...
+                                                         T,R,Q,H1,Z,mmm,pp,rr);
         diffuse_periods = length(dlik);
     end
   case 4% Start from the solution of the Riccati equation.