function B = inrmlMat2(A,lev,temp)

% 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
      B(1:2^(end2+i),(2^(end2+i)+1):2^(end1+i)) = temp(i+1).*A(1:2^(end2+i),(2^(3+i)+1):2^(end1+i));
   B((2^(end2+i)+1):2^(end1+i),(2^(end2+i)+1):2^(end1+i)) = temp(i+1).*A(2^(end2+i)+1:2^(end1+i),(2^(end2+i)+1):2^(end1+i));  
   B((2^(end2+i)+1):2^(end1+i),1:2^(end2+i)) = temp(i+1).*A((2^(end2+i)+1):(2^(end1+i)),(1:2^(end2+i)));
end
B(1:2^end1,1:2^end1) = temp(1).*A(1:2^end1,1:2^end1);