function [B,temp] = nrmlMat2(A,lev)

% This function normalizes the input matrix A, so that all the elements
% of the matrix are between -1 and 1.  Each level is normalized individually,
% so information is not lost.

n = length(A);
n2 = log2(n);
end1 = n2 - lev;
end2 = end1 - 1;

for i = 1:lev
   mx1 = max(max(abs(A((1:2^(end2+i)),(2^(end2+i)+1):(2^(end1+i))))));
   mx2 = max(max(abs(A((2^(end2+i)+1):(2^(end1+i)),(2^(end2+i)+1):(2^(end1+i))))));
   mx3 = max(max(abs(A((2^(end2+i)+1):(2^(end1+i)),(1:2^(end2+i))))));
   temp(i+1) = max([mx1 mx2 mx3]);
      B(1:2^(end2+i),(2^(end2+i)+1):2^(end1+i)) = A(1:2^(end2+i),(2^(3+i)+1):2^(end1+i))./temp(i+1);
   B((2^(end2+i)+1):2^(end1+i),(2^(end2+i)+1):2^(end1+i)) = A(2^(end2+i)+1:2^(end1+i),(2^(end2+i)+1):2^(end1+i))./temp(i+1);
   B((2^(end2+i)+1):2^(end1+i),1:2^(end2+i)) = A((2^(end2+i)+1):(2^(end1+i)),(1:2^(end2+i)))./temp(i+1);
end
temp(1) = max(max(abs(A(1:2^end1,1:2^end1))));
B(1:2^end1,1:2^end1) = A(1:2^end1,1:2^end1)./temp(1);