% function pert = unwrapp(pvec,blk)
%
%   Unwraps the perturbation matrix PVEC (from a lower 
%   bound MU calculation) into the block diagonal form.
%
%
%   See also: MU and UNWRAPD.

function pert = unwrapp(rvpert,blk)
 if nargin < 2
   disp('usage: pert = unwrapp(pvec,blk)')
   return
 end
 [nblk,dum] = size(blk);
 if dum ~= 2
   error('block data incorrect format')
   return
 end
 wp = 0;
 nrp = 0;
 ncp = 0;
 for i=1:nblk
   nrp = nrp + blk(i,1);
   if blk(i,2) == 0
     ncp = ncp + blk(i,1);
     wp = wp+1;
   else
     wp = wp + blk(i,1)*blk(i,2);
     ncp = ncp + blk(i,2);
   end
 end
 [mtype,mrows,mcols,mnum] = minfo(rvpert);
 if mtype == 'vary'
   if mrows == 1 & mcols == wp
     pert = zeros(nrp*mnum,ncp);
     ff = (mnum+1)*nrp;
     pout = 1:nrp:ff;
     poutm1 = pout(2:mnum+1) - 1;
     for i=1:mnum
       pert(pout(i):poutm1(i),1:ncp) = ...
           mkpfrv(rvpert(i,1:mcols),blk,nrp,ncp);
     end
     pert = vpck(pert,rvpert(1:mnum,mcols+1));
   else
     error('incompatible dimensions')
     return
   end
 elseif mtype == 'cons'
   if mrows == 1 & mcols == wp
     pert = mkpfrv(rvpert,blk,nrp,ncp);
   else
     error('incompatible dimensions')
     return
   end
 else
  error('row vector should not be a SYSTEM matrix')
  return
 end
%
% Copyright MUSYN INC 1991,  All Rights Reserved
