function D = diag(C,k)
% D = diag(C,k)
% Same behaviour as diag for matrices
%
% E.g.: diag(diag(num2cell(randn(4)),2),2)
%
% See also: diag, cell/triu
%
% Written by tommsch, 2018

if(nargin==1); k=0; end;
if(k==inf); k=size(C,1)+1; end;
if(k==-inf); k=-size(C,1)-1; end;
sze=size(C);

if(any(sze==1))
    if(any(sze==0)); D=cell(abs(k)); return; end;
    sze=max(sze);
    D=cell(sze+abs(k));
    if(k>=0)
        for i=1:sze;
            D{i,i+k}=C{i};
        end
    else
        for i=1:sze
            D{i-k,i}=C{i};
        end
    end
else
    if(any(sze)==0); D=cell(0,1); return; end;
    if(k>=0); 
        l=min(min(sze),sze(2)-k);
        if(l<=0); 
            D=cell(0,1);
        else
            D=cell(l,1);
            for i=1:l
                D{i}=C{i,i+k};
            end
        end;   
    else;     
        l=min(min(sze),sze(1)+k);
        if(l<=0); 
            D=cell(0,1);
        else
            D=cell(l,1);
            for i=1:l
                D{i}=C{i-k,i};
            end
        end;
    end;
    
end
   

end