Mercurial > hg > octave-terminal
changeset 15436:32fd31378052
Additional changes to repmat.m (e1f59fd57756).
Bug # 37390.
* scripts/general/repmat.m: Changeset e1f59fd57756 did not handle empties
in all situations correctly. Also, ML allows the inputs to be matrices.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Sat, 22 Sep 2012 16:48:19 -0400 |
parents | 13ffb3130b2f |
children | e39a51e0d54b |
files | scripts/general/repmat.m |
diffstat | 1 files changed, 48 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/general/repmat.m +++ b/scripts/general/repmat.m @@ -40,33 +40,48 @@ endif if (nargin == 3) - if (! (numel (m) < 2 && numel (n) < 2)) - error ("repmat: with 3 arguments M and N must be scalar"); + if (! isempty (m) && isempty (n)) + m = m(:).'; + n = 1; + elseif (isempty (m) && ! isempty (n)) + m = n(:).'; + n = 1; + elseif (isempty (m) && isempty (n)) + m = n = 1; + else + if (all (size (m) > 1)) + m = m(:,1); + if (numel (m) < 3) + n = n(end); + else + n = []; + endif + endif + if (all (size (n) > 1)) + n = n(:,1); + endif + m = m(:).'; + n = n(:).'; endif - if (isempty (m)) - m = 1; - endif - if (isempty (n)) - n = 1; - endif - idx = [m, n]; else if (isempty (m)) - m = 1; - endif - if (isscalar (m)) - idx = [m, m]; + m = n = 1; + elseif (isscalar (m)) n = m; - elseif (isvector (m) && length (m) > 1) - ## Ensure that we have a row vector - idx = m(:).'; + elseif (ndims (m) > 2) + error ("repmat: M has more than 2 dimensions") + elseif (all (size (m) > 1)) + m = m(:,1).'; + n = []; else - error ("repmat: invalid dimensional argument"); + m = m(:).'; + n = []; endif endif + idx = [m, n]; if (all (idx < 0)) - error ("repmat: invalid dimensions"); + error ("repmat: invalid dimensions") else idx = max (idx, 0); endif @@ -110,11 +125,24 @@ endfunction +# Tests for ML compatibility +%!shared x +%! x = [1 2 3]; +%!assert (repmat (x, [3, 1]), repmat (x, 3, [])) +%!assert (repmat (x, [3, 1]), repmat (x, [], 3)) +%!assert (repmat (x, [1, 3]), repmat (x, [], [1, 3])) +%!assert (repmat (x, [1, 3]), repmat (x, [1, 3], [])) +%!assert (repmat (x, [1 3]), repmat (x, [1 3; 3 3])) +%!assert (repmat (x, [1 1 2]), repmat (x, [1 1; 1 3; 2 1])) +%!assert (repmat (x, [1 3; 1 3], [1; 3]), repmat (x, [1 1 3])) +%!assert (repmat (x, [1 1], 4), repmat (x, [1 3; 1 3], [1; 4])) +%!assert (repmat (x, [1 1], 4), repmat (x, [1 3; 1 3], [1 2; 3 4])) +%!assert (repmat (x, [1 1], 4), repmat (x, [1 1 4])); +%!assert (repmat (x, [1 1], 4), repmat (x, 1, [1 4])); + # Test various methods of providing size parameters %!shared x %! x = [1 2;3 4]; -%!assert (repmat (x, 1, []), repmat (x, 1)) -%!assert (repmat (x, [], 3), repmat (x, [1, 3])) %!assert (repmat (x, [1 1]), repmat (x, 1)) %!assert (repmat (x, [3 3]), repmat (x, 3)) %!assert (repmat (x, [1 1]), repmat (x, 1, 1))