diff scripts/general/fliplr.m @ 19318:995df67fc912

Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip. * fliplr.m, flipud.m: add support for N-dimensional arrays by making use of flip(). Added new tests for ND arrays and defaults. * flipdim.m: deprecate in favour of new function flip() which has exactly the same syntax and is part of Matlab since R2014a. * flip.m: new function copied from flipdim. Added tests for ND arrays and defaults. * matrix.txi: replace flipdim DOCSTRINg with flip. * rot90.m, rotdim.m, del2.m: replace flipdim() with flip() * NEWS: note deprecation of flip(), new function flipdim(), and ND support for flipud() and fliplr().
author Carnë Draug <carandraug+dev@gmail.com>
date Sun, 21 Sep 2014 18:49:08 +0100
parents d63878346099
children 76baa2d10abb
line wrap: on
line diff
--- a/scripts/general/fliplr.m
+++ b/scripts/general/fliplr.m
@@ -18,6 +18,8 @@
 
 ## -*- texinfo -*-
 ## @deftypefn {Function File} {} fliplr (@var{x})
+## Flip array left to right.
+##
 ## Return a copy of @var{x} with the order of the columns reversed.  In
 ## other words, @var{x} is flipped left-to-right about a vertical axis.  For
 ## example:
@@ -30,9 +32,7 @@
 ## @end group
 ## @end example
 ##
-## Note that @code{fliplr} only works with 2-D arrays.  To flip N-D arrays
-## use @code{flipdim} instead.
-## @seealso{flipud, flipdim, rot90, rotdim}
+## @seealso{flipud, rot90, rotdim}
 ## @end deftypefn
 
 ## Author: jwe
@@ -42,13 +42,7 @@
   if (nargin != 1)
     print_usage ();
   endif
-
-  if (ndims (x) > 2)
-    error ("fliplr: Only works with 2-D arrays");
-  endif
-
-  nc = columns (x);
-  y = x (:, nc:-1:1);
+  y = flip (x, 2);
 
 endfunction
 
@@ -56,6 +50,29 @@
 %!assert (fliplr ([1, 2; 3, 4]), [2, 1; 4, 3])
 %!assert (fliplr ([1, 2; 3, 4; 5, 6]), [2, 1; 4, 3; 6, 5])
 %!assert (fliplr ([1, 2, 3; 4, 5, 6]), [3, 2, 1; 6, 5, 4])
+%!assert (fliplr ([1 2 3].'), [1 2 3].')
+
+## Test NDArrays
+%!test
+%! a(:,:,1) = [ 1  2;  3  4;  5  6];
+%! a(:,:,2) = [ 7  8;  9 10; 11 12];
+%! b(:,:,1) = [ 2  1;  4  3;  6  5];
+%! b(:,:,2) = [ 8  7; 10  9; 12 11];
+%! assert (fliplr (a), b)
+
+## Test NDArray with singleton dimensions
+%!test
+%! a(:,:,:,1) = [ 1  2;  3  4;  5  6];
+%! a(:,:,:,2) = [ 7  8;  9 10; 11 12];
+%! b(:,:,:,1) = [ 2  1;  4  3;  6  5];
+%! b(:,:,:,2) = [ 8  7; 10  9; 12 11];
+%! assert (fliplr (a), b)
+
+## Test for 1 row, i.e., returns the same
+%!test
+%! a(:,1,:,1) = [ 1  2  3  4];
+%! a(:,1,:,2) = [ 5  6  7  8];
+%! assert (fliplr (a), a)
 
 %!error fliplr()
 %!error fliplr (1, 2)