function newDimension = subspace_dimension(S, energyLossThreshold)
% Determines the dimension of a group given its singular values and a threshold on the MSE of the projection.
% inputs: 
%  S: a vector of singular values
%  energyLossThreshold: a threshold on the MSE of the projection.
% 
% outputs:
%   newDimension: The dimension of the smallest subspace with acceptable MSE.
%
oldDimension = length(S);
    if (oldDimension<1)
        newDimension=0;
    else
        sumEnergyLoss=0; 
    for index=oldDimension:-1:1
        sumEnergyLoss=sumEnergyLoss+S(index)^2;
        if sumEnergyLoss>energyLossThreshold
            break;
        end;
    end;
    newDimension=index;
end;
