comparison scripts/set/union.m @ 20623:2edd668e7784

union.m: Matlab compatible output orientation for empty input ([]). * union.m: Check inputs a,b with isempty which qualifies as a row vector. Add BIST tests to verify new behavior.
author Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
date Fri, 31 Jul 2015 14:01:39 +0200
parents 72ccbd36e23c
children 6dc15d4cc17e
comparison
equal deleted inserted replaced
20622:110c7a54586b 20623:2edd668e7784
49 endif 49 endif
50 50
51 [a, b] = validsetargs ("union", a, b, varargin{:}); 51 [a, b] = validsetargs ("union", a, b, varargin{:});
52 52
53 by_rows = nargin == 3; 53 by_rows = nargin == 3;
54 isrowvec = isvector (a) && isvector (b) && isrow (a) && isrow (b); 54 isrowvec = (isrow (a) || isempty (a)) && (isrow (b) || isempty (b));
55 55
56 if (by_rows) 56 if (by_rows)
57 y = [a; b]; 57 y = [a; b];
58 else 58 else
59 y = [a(:); b(:)]; 59 y = [a(:); b(:)];
92 %! b = [1, 2, 3, 4]; 92 %! b = [1, 2, 3, 4];
93 %! [y, ia, ib] = union (a, b.'); 93 %! [y, ia, ib] = union (a, b.');
94 %! assert (y, [1; 2; 3; 4; 5]); 94 %! assert (y, [1; 2; 3; 4; 5]);
95 %! assert (y, sort ([a(ia)'; b(ib)'])); 95 %! assert (y, sort ([a(ia)'; b(ib)']));
96 96
97 ## Test format when input is empty
98 %!assert (union ([],[1,2]), [1,2])
99 %!assert (union ([1,2],[]), [1,2])
100 %!assert (union ([],[1;2]), [1;2])
101 %!assert (union ([1;2],[]), [1;2])
102
97 ## Test common input validation for set routines contained in validsetargs 103 ## Test common input validation for set routines contained in validsetargs
98 %!error <cell array of strings cannot be combined> union ({"a"}, 1) 104 %!error <cell array of strings cannot be combined> union ({"a"}, 1)
99 %!error <A and B must be arrays or cell arrays> union (@sin, 1) 105 %!error <A and B must be arrays or cell arrays> union (@sin, 1)
100 %!error <invalid option: columns> union (1, 2, "columns") 106 %!error <invalid option: columns> union (1, 2, "columns")
101 %!error <cells not supported with "rows"> union ({"a"}, {"b"}, "rows") 107 %!error <cells not supported with "rows"> union ({"a"}, {"b"}, "rows")
102 %!error <A and B must be arrays or cell arrays> union (@sin, 1, "rows") 108 %!error <A and B must be arrays or cell arrays> union (@sin, 1, "rows")
103 %!error <A and B must be 2-dimensional matrices> union (rand(2,2,2), 1, "rows") 109 %!error <A and B must be 2-dimensional matrices> union (rand(2,2,2), 1, "rows")
104 %!error <number of columns in A and B must match> union ([1 2], 1, "rows") 110 %!error <number of columns in A and B must match> union ([1 2], 1, "rows")
105