function retval = reshape2(a, m, n)
  ## usage: reshape2(a, m, n)
  ## Simple wrapper around reshape that guesses the value of one (but
  ## not both) dimension of the new array.  If m (or n) is zero, then the
  ## value of m (or n) is inferred from the size of the matrix a.
  ##
  ## Example: reshape2(linspace (10, 100, 10), 0, 5)
  if ( (m ==0 ) && (n==0))
    usage("one of m or n must be non-zero.")
  endif
  if (nargin != 3)
    usage ("reshape2 (a, m, n)");
  else
    [nr, nc] = size (a);
    nelems = nr*nc;
    if (m==0)
      m = floor(nelems/n);
    elseif (n==0)
      n = floor(nelems/m);
    endif

    ## Check if new dimensions of array are valid.
    if ( m*n != nelems) 
      error("Matrix cannot be reshaped: nelems %d, m %d n %d\n", nelems, m, n);
    else
      retval = reshape(a, m, n);
    endif
    
  endif
endfunction
