summaryrefslogtreecommitdiff
path: root/inst/arrays/@stk_factorialdesign/ndgrid.m
blob: f731f3a290487e412c8f42a3b48c3b7bdbe870b7 (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
% NDGRID produces ndgrid-style coordinate arrays
%
% CALL: [X1, X2, ...] = ndgrid (X)
%
%    produces ndgrid-style coordinate arrays X1, X2, ... Xd based on the
%    @stk_factorialdesign object X (where d is the number of columns of
%    X). This is equivalent to
%
%       [X1, ..., Xd] = ndgrid (X.levels{1}, ..., X.levels{d});
%
% See also: ndgrid

% Copyright Notice
%
%    Copyright (C) 2018 CentraleSupelec
%    Copyright (C) 2013, 2014 SUPELEC
%
%    Author:  Julien Bect  <julien.bect@centralesupelec.fr>

% Copying Permission Statement
%
%    This file is part of
%
%            STK: a Small (Matlab/Octave) Toolbox for Kriging
%               (http://sourceforge.net/projects/kriging)
%
%    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 = ndgrid (x)

d = length (x.levels);

if nargout > d
    
    stk_error ('Too many output arguments.', 'TooManyOutputArgs');
    
else
    
    if (d == 0) || any (cellfun (@isempty, x.levels))
        
        varargout = repmat ({[]}, 1, nargout);
        
    elseif d == 1
        
        varargout = {x.levels{1}(:)};
        
    else
        
        varargout = cell (1, max (nargout, 1));
        [varargout{:}] = ndgrid (x.levels{:});
        
    end
    
end

end % function


%--- general case -------------------------------------------------------------

%!shared data
%! data = stk_factorialdesign ({[0 1], [5 6 7]});

%!test % nargout = 0
%! ndgrid (data);
%! assert (isequal (ans, [0 0 0; 1 1 1]));

%!test % nargout = 1
%! x = ndgrid (data);
%! assert (isequal (x, [0 0 0; 1 1 1]));

%!test % nargout = 2
%! [x, y] = ndgrid (data);
%! assert (isequal ({x, y}, {[0 0 0; 1 1 1], [5 6 7; 5 6 7]}));

%!error % nargout = 3
%! [x, y, z] = ndgrid (data);

%--- special cases ------------------------------------------------------------

%!test
%! data = stk_factorialdesign ({[], []});
%! [x, y] = ndgrid (data);
%! assert (isequal ({x, y}, {[], []}));

%!test
%! data = stk_factorialdesign ({[1:3]});
%! x = ndgrid (data);
%! assert (isequal (x, [1; 2; 3]));