# HG changeset patch # User Ben Abbott # Date 1348346899 14400 # Node ID 32fd3137805252249604aa92dc1fe569f0969797 # Parent 13ffb3130b2fcc7ed8d5c92f1332c355ff8535d8 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. diff -r 13ffb3130b2f -r 32fd31378052 scripts/general/repmat.m --- a/scripts/general/repmat.m Fri Sep 21 22:44:10 2012 -0700 +++ b/scripts/general/repmat.m Sat Sep 22 16:48:19 2012 -0400 @@ -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))