You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: ProjectProbabilitySimplexL1Analytic.m
    path of file: ./repos/StackExchangeCodes/Mathematics/Q2477400
    
    the code of the file until where you have to start completion: function [ vX ] = ProjectProbabilitySimplexL1Analytic( vY )
% ----------------------------------------------------------------------------------------------- %
% [ vX ] = ProjectProbabilitySimplexL1( vY )
%   Solves \arg \min_{x} || x - y ||_{1} s.t. x \in Probability Simplex.
% Input:
%   - vY            -   Input Vector.
%                       Structure: Vector (n x 1).
%                       Type: 'Single' / 'Double'.
%                       Range: (-inf, inf).
% Output:

    
function [ vX ] = ProjectProbabilitySimplexL1Analytic( vY )
% ----------------------------------------------------------------------------------------------- %
% [ vX ] = ProjectProbabilitySimplexL1( vY )
%   Solves \arg \min_{x} || x - y ||_{1} s.t. x \in Probability Simplex.
% Input:
%   - vY            -   Input Vector.
%                       Structure: Vector (n x 1).
%                       Type: 'Single' / 'Double'.
%                       Range: (-inf, inf).
% Output:
%   - vX            -   Solution Vector.
%                       The solution to the optimization problem.
%                       Structure: Vector (n x 1).
%                       Type: 'Single' / 'Double'.
%                       Range: (-inf, inf).
% References
%   1.  See https://math.stackexchange.com/questions/2477400.
% Remarks:
%   1.  The sconcept is this is an element wise optimizatio. so first for
%       all values of 'vY' the closest 'vX' can get is 0. Than for the
%       rest, they either sum to 1, then take them as they are, or they are
%       above or below 1. In any case there is a busget to spread in order
%       to have the sum of 'vX' to be 1.
% TODO:
%   1.  C
% Release Notes:
%   -   1.0.000     13/04/2020  Royi Avital
%       *   First release version.
% ----------------------------------------------------------------------------------------------- %

FALSE   = 0;
TRUE    = 1;

OFF     = 0;
ON      = 1;

numElements = size(vY, 1);

vX = zeros(numElements, 1);

vS = (vY > 0);
vI = find(vS);
sumS = sum(vY(vI));

vX(vI) = vY(vI);

% cover the cases 'vY(vI)' doesn't add up to 1
if(sumS > 1)
    resX = sumS - 1; %<! Residual for Sum of 1
    for ii = vI.'
        minVal = min(vY(ii), resX); %<! Taking as much as we can from the budget but keeping the element non negative
        vX(ii) = vY(ii) - minVal;
        resX = resX - minVal;
        if(resX == 0)
            % The budget is empty, 'sum(vX) == 1'.
            break;
        end