%EUSIPCO 2012
%Imperial College London 
%Andriy Gelman, Jon Onativia and Pier Luigi Dragotti 

function [label, D] = remove_unused_layer(label, D)

number_of_layers = length(D);

l = 1;
while l <= number_of_layers
   
    %Test if the layer exists
    len = length(find(label == l));
   
    %modify label if the length is zero
    if len == 0
        
         for i = l+1:number_of_layers
             label(label == i) = i-1;   
         end
         D = D([1:l-1,l+1:end]);     
         number_of_layers = length(D);
    else
     l = l + 1;
    end
    
end


end