summaryrefslogtreecommitdiff
path: root/inst/arrays/@stk_factorialdesign/ismember.m
blob: f4947a682f370ee0b4bda8ac746103e2353d6787 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
% ISMEMBER [overload base function]

% Copyright Notice
%
%    Copyright (C) 2017 CentraleSupelec
%
%    Author:  Julien Bect  <julien.bect@centralesupelec.fr>

% Copying Permission Statement
%
%    This file is part of
%
%            STK: a Small (Matlab/Octave) Toolbox for Kriging
%               (https://github.com/stk-kriging/stk/)
%
%    STK 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.
%
%    STK 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 STK.  If not, see <http://www.gnu.org/licenses/>.

function varargout = ismember (A, B, varargin)

if ~ all (cellfun (@ischar, varargin))
    stk_error ('Invalid flag (should be a string).', 'InvalidArgument');
end

if isa (A, 'stk_factorialdesign')

    varargout = cell (1, max (nargout, 1));
    [varargout{:}] = ismember (double (A), B, varargin{:});
    
else  % B is an stk_factorialdesign object
    
    has_rows = false;
    has_legacy = false;
    
    for k = 1:(numel (varargin))
        switch varargin{k}
            case 'rows'
                has_rows = true;
            case 'legacy'
                has_legacy = true;
            otherwise
                errmsg = sprintf ('Unknown flag: %s', varargin{k});
                stk_error (errmsg, 'InvalidArgument');
        end
    end
    
    if has_rows && ~ has_legacy && (nargout < 2)  % This case can be optimized
        
        lia = ismember_fd (A, B);
        varargout = {lia};
        
    else  % otherwise, use the base ismember function
        
        varargout = cell (1, max (nargout, 1));
        [varargout{:}] = ismember (A, double (B), varargin{:});
        
    end
    
end

end % function


function [lia, locb] = ismember_fd (A, B)

[n, dim] = size (A);
if dim ~= length (B.levels)
    stk_error (['A and B should have the same number ' ...
        'of columns'], 'InvalidArgument');
end

b = false (n, dim);
for j = 1:dim
    b(:, j) = ismember (A(:, j), B.levels{j});
end

lia = all (b, 2);

end % function


%!shared A, B, BB, b
%!
%! i_max = 10;  n = 100;  d = 5;
%!
%! A = randi (i_max, n, d);
%!
%! levels = repmat ({1:i_max}, 1, d);
%! levels{4} = 1:2:i_max;
%! B = stk_factorialdesign (levels);
%!
%! BB = double (B);

%!test b = ismember (A, B);
%!assert (isequal (b, ismember (A, BB)));

%!test b = ismember (A, B, 'rows');
%!assert (isequal (b, ismember (A, BB, 'rows')));