diff --git a/matlab/aggregate.m b/matlab/aggregate.m
index 1f7d4f5af8cd632950811fdebd66ad1a7efca489..e85455506afe77b43eaa854c275623af4faf2009 100644
--- a/matlab/aggregate.m
+++ b/matlab/aggregate.m
@@ -2,7 +2,7 @@ function aggregate(ofile, dynopt, rootfolder, varargin)
 
 % Agregates cherry-picked models.
 
-% Copyright (C) 2019 Dynare Team
+% Copyright (C) 2019-2021 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -101,7 +101,11 @@ for i=1:length(varargin)
     end
 end
 eqlist = eqlist(1:eqnum,:);
-[~, idx] = unique(eqlist(:,1), 'stable');
+if isoctave && octave_ver_less_than('6')
+    [~, idx] = unique_stable(eqlist(:,1));
+else
+    [~, idx] = unique(eqlist(:,1), 'stable');
+end
 eqlist = eqlist(idx, :);
 
 % Get endogenous variables.
@@ -124,7 +128,11 @@ for i=1:length(varargin)
     fclose(fid);
 end
 elist = elist(1:enum,:);
-[~, idx] = unique(elist(:,1), 'stable');
+if isoctave && octave_ver_less_than('6')
+    [~, idx] = unique_stable(elist(:,1));
+else
+    [~, idx] = unique(elist(:,1), 'stable');
+end
 elist = elist(idx,:);
 
 % Get exogenous variables.
@@ -152,7 +160,11 @@ for i=1:length(varargin)
     fclose(fid);
 end
 xlist = xlist(1:xnum,:);
-[~, idx] = unique(xlist(:,1), 'stable');
+if isoctave && octave_ver_less_than('6')
+    [~, idx] = unique_stable(xlist(:,1));
+else
+    [~, idx] = unique(xlist(:,1), 'stable');
+end
 xlist = xlist(idx,:);
 
 % Get parameter values.
@@ -291,4 +303,4 @@ function blkname = getblockname(str, ROOT_FOLDER)
     str = strrep(str, '/', filesep());
     str = strrep(str, [ROOT_FOLDER filesep() 'blocks' filesep()], '');
     idx = strfind(str, filesep());
-    blkname = str(1:idx(1)-1);
\ No newline at end of file
+    blkname = str(1:idx(1)-1);
diff --git a/matlab/dynare_config.m b/matlab/dynare_config.m
index f54506f4fa41734cfce21cf7dac064297773b72a..1ee41770374ae47522a57c0a4c1e507f2d69b9d1 100644
--- a/matlab/dynare_config.m
+++ b/matlab/dynare_config.m
@@ -16,7 +16,7 @@ function dynareroot = dynare_config(path_to_dynare)
 % SPECIAL REQUIREMENTS
 %   none
 
-% Copyright (C) 2001-2020 Dynare Team
+% Copyright (C) 2001-2021 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -90,9 +90,10 @@ if isoctave && octave_ver_less_than('5')
     p{end+1} = '/missing/ordeig';
 end
 
-%% intersect(…, 'stable') doesn't exist in Octave < 6
+%% intersect(…, 'stable') and unique(…, 'stable') doen't exist in Octave < 6
 if isoctave && octave_ver_less_than('6')
     p{end+1} = '/missing/intersect_stable';
+    p{end+1} = '/missing/unique_stable';
 end
 
 % Replacements for functions of the MATLAB statistics toolbox
diff --git a/matlab/missing/unique_stable/unique_stable.m b/matlab/missing/unique_stable/unique_stable.m
new file mode 100644
index 0000000000000000000000000000000000000000..113e12beb51477bae79c51618f41b13dd70d9d1f
--- /dev/null
+++ b/matlab/missing/unique_stable/unique_stable.m
@@ -0,0 +1,36 @@
+% Crude implementation of unique(…, 'stable'), which is missing in Octave < 6
+
+% Copyright (C) 2021 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare 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 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare 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 Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+function r = unique_stable(x)
+
+if iscell(x)
+    error('Cell arrays unsupported')
+end
+
+r = [];
+for i = 1:numel(x)
+    if ~ismember(x(i), r)
+        r = [ r, x(i) ];
+    end
+end
+if size(x, 2) == 1
+    r = r';
+end
+
+end
diff --git a/matlab/ols/getEquationsByTags.m b/matlab/ols/getEquationsByTags.m
index 29edee83253508a5dde3fb30b6e97d63478a9fd3..7d7c85b91211a7f9f12fca3ebe55a0514011bb98 100644
--- a/matlab/ols/getEquationsByTags.m
+++ b/matlab/ols/getEquationsByTags.m
@@ -17,7 +17,7 @@ function [ast] = getEquationsByTags(ast, tagname, tagvalue)
 % SPECIAL REQUIREMENTS
 %   none
 
-% Copyright (C) 2017-2019 Dynare Team
+% Copyright (C) 2017-2021 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -72,5 +72,9 @@ for i = 1:length(tagvalue)
     end
 end
 assert(~isempty(idx2keep), 'getEquationsByTags: no equations selected');
-ast = ast(unique(idx2keep, 'stable'));
+if isoctave && octave_ver_less_than('6')
+    ast = ast(unique_stable(idx2keep));
+else
+    ast = ast(unique(idx2keep, 'stable'));
+end
 end