function DV = alphaPlanesExhaustive(T2FS, numPlanes);

% DV = alphaPlanesExhaustive(T2FS, numPlanes);
% defuzzifies a type-2 fuzzy set to give an x-value
% firstly slices the type-2 fuzzy set into numSlices alpha-planes,
% secondly applies the exhaustive method to each alpha-plane to give the defuzzified value,
% thirdly assembles the defuzzified values into a type-1 set (TRS)'
% and fourthly defuzzifies the type-1 set to give the defuzzified value of
% the original type-2 set.
%
% SG 171209

degreeDisc = 1/(numPlanes-1); % degree of discretisation of z-axis
sizeT2FS = size(T2FS);
domainSize = sizeT2FS(2); % number of x slices (columns)
coDomainSize = sizeT2FS(1); % number of y slices (rows)

resultsPairList = []; % initialisation of TRS

xVector = linspace(0,1,domainSize); % initialise x-axis

nullES = zeros(1,domainSize)+0.0001; % initialise x-axis
totalPlanes = 0; % initialise total of planes defuzzified
totalDV = 0; % initialise total of planes' defuzzified values

for planesCounter = 1:numPlanes
    planeHeight = (planesCounter-1)*degreeDisc; % height of alpha-plane
      
    lowerES = zeros(1, domainSize); % initialisation of a list of y co-ords defining the ES of the lower MF
    upperES = zeros(1, domainSize); % initialisation of a list of y co-ords defining the ES of the upper MF
    
    for columnCounter = 1:domainSize % work through all the columns of the type-2 fuzzy set
        for rowCounter = 1:coDomainSize % work through all the rows of the type-2 fuzzy set
            if T2FS(rowCounter,columnCounter) >= 0.0001
                if T2FS(rowCounter,columnCounter) >= planeHeight
                    lowerES(columnCounter) = rowCounter; % y co-ord of lower MF
                    break
                end
            end
        end
    end
    %lowerES
    for columnCounter = 1:domainSize % work through all the columns of the type-2 fuzzy set
        for rowCounter = 1:coDomainSize % work through all the rows of the type-2 fuzzy set
            if T2FS(rowCounter,columnCounter) >= 0.0001
                if T2FS(rowCounter,columnCounter) >= planeHeight
                    upperES(columnCounter) = rowCounter; % y co-ord of upper MF
                end
            end
        end
    end
    %upperES    

    totalPlanes = totalPlanes+1;
    midGrades = (upperES + lowerES)/2;
    if midGrades < nullES
        break
    end

    TRS = 0;
    totalEmbSets = 2.^domainSize;

    for count = 0:totalEmbSets-1
        E = zeros(1,domainSize);
        countBinary = dec2bin((2.^domainSize)+count);
        moduli = mod(countBinary,2);
        for index = 1:domainSize
            if moduli(index+1) == 0
                E(index) = lowerES(index);
            elseif moduli(index+1) == 1
                E(index) = upperES(index);
            end
        end
        dv = defuzz(xVector,E,'centroid');
        TRS = TRS + dv;
    end

    planeDV = TRS/totalEmbSets;
    
    resultsPair = [planeDV; planeHeight]; % pair of (x,z) co-ordinates
    resultsPairList = [resultsPairList resultsPair]; % matrix comprising list of (x,z) co-ordinates
end

DV = defuzz(resultsPairList(1,:), resultsPairList(2,:), 'centroid'); % defuzzify the type-1 fuzzy set 

fprintf('\n')
fprintf('Number of alpha-planes: %0.0f \n', numPlanes);
fprintf('Number of alpha-planes used: %0.0f \n', totalPlanes);
fprintf('Defuzzified value: %0.10f \n', DV);
