function [nb,rows,ncols,last,blocks]=bkbrk(blokmat,print)
% BKBRK	Details of almost block diagonal matrix.
%
%        [nb,rows,ncols,last,blocks]=bkbrk(blokmat[,print])
%
% returns the details of the almost block diagonal matrix contained in
%  blokmat , with  rows  and  last  nb-vectors, and  blocks  a matrix of
% size  sum(rows)-by-ncols .

% C. de Boor / latest change: March 28, '90
% C. de Boor / latest change: February 1, '91: correct misspelling
% Copyright (c) 1990-92 by Carl de Boor and The MathWorks, Inc.

if (blokmat(1)==41), %data type number for the spline block format is 41
   nb=blokmat(2);
   rows=blokmat(2+[1:nb]);
   ncols=blokmat(2+nb+1);
   last=blokmat(3+nb+[1:nb]);
   blocks=zeros(sum(rows),ncols);blocks(:)=blokmat(3+2*nb+[1:sum(rows)*ncols]);
   
elseif (blokmat(1)==40), % data type number for general almost block diagonal
                          % format is 40;
   nb=blokmat(2);
   rows=blokmat(2+[1:nb]);
   cols=blokmat(2+nb+[1:nb]);
   last=blokmat(2+2*nb+[1:nb]);
   row=cumsum([0,rows]);
   ne=sum(rows);ncols=max(cols); 
   len=rows.*cols;
   index=cumsum([2+3*nb len]);
   blocks=zeros(ne,ncols);
   for j=1:nb;
      block=zeros(rows(j),cols(j));
      block(:)=blokmat(index(j)+[1:len(j)]);
      blocks(row(j)+[1:row(j+1)],[1:cols(j)])=block;
   end
else,
   error('argument is not an almost block diagonal matrix'),
end

if(nargin>1), %print out the blocks
   rowsum=cumsum([0 rows]);
   for j=1:nb,
      fprintf(['block ',int2str(j),' has ',int2str(rows(j)),' row(s)\n'])
      disp(blocks(rowsum(j)+[1:rows(j)],:))
     fprintf([' next block is shifted over ',int2str(last(j)),' column(s)\n\n'])
   end
end

