function A=rescale(A,lower,upper)
%RESCALE Rescales matrix.
%
%A=rescale(A[,lower[,upper]])
%returns matrix A rescaled to lower<= Aij <= upper
%lower,upper defaults do 0,1.

% v1.0  93-04-05 (?). Niclas Borlin, niclas@cs.umu.se.
% v1.1  95-07-06. Added default values.
% v1.2  95-11-22. Corrected 'all values equal' bug.

if (nargin<2)
	lower=0;
end

if (nargin<3)
	upper=1;
end

mn=min(min(A));
mx=max(max(A));

if (mn==mx)
	A=ones(size(A))*(lower+upper)/2;
else
	A=(A-mn)/(mx-mn)*(upper-lower)+lower;
end
