comparison scripts/set/create_set.m @ 11921:166a195399f7 release-3-0-x

[project @ 2008-01-04 18:18:22 by jwe]
author jwe
date Fri, 16 Jan 2009 08:07:05 +0100
parents a1dbe9d80eee
children 83a8781b529d af4fa72ee250
comparison
equal deleted inserted replaced
11920:7118a78a4378 11921:166a195399f7
1 ## Copyright (C) 1994, 1996, 1997, 1999, 2000, 2004, 2005, 2006, 2007 1 ## Copyright (C) 1994, 1996, 1997, 1999, 2000, 2004, 2005, 2006, 2007, 2008
2 ## John W. Eaton 2 ## John W. Eaton
3 ## 3 ##
4 ## This file is part of Octave. 4 ## This file is part of Octave.
5 ## 5 ##
6 ## Octave is free software; you can redistribute it and/or modify it 6 ## Octave is free software; you can redistribute it and/or modify it
17 ## along with Octave; see the file COPYING. If not, see 17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>. 18 ## <http://www.gnu.org/licenses/>.
19 19
20 ## -*- texinfo -*- 20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {} create_set (@var{x}) 21 ## @deftypefn {Function File} {} create_set (@var{x})
22 ## @deftypefnx{Function File} {} create_set (@var{x}, "rows")
22 ## Return a row vector containing the unique values in @var{x}, sorted in 23 ## Return a row vector containing the unique values in @var{x}, sorted in
23 ## ascending order. For example, 24 ## ascending order. For example,
24 ## 25 ##
25 ## @example 26 ## @example
26 ## @group 27 ## @group
27 ## create_set ([ 1, 2; 3, 4; 4, 2 ]) 28 ## create_set ([ 1, 2; 3, 4; 4, 2; 1, 2 ])
28 ## @result{} [ 1, 2, 3, 4 ] 29 ## @result{} [ 1, 2, 3, 4 ]
29 ## @end group 30 ## @end group
30 ## @end example 31 ## @end example
31 ## @seealso{union, intersection, complement} 32 ##
33 ## If the optional second input argument is the string "rows" each row of
34 ## the matrix @var{x} will be considered an element of set. For example,
35 ## @example
36 ## @group
37 ## create_set ([ 1, 2; 3, 4; 4, 2; 1, 2 ], "rows")
38 ## @result{} 1 2
39 ## 3 4
40 ## 4 2
41 ## @end group
42 ## @end example
43 ## @seealso{union, intersection, complement, unique}
32 ## @end deftypefn 44 ## @end deftypefn
33 45
34 ## Author: jwe 46 ## Author: jwe
35 47
36 function y = create_set(x) 48 function y = create_set (x, rows_opt)
37 49
38 if (nargin != 1) 50 if (nargin < 1 || nargin > 2)
39 print_usage (); 51 print_usage ();
40 endif 52 endif
41 53
42 if (isempty(x)) 54 if (nargin == 1)
43 y = []; 55 y = unique (x).';
56 elseif (strcmpi (rows_opt, "rows"))
57 y = unique (x, "rows");
44 else 58 else
45 nel = numel (x); 59 error ("create_set: expecting \"rows\" as second argument");
46 y = sort (reshape (x, 1, nel));
47 els = find (y(1:nel-1) != y(2:nel));
48 if (isempty (els));
49 y = y(1);
50 else
51 y = y([1, els+1]);
52 endif
53 endif 60 endif
54 61
55 endfunction 62 endfunction