function y = reshape(x,m,n)
%RESHAPE Change size.
%	RESHAPE(X,M,N) returns the M-by-N matrix whose elements
%	are taken columnwise from X.  An error results if X does
%	not have M*N elements.
%
%	RESHAPE(X,[M N]) also works.

%	Copyright (c) 1984-94 by The MathWorks, Inc.

error(nargchk(2,3,nargin));

if nargin==2,
  if any(size(m)~=[1 2]), error('Size vector must be 1-by-2.'); end
  n = m(2); m = m(1);
end
[mm,nn] = size(x);
if mm*nn ~= m*n
    error('Matrix must have M*N elements.')
end

if ~issparse(x)
   y = zeros(m,n);
   y(:) = x;
else
   [i,j,s] = find(x);
   k = (j-1)*mm + i;
   i = rem(k-1,m) + 1;
   j = (k-i)/m + 1;
   y = sparse(i,j,s,m,n);
end
