% vi:filetype=matlab:tabstop=4
% octave compability blkdiag.m
% By Henrik Holst <holst@kth.se>
% Last changed: Sun 14 Aug 2011 12:46:50 PM CEST 
function B = blkdiag(varargin)
if ismatlab
	B = builtin('blkdiag',varargin{:});
else
	disp('INFO: blkdiag replacement used.')
	any_sparse=false;
	m=zeros(1,nargin);n=zeros(1,nargin);
	for k=1:nargin
		Bn=varargin{k};
		m(k)=size(Bn,1);
		n(k)=size(Bn,2);
		any_sparse=any_sparse|issparse(Bn);
	end
	if any_sparse
		B=sparse(sum(m),sum(n));
	else
		B=zeros(sum(m),sum(n));
	end
	i=0;j=0;
	for k = 1:nargin
		Bn=varargin{k};
		B(i+1:i+m(k),j+1:j+n(k))=Bn;
		i=i+m(k);
		j=j+n(k);
	end
end
