function [ Dist ] = AffHullDist( Xi, Xj)
%AffHullDist Finds shortest distance between two affine hulls numerically
%using CVX
%   This function was initially the same as Hull Distance(where the values
%   of L and U define what type of hull the feature vectors describe).
%   In the case of a full affine hull: L=-inf , U=+inf. 
%   In other words, the bounds are inactive. They were removed to make this
%   function more efficient for CVX to solve.
%
%   Inputs:
%   Xi: Feature vector matrix of one set
%   Xj: Feature vector matrix of other set
%
%   Outputs:
%   Dist: The minimal distance between the selected hulls of the input features


size_ai=size(Xi,2);
size_aj=size(Xj,2);

cvx_begin quiet
% Variables: ai, aj
    variable ai(size_ai,1)
    variable aj(size_aj,1)
    
    minimize norm(Xi*ai - Xj*aj)
    
    subject to
        sum(ai)==1;
        sum(aj)==1;
cvx_end

Dist=cvx_optval;

end

