home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts / general / reshape.m < prev    next >
Encoding:
Text File  |  1999-11-21  |  2.5 KB  |  90 lines

  1. ## Copyright (C) 1996, 1997 John W. Eaton
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## -*- texinfo -*-
  21. ## @deftypefn {Function File} {} reshape (@var{a}, @var{m}, @var{n})
  22. ## Return a matrix with @var{m} rows and @var{n} columns whose elements are
  23. ## taken from the matrix @var{a}.  To decide how to order the elements,
  24. ## Octave pretends that the elements of a matrix are stored in column-major
  25. ## order (like Fortran arrays are stored).
  26. ## 
  27. ## For example,
  28. ## 
  29. ## @example
  30. ## @group
  31. ## reshape ([1, 2, 3, 4], 2, 2)
  32. ##      @result{}  1  3
  33. ##          2  4
  34. ## @end group
  35. ## @end example
  36. ## 
  37. ## If the variable @code{do_fortran_indexing} is nonzero, the
  38. ## @code{reshape} function is equivalent to
  39. ## 
  40. ## @example
  41. ## @group
  42. ## retval = zeros (m, n);
  43. ## retval (:) = a;
  44. ## @end group
  45. ## @end example
  46. ## 
  47. ## @noindent
  48. ## but it is somewhat less cryptic to use @code{reshape} instead of the
  49. ## colon operator.  Note that the total number of elements in the original
  50. ## matrix must match the total number of elements in the new matrix.
  51. ## @end deftypefn
  52.  
  53. ## See also: `:', do_fortran_indexing
  54.  
  55. ## Author: jwe
  56.  
  57. function retval = reshape (a, m, n)
  58.  
  59.   if (nargin == 2 && prod (size (m)) == 2)
  60.     n = m(2);
  61.     m = m(1);
  62.     nargin = 3;
  63.   endif
  64.  
  65.   if (nargin == 3)
  66.     [nr, nc] = size (a);
  67.     if (nr * nc == m * n)
  68.       dfi = do_fortran_indexing;
  69.       istno = implicit_str_to_num_ok;
  70.       unwind_protect
  71.         do_fortran_indexing = 1;
  72.     implicit_str_to_num_ok = 1;
  73.         retval = zeros (m, n);
  74.         retval (:) = a;
  75.     if (isstr (a))
  76.       retval = setstr (retval);
  77.     endif
  78.       unwind_protect_cleanup
  79.         do_fortran_indexing = dfi;
  80.     implicit_str_to_num_ok = istno;
  81.       end_unwind_protect
  82.     else
  83.       error ("reshape: sizes must match");
  84.     endif
  85.   else
  86.     usage ("reshape (a, m, n) or reshape (a, size (b))");
  87.   endif
  88.  
  89. endfunction
  90.