function [M DOF] = make_blocking(nu,N,ibtype)
%% Inputblocking function to construct inputblocking matrix
% Size of matrix M and insert of zeros
nibt = size(ibtype,2);      %number of input blocking types
neq = 0;
sum = 0;
minus = 0;
DOF = 0;

% size of matrix M
for s = 1:nibt
    sum = sum + ibtype(s); 
    pom = ibtype(s);
    if pom == 1
        neq = neq + pom;    %number of equations different if ibtype = 1 (free)
    else
    neq = neq + (pom -1);   %number of equations
    end
end

lastibtype = ibtype(s);     %last input blocking type
if lastibtype == 1

    pom2 = 1;
else
    
    pom2 = lastibtype-1;        %last input blocking type -1 to get how many rows it takes
end

% Zeros
if sum < N
times_neq = (N-sum)/lastibtype;  
neq = neq + (pom2*times_neq);           %number of equations if sum of ib types < N
M = cell(neq,N);            %cell array M
    for r=1:neq
        for t=1:N
            M{r,t}=zeros(nu);   %inserting zeros 
        end
    end
    

else    
    for r=1:neq
        for t=1:N
            M{r,t}=zeros(nu);   %inserting zeros 
        end
    end

end

% calculation Degrees of freedom DOF

for q = 1:nibt                   %for every input blocking type
minus = minus + (ibtype(q)-1);   %what we should subtract from N to get DOF
end
if sum < N
    minus = minus + ((ibtype(q)-1)*times_neq); %if sum of IB types is < N
end

DOF = N - minus;


% fill matrix M

if ibtype(1) == 1 & nibt == 1     %do nothing if ibtype = [1]

    
M = cell2mat(M);
Msize = size(M);

else    
    
Mplus = eye(nu);        %eye matrix with size of number of inputs
Mminus = -eye(nu);      %-eye matrix with size of number of inputs
ii = 1;
jj = 1;
i = 0;
j = 0;

% deffine matrix M
for s = 1:nibt 

    ne = ibtype(s)-1;
    if ne == 0
            for k = 1:N
            M{ii,k} = zeros(nu);
            end
            if s ~= 1
               j = j+1;
               i = i+1;

            end
                        
     elseif  ne == 1
            M{ii,jj} = Mplus;
            M{ii,jj+1} = Mminus;
            if s == 1
                j = j+1;
            else
                i = i+1;
                j = j+2;
            end
     else

            M{ii,jj} = Mplus;
            M{ii,jj+1} = Mminus;
            for i=ii:(ii+ne-2)
                for j=jj:(jj+ne-1)
                    M{i+1,j+1} = M{i,j};
                end
            end

    end
    ii = i+2;
    jj = j+2;
end

% finishing the M matrix if it is less input blocking types as inputs

while jj <= (N-1)

    ne = ibtype(s)-1;
    if ne == 0
            for k = 1:N
            M{ii,k} = zeros(nu);
            end
            if s ~= 1
               j = j+1;
               i = i+1;

            end

     elseif  ne == 1
            M{ii,jj} = Mplus;
            M{ii,jj+1} = Mminus;
                i = i+1;
                j = j+2;
     else

            M{ii,jj} = Mplus;
            M{ii,jj+1} = Mminus;
            for i=ii:(ii+ne-2)
                for j=jj:(jj+ne-1)
                    M{i+1,j+1} = M{i,j};
                end
            end

    end
    ii = i+2;
    jj = j+2;
end

M = cell2mat(M);
Msize = size(M);

end
