# HG changeset patch # User Carnë Draug # Date 1470431052 -3600 # Node ID 30eefd9ddb48071b40271262d16a4f926fae42e6 # Parent cfa684a0539da407b92c417a9be1c69f9cedcf33 prepad, postpad: do nothing wen input is already of the right size. diff -r cfa684a0539d -r 30eefd9ddb48 scripts/general/postpad.m --- a/scripts/general/postpad.m Fri Aug 05 20:09:09 2016 +0100 +++ b/scripts/general/postpad.m Fri Aug 05 22:04:12 2016 +0100 @@ -73,7 +73,15 @@ d = sz(dim); - if (d >= l) + if (d == l) + ## This optimization makes sense because the function is used to match + ## the length between two vectors not knowing a priori is larger, and + ## allow for: + ## ml = max (numel (v1), numel (v2)); + ## v1 = postpad (v1, ml); + ## v2 = postpad (v2, ml); + y = x; + elseif (d >= l) idx = repmat ({':'}, nd, 1); idx{dim} = 1:l; y = x(idx{:}); @@ -93,6 +101,10 @@ %!assert (postpad ([1;2], 2, 2, 3), reshape ([1;2;2;2], 2, 1, 2)) %!assert (postpad ([1,2], 2, 2, 3), reshape ([1,2,2,2], 1, 2, 2)) +%!assert (postpad ([1 2], 2), [1 2]) +%!assert (postpad ([1; 2], 2), [1; 2]) +%!assert (postpad ([1; 2], 2, 3, 2), [1 3; 2 3]) + %! ## Test with string concatenation (bug #44162) %!assert (postpad ("Octave", 16, "x"), "Octavexxxxxxxxxx") %!assert (postpad ("Octave", 4), "Octa") diff -r cfa684a0539d -r 30eefd9ddb48 scripts/general/prepad.m --- a/scripts/general/prepad.m Fri Aug 05 20:09:09 2016 +0100 +++ b/scripts/general/prepad.m Fri Aug 05 22:04:12 2016 +0100 @@ -73,7 +73,15 @@ d = sz(dim); - if (d >= l) + if (d == l) + ## This optimization makes sense because the function is used to match + ## the length between two vectors not knowing a priori is larger, and + ## allow for: + ## ml = max (numel (v1), numel (v2)); + ## v1 = prepad (v1, ml); + ## v2 = prepad (v2, ml); + y = x; + elseif (d >= l) idx = repmat ({':'}, nd, 1); idx{dim} = d-l+1:d; y = x(idx{:}); @@ -91,6 +99,10 @@ %!assert (prepad ([1,2], 4, 2), [2,2,1,2]) %!assert (prepad ([1;2], 4, 2), [2;2;1;2]) +%!assert (prepad ([1 2], 2), [1 2]) +%!assert (prepad ([1; 2], 2), [1; 2]) +%!assert (prepad ([1; 2], 2, 3, 2), [3 1; 3 2]) + %!assert (prepad ([1,2], 2, 2, 1), [2,2;1,2]) %!assert (prepad ([1,2], 2, 2, 3), reshape ([2,2,1,2], 1, 2, 2))