intersect(…, 'stable') was introduced in MATLAB R2013a

Re-use the existing Octave replacement by making it MATLAB-compatible.
time-shift
Sébastien Villemot 2019-03-26 18:33:15 +01:00
parent d6c449ea70
commit e48177543b
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
10 changed files with 56 additions and 59 deletions

View File

@ -145,7 +145,7 @@ if M_.exo_nbr > 1
end end
skipline(); skipline();
if ~all(M_.H==0) if ~all(M_.H==0)
if isoctave if isoctave || matlab_ver_less_than('8.1')
[observable_name_requested_vars, varlist_pos] = intersect_stable(var_list_, options_.varobs); [observable_name_requested_vars, varlist_pos] = intersect_stable(var_list_, options_.varobs);
else else
[observable_name_requested_vars, varlist_pos] = intersect(var_list_, options_.varobs, 'stable'); [observable_name_requested_vars, varlist_pos] = intersect(var_list_, options_.varobs, 'stable');

View File

@ -89,7 +89,7 @@ end
% Measurement error % Measurement error
if ~all(StateSpaceModel.measurement_error==0) if ~all(StateSpaceModel.measurement_error==0)
if isoctave if isoctave || matlab_ver_less_than('8.1')
[observable_pos,index_subset,index_observables]=intersect_stable(SubsetOfVariables,StateSpaceModel.observable_pos); [observable_pos,index_subset,index_observables]=intersect_stable(SubsetOfVariables,StateSpaceModel.observable_pos);
else else
[observable_pos,index_subset,index_observables]=intersect(SubsetOfVariables,StateSpaceModel.observable_pos,'stable'); [observable_pos,index_subset,index_observables]=intersect(SubsetOfVariables,StateSpaceModel.observable_pos,'stable');
@ -105,4 +105,4 @@ if ~all(StateSpaceModel.measurement_error==0)
ConditionalVarianceDecomposition_ME(:,:,number_of_state_innovations+1)=1-sum(ConditionalVarianceDecomposition_ME(:,:,1:number_of_state_innovations),3); ConditionalVarianceDecomposition_ME(:,:,number_of_state_innovations+1)=1-sum(ConditionalVarianceDecomposition_ME(:,:,1:number_of_state_innovations),3);
else else
ConditionalVarianceDecomposition_ME=[]; ConditionalVarianceDecomposition_ME=[];
end end

View File

@ -63,7 +63,7 @@ if isempty(exogenous_variable_index)
end end
end end
if isoctave if isoctave || matlab_ver_less_than('8.1')
[observable_pos_requested_vars,index_subset,index_observables]=intersect_stable(var_list,options_.varobs); [observable_pos_requested_vars,index_subset,index_observables]=intersect_stable(var_list,options_.varobs);
else else
[observable_pos_requested_vars,index_subset,index_observables]=intersect(var_list,options_.varobs,'stable'); [observable_pos_requested_vars,index_subset,index_observables]=intersect(var_list,options_.varobs,'stable');
@ -136,4 +136,4 @@ oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.HPDsup.(name_1).(name_2
oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.deciles.(name_1).(name_2) = p_deciles; oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.deciles.(name_1).(name_2) = p_deciles;
if options_.estimation.moments_posterior_density.indicator if options_.estimation.moments_posterior_density.indicator
oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.density.(name_1).(name_2) = p_density; oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.density.(name_1).(name_2) = p_density;
end end

View File

@ -50,7 +50,7 @@ y = y(ivar,options_.drop+1:end)';
ME_present=0; ME_present=0;
if ~all(M_.H==0) if ~all(M_.H==0)
if isoctave if isoctave || matlab_ver_less_than('8.1')
[observable_pos_requested_vars, index_subset, index_observables] = intersect_stable(ivar, options_.varobs_id); [observable_pos_requested_vars, index_subset, index_observables] = intersect_stable(ivar, options_.varobs_id);
else else
[observable_pos_requested_vars, index_subset, index_observables] = intersect(ivar, options_.varobs_id, 'stable'); [observable_pos_requested_vars, index_subset, index_observables] = intersect(ivar, options_.varobs_id, 'stable');

View File

@ -55,7 +55,7 @@ oo_.var = oo_.gamma_y{1};
ME_present=0; ME_present=0;
if ~all(M_.H==0) if ~all(M_.H==0)
if isoctave if isoctave || matlab_ver_less_than('8.1')
[observable_pos_requested_vars,index_subset,index_observables]=intersect_stable(ivar,options_.varobs_id); [observable_pos_requested_vars,index_subset,index_observables]=intersect_stable(ivar,options_.varobs_id);
else else
[observable_pos_requested_vars,index_subset,index_observables]=intersect(ivar,options_.varobs_id,'stable'); [observable_pos_requested_vars,index_subset,index_observables]=intersect(ivar,options_.varobs_id,'stable');
@ -105,7 +105,7 @@ if size(stationary_vars, 1) > 0
lh = cellofchararraymaxlength(M_.endo_names(ivar(stationary_vars)))+2; lh = cellofchararraymaxlength(M_.endo_names(ivar(stationary_vars)))+2;
dyntable(options_, title, headers, M_.endo_names(ivar(stationary_vars)), 100*oo_.gamma_y{options_.ar+2}(stationary_vars,:), lh, 8, 2); dyntable(options_, title, headers, M_.endo_names(ivar(stationary_vars)), 100*oo_.gamma_y{options_.ar+2}(stationary_vars,:), lh, 8, 2);
if ME_present if ME_present
if isoctave if isoctave || matlab_ver_less_than('8.1')
[stationary_observables, pos_index_subset] = intersect_stable(index_subset, stationary_vars); [stationary_observables, pos_index_subset] = intersect_stable(index_subset, stationary_vars);
else else
[stationary_observables, pos_index_subset] = intersect(index_subset, stationary_vars, 'stable'); [stationary_observables, pos_index_subset] = intersect(index_subset, stationary_vars, 'stable');

View File

@ -84,7 +84,7 @@ MaXNumberOfConditionalDecompLines = ceil(options_.MaxNumberOfBytes/NumberOfSaved
ME_present=0; ME_present=0;
if ~all(M_.H==0) if ~all(M_.H==0)
if isoctave if isoctave || matlab_ver_less_than('8.1')
[observable_pos_requested_vars,index_subset,index_observables]=intersect_stable(ivar,options_.varobs_id); [observable_pos_requested_vars,index_subset,index_observables]=intersect_stable(ivar,options_.varobs_id);
else else
[observable_pos_requested_vars,index_subset,index_observables]=intersect(ivar,options_.varobs_id,'stable'); [observable_pos_requested_vars,index_subset,index_observables]=intersect(ivar,options_.varobs_id,'stable');

View File

@ -87,7 +87,7 @@ MaXNumberOfDecompLines = ceil(options_.MaxNumberOfBytes/NumberOfSavedElementsPer
ME_present=0; ME_present=0;
if ~all(M_.H==0) if ~all(M_.H==0)
if isoctave if isoctave || matlab_ver_less_than('8.1')
[observable_pos_requested_vars,index_subset,index_observables]=intersect_stable(ivar,options_.varobs_id); [observable_pos_requested_vars,index_subset,index_observables]=intersect_stable(ivar,options_.varobs_id);
else else
[observable_pos_requested_vars,index_subset,index_observables]=intersect(ivar,options_.varobs_id,'stable'); [observable_pos_requested_vars,index_subset,index_observables]=intersect(ivar,options_.varobs_id,'stable');

View File

@ -93,8 +93,8 @@ if isoctave && octave_ver_less_than('4.4') && ~user_has_octave_forge_package('na
p{end+1} = '/missing/corrcoef'; p{end+1} = '/missing/corrcoef';
end end
% intersect(..., 'stable') doesn't exist in Octave %% intersect(…, 'stable') doesn't exist in Octave and in MATLAB < R2013a
if isoctave if isoctave || matlab_ver_less_than('8.1')
p{end+1} = '/missing/intersect_stable'; p{end+1} = '/missing/intersect_stable';
end end

View File

@ -1,63 +1,64 @@
### Crude implementation of insersect(, 'stable'), which is missing in Octave % Crude implementation of intersect(…, 'stable'), which is missing in Octave
### Copyright (C) 2019 Dynare Team % Copyright (C) 2019 Dynare Team
### %
### This file is part of Dynare. % This file is part of Dynare.
### %
### Dynare is free software: you can redistribute it and/or modify % Dynare is free software: you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by % it under the terms of the GNU General Public License as published by
### the Free Software Foundation, either version 3 of the License, or % the Free Software Foundation, either version 3 of the License, or
### (at your option) any later version. % (at your option) any later version.
### %
### Dynare is distributed in the hope that it will be useful, % Dynare is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of % but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
### GNU General Public License for more details. % GNU General Public License for more details.
### %
### You should have received a copy of the GNU General Public License % You should have received a copy of the GNU General Public License
### along with Dynare. If not, see <http://www.gnu.org/licenses/>. % along with Dynare. If not, see <http://www.gnu.org/licenses/>.
function [c, ia, ib] = intersect_stable(a, b) function [c, ia, ib] = intersect_stable(a, b)
if (nargin != 2) if nargin ~= 2
print_usage (); error('intersect_stable: needs exactly 2 input arguments');
endif end
if (isnumeric (a) && isnumeric (b)) if isnumeric (a) && isnumeric (b)
c = []; c = [];
elseif (iscell (b)) elseif iscell (b)
c = {}; c = {};
else else
c = ""; c = '';
endif end
ia = ib = []; ia = [];
ib = [];
if (isempty (a) || isempty (b)) if isempty (a) || isempty (b)
return return
else else
isrowvec = isrow (a) && isrow (b); isrowvec = isrow (a) && isrow (b);
for i = 1:numel(a) for i = 1:numel(a)
if (iscellstr(c)) if iscellstr(c)
idx = strcmp(a(i), b); idx = strcmp(a(i), b);
else else
idx = a(i) == b; idx = a(i) == b;
endif end
if (any(idx) && !ismember(a(i), c)) if any(idx) && ~ismember(a(i), c)
c = [c(:); a(i)]; c = [c(:); a(i)];
if (nargout > 1) if nargout > 1
ia = [ia, i]; ia = [ia, i];
ib = [ib, find(idx)]; ib = [ib, find(idx)];
endif end
endif end
endfor end
## Adjust output orientation for MATLAB compatibility %% Adjust output orientation for MATLAB compatibility
if (isrowvec) if isrowvec
c = c.'; c = c.';
endif end
endif end
endfunction end
%!test %!test
%! a = [3 4 1 5]; %! a = [3 4 1 5];
@ -80,15 +81,11 @@ endfunction
%! assert(b(ib), c) %! assert(b(ib), c)
%!test %!test
%! a = { "defun", "mapcar", "let", "eval-when"}; %! a = { 'defun', 'mapcar', 'let', 'eval-when'};
%! b = { "setf", "let", "list", "cdr", "defun"}; %! b = { 'setf', 'let', 'list', 'cdr', 'defun'};
%! [c,ia,ib]=intersect_stable(a,b); %! [c,ia,ib]=intersect_stable(a,b);
%! assert(c, { "defun", "let" }) %! assert(c, { 'defun', 'let' })
%! assert(ia, [1 3]) %! assert(ia, [1 3])
%! assert(ib, [5 2]) %! assert(ib, [5 2])
%! assert(a(ia), c) %! assert(a(ia), c)
%! assert(b(ib), c) %! assert(b(ib), c)
## Local variables:
## mode: Octave
## End:

View File

@ -66,7 +66,7 @@ switch type
M_.exo_names,arg2,vartan,arg1,options_.mh_conf_sig,oo_,options_); M_.exo_names,arg2,vartan,arg1,options_.mh_conf_sig,oo_,options_);
if ~all(M_.H==0) if ~all(M_.H==0)
if strmatch(arg1,options_.varobs,'exact') if strmatch(arg1,options_.varobs,'exact')
if isoctave if isoctave || matlab_ver_less_than('8.1')
[observable_name_requested_vars,index_subset,index_observables]=intersect_stable(vartan,options_.varobs); [observable_name_requested_vars,index_subset,index_observables]=intersect_stable(vartan,options_.varobs);
else else
[observable_name_requested_vars,index_subset,index_observables]=intersect(vartan,options_.varobs,'stable'); [observable_name_requested_vars,index_subset,index_observables]=intersect(vartan,options_.varobs,'stable');
@ -97,4 +97,4 @@ switch type
end end
otherwise otherwise
disp('Not yet implemented') disp('Not yet implemented')
end end