function srd=srdMaster(A,T,exact,cv,nIteration,percent,allMix)
% Perform SRD computations on an input matrix A using target T
%==========================================================================
%
% INPUT:
% [1] A: Two options
%     a) An "nrow x ncol" input matrix that the SRD computations 
%        will be applied to. 
%     b) A cell array of size nBlock x 1.  Each cell array component
%        contains an "nrow x ncol" input matrix.  One "uber" matrix will 
%        be created by stacking the matrix in each component such that 
%        the final A matrix will be nBlock*nrow x ncol
%
% [2] T: Target information corresponding to vector of references.  There
%     are two possible inputs:
%     a) One string: choices are 'min', 'mean', 'median' or 'max'. 
%        For example, if T='mean', then target will be mean of A across 
%        columns, i.e. mean(A,2);.  Another example: If T='min', then 
%        target will be min of A across columns, i.e. min(A,[],2).  
%        Note: If A is a cell array, then T will be applied to all blocks.
%     b) Cell array of strings: Each cell array component is a string 
%        as described in a), one for each block.
%     c) Vector of target values.  T is a nBlock*nRow x 1 vector 
%        of user-supplied target values where nRow is the number of samples
%        in a block.
%     d) Cell array of vectors: Each cell array component is a vector
%        as described in c), one for each block.
%
% [3] exact: true or false
%     true if exact SRD distribution is requested; otherwise false; 
%
% [4] cv: 'none', 'classic', 'lmo'
%     - 'none': no cross validation
%     - 'classic': classical cross-validation.  In this case, the input
%       parameter nIteration corresponds to the number of folds.
%     - 'lmo': Acronym for Leave Multiple Out (LMO) cross validation.
%       In this case, for each LMO iteration , a fraction of the samples 
%       (determined by percent) are withheld and will be used as the 
%       validation set.  The number of random splits (or LMO iterations) 
%       is set by nIteration.
%
% [5] nIteration: positive integer
%     - When cv is 'classic', this corresponds to the number of folds.
%     - When cv is 'lmo', this corresponds to the number of LMO iterations.
%     - Not applicable when cv is 'none'.
%     
% [6] percent: 0 <= percent <= 100.  This variable corresponds only to the 
%     cross validation scenario of cv='lmo'.  If so, then percent 
%     corresponds to the percentage of random samples that will be 
%     REMOVED per LMO iteration. 
%
% [7] allMix: true or false.  This variable corresponds only to the 
%     scenario when the number of blocks of A is greater than 1 and when 
%     cv is set to 'lmo'.
%     
%     However, at this stage, it might be worthwhile to discuss both 
%     the classic and lmo CV cases.  Suppose there are n blocks of A 
%     such that there are m rows for each block (the same number of rows 
%     for each block). In the classic CV case, the procedure for 
%     selecting rows is as follows: 
%     a) Find the rows associated with the 1st fold on block 1.  
%        Suppose there are m1 rows associated with this fold on this block.
%     b) Find the exact same m1 rows on the other blocks.  As a result, 
%        there are a total of m1*n rows that are removed, and there are 
%        (m-m1)*p rows remaining that are used for the SRD calculations.
%     c) Repeat steps a) and b) for folds 2,3,...,n. 
%  
%     This same process is used for LMO except that in step a) of the 
%     above process, the selection of the rows is done randomly.  
%     Step b) is the same.  Step c) for the LMO differs in that there are 
%     no folds (just LMO iterations).  
% 
%     When allMIx is set to true, this describes the MLO procedure in the  
%     above paragraph.  When allMix=false, Step b) is modified such that 
%     the exact same m1 rows are not used on the other blocks.  Instead,
%     a random set of m1 rows are used.
%
%     When the number of rows of all blocks are not the same, then 
%     allMix will be set to true.
%     
%--------------------------------------------------------------------------
%
% OUTPUT: 
%
% [1] srd: results struct consisting of the following fields
%     .raw:      nFold x ncol matrix of raw SRD values
%     .norm:     N x ncol matrix of normalized SRD values
%     .x:        N x p matrix of raw SRD values corresponding to the 
%                x-coordinates of the probability density function (PDF) 
%                curves in Figure 1
%     .relFreq:  N x p matrix of PDF values corresponding to the 
%                y-coordinates PDF curves in Figure 1
%     .prob:     N x ncol matrix of probabilities associated with srd.raw
%     .rank:     N x ncol matrix of rankings associated with srd.raw
%     .max:      N x 1 vector, e.g. srd.max(i) is the largest SRD values 
%                possible for the ith fold.
%     .nrow:     N x 1 vector, e.g. srd.nrow(i) is the number of rows used
%                in the ith fold.
%     .display:  srd.display is a struct containing four fields.
%                .x: x-axis of Figure 1
%                .relFreq: y-axis of Figure 1
%                .norm: x-axis of Figure 2
%                .prob: y-axis of Figure 2
%     .data:     struct returned by srdPreProcess.m
%   
% USAGE: see srdMasterDemo.m 
% 
%==========================================================================

%
% Check and parse inputs
%
if nargin<4, cv=[]; end; 
if isempty(cv), cv='none'; end
%
if nargin<5, nIteration=[]; end; 
if isempty(nIteration), nIteration='none'; end
%
if nargin<6, percent=[]; end
if isempty(percent), percent=50; end    
%
if nargin<7, allMix=[]; end
if isempty(allMix), allMix=true; end
    

%
% Preprocess inputs
%
data=srdPreProcess(A,T,exact,cv,nIteration,percent,allMix);
%
% Compute SRD values and get associated statistics 
%
srd=srdCV(data);
%
% Post-process: re-package fields of SRD 
% 
srd=srdPostProcess(srd,data);
%==========================================================================
end

