Mercurial > hg > octave-avbm
changeset 7652:b5731e43283a
ismember: correctly size idx output for empty args
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 26 Mar 2008 23:01:16 -0400 |
parents | 443a8f5a50fd |
children | d9eb2aec6d84 |
files | scripts/ChangeLog scripts/general/logical.m scripts/set/ismember.m |
diffstat | 3 files changed, 52 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,5 +1,10 @@ 2008-03-26 John W. Eaton <jwe@octave.org> + * set/ismember.m: Set size of idx output correctly for empty args. + New tests. + + * general/logical.m: Correctly handle empty args. New tests. + * control/hinf/h2syn.m, general/__splinen__.m, general/gradient.m, geometry/inpolygon.m, geometry/trimesh.m, geometry/triplot.m, image/imagesc.m, io/csvread.m, io/csvwrite.m,
--- a/scripts/general/logical.m +++ b/scripts/general/logical.m @@ -37,8 +37,10 @@ function y = logical (x) if (nargin == 1) - if (islogical (x) || isempty (x)) + if (islogical (x)) y = x; + elseif (isempty (x)) + y = zeros (size (x), "logical"); elseif (isnumeric (x)) y = x != 0; else @@ -49,3 +51,12 @@ endif endfunction + +%!assert (logical ([]), zeros ([0, 0], "logical")); +%!assert (logical (zeros (2, 0)), zeros ([2, 0], "logical")); +%!assert (logical (0), false); +%!assert (logical (13), true); +%!assert (logical (-13), true); +%!assert (logical (int8 (13)), true); +%!assert (logical (int8 (-13)), true); +%!assert (logical ([-1, 0, 1, NaN, Inf]), [-1, 0, 1, NaN, Inf] != 0);
--- a/scripts/set/ismember.m +++ b/scripts/set/ismember.m @@ -119,7 +119,7 @@ ## Do the actual work. if (isempty (a) || isempty (s)) tf = zeros (size (a), "logical"); - a_idx = []; + a_idx = zeros (size (a)); elseif (numel (s) == 1) tf = (a == s); a_idx = double (tf); @@ -165,12 +165,12 @@ [v, p] = sort ([s(2:lt)(:); a(:)]); idx(p) = cumsum (p <= lt-1) + 1; idx = idx(lt:end); - tf = (a == reshape (s (idx), size (a))); - a_idx = zeros (size(tf)); + tf = (a == reshape (s(idx), size (a))); + a_idx = zeros (size (tf)); a_idx(tf) = sidx(idx(tf)); endif ## Resize result to the original size of 'a' - size_a = size(a); + size_a = size (a); tf = reshape (tf, size_a); a_idx = reshape (a_idx, size_a); endif @@ -200,7 +200,7 @@ ## Do the actual work if (isempty (a) || isempty (s)) tf = zeros (size (a), "logical"); - a_idx = []; + a_idx = zeros (size (a)); elseif (numel (s) == 1) tf = strcmp (a, s); a_idx = double (tf); @@ -257,6 +257,36 @@ %!assert (ismember ("1", "0123456789."), true); %!test +%! [result, a_idx] = ismember ([1, 2], []); +%! assert (result, logical ([0, 0])) +%! assert (a_idx, [0, 0]); + +%!test +%! [result, a_idx] = ismember ([], [1, 2]); +%! assert (result, logical ([])) +%! assert (a_idx, []); + +%!test +%! [result, a_idx] = ismember ({'a', 'b'}, ''); +%! assert (result, logical ([0, 0])) +%! assert (a_idx, [0, 0]); + +%!test +%! [result, a_idx] = ismember ({'a', 'b'}, {}); +%! assert (result, logical ([0, 0])) +%! assert (a_idx, [0, 0]); + +%!test +%! [result, a_idx] = ismember ('', {'a', 'b'}); +%! assert (result, false) +%! assert (a_idx, 0); + +%!test +%! [result, a_idx] = ismember ({}, {'a', 'b'}); +%! assert (result, logical ([])) +%! assert (a_idx, []); + +%!test %! [result, a_idx] = ismember([1 2 3 4 5], [3]); %! assert (all (result == logical ([0 0 1 0 0])) && all (a_idx == [0 0 1 0 0]));