% Subroutine of rcompletePADM2.m

function x = invmap(x,beta,q,m,n,i,j)

L = min(i,j);

for r = 1:L-1
  if q(1,n*(r-1)+1) ~= 0
    x(:,n*(r-1)+1:n*r) = x(:,n*(r-1)+1:n*r)/(1+beta*r);
  else
    x(:,n*(r-1)+1:n*r) = x(:,n*(r-1)+1:n*r)/(beta*r);
  end
  if q(1,n*(i+j-1-r)+1) ~= 0
    x(:,n*(i+j-1-r)+1:n*(i+j-r)) = x(:,n*(i+j-1-r)+1:n*(i+j-r))/(1+beta*r);
  else
    x(:,n*(i+j-1-r)+1:n*(i+j-r)) = x(:,n*(i+j-1-r)+1:n*(i+j-r))/(beta*r);
  end
end

r = L;
for s = L:i+j-L
  if q(1,n*(s-1)+1) ~= 0
    x(:,n*(s-1)+1:n*s) = x(:,n*(s-1)+1:n*s)/(1+beta*r);
  else
    x(:,n*(s-1)+1:n*s) = x(:,n*(s-1)+1:n*s)/(beta*r);
  end
end

%y = x;