comparison scripts/general/postpad.m @ 22210:30eefd9ddb48

prepad, postpad: do nothing wen input is already of the right size.
author Carnë Draug <carandraug@octave.org>
date Fri, 05 Aug 2016 22:04:12 +0100
parents 516bb87ea72e
children bac0d6f07a3e
comparison
equal deleted inserted replaced
22209:cfa684a0539d 22210:30eefd9ddb48
71 sz(nd+1:dim) = 1; 71 sz(nd+1:dim) = 1;
72 endif 72 endif
73 73
74 d = sz(dim); 74 d = sz(dim);
75 75
76 if (d >= l) 76 if (d == l)
77 ## This optimization makes sense because the function is used to match
78 ## the length between two vectors not knowing a priori is larger, and
79 ## allow for:
80 ## ml = max (numel (v1), numel (v2));
81 ## v1 = postpad (v1, ml);
82 ## v2 = postpad (v2, ml);
83 y = x;
84 elseif (d >= l)
77 idx = repmat ({':'}, nd, 1); 85 idx = repmat ({':'}, nd, 1);
78 idx{dim} = 1:l; 86 idx{dim} = 1:l;
79 y = x(idx{:}); 87 y = x(idx{:});
80 else 88 else
81 sz(dim) = l - d; 89 sz(dim) = l - d;
91 %!assert (postpad ([1;2], 4, 2), [1;2;2;2]) 99 %!assert (postpad ([1;2], 4, 2), [1;2;2;2])
92 %!assert (postpad ([1,2], 2, 2, 1), [1,2;2,2]) 100 %!assert (postpad ([1,2], 2, 2, 1), [1,2;2,2])
93 %!assert (postpad ([1;2], 2, 2, 3), reshape ([1;2;2;2], 2, 1, 2)) 101 %!assert (postpad ([1;2], 2, 2, 3), reshape ([1;2;2;2], 2, 1, 2))
94 %!assert (postpad ([1,2], 2, 2, 3), reshape ([1,2,2,2], 1, 2, 2)) 102 %!assert (postpad ([1,2], 2, 2, 3), reshape ([1,2,2,2], 1, 2, 2))
95 103
104 %!assert (postpad ([1 2], 2), [1 2])
105 %!assert (postpad ([1; 2], 2), [1; 2])
106 %!assert (postpad ([1; 2], 2, 3, 2), [1 3; 2 3])
107
96 %! ## Test with string concatenation (bug #44162) 108 %! ## Test with string concatenation (bug #44162)
97 %!assert (postpad ("Octave", 16, "x"), "Octavexxxxxxxxxx") 109 %!assert (postpad ("Octave", 16, "x"), "Octavexxxxxxxxxx")
98 %!assert (postpad ("Octave", 4), "Octa") 110 %!assert (postpad ("Octave", 4), "Octa")
99 111
100 %!error postpad () 112 %!error postpad ()