function [trainClasses,trainData]=balanceClasses(trainClasses,trainData,maxPerClass,noClasses)
    if maxPerClass > min(histcounts(trainClasses,noClasses))
        maxPerClass=min(histcounts(trainClasses));
        warning('not enough Data per Class; set maxPerClass to %i', maxPerClass);
    end
    i=1;
    delPerClass=histcounts(trainClasses,noClasses)-maxPerClass;
    delIndex=zeros(sum(delPerClass),1);
    while sum(delPerClass)>0
        r=fix(size(trainClasses,1)*rand(1)+1);
        if delPerClass(trainClasses(r)+1)>0 && all(delIndex~=r)
            delIndex(i)=r;
            i=i+1;
            delPerClass(trainClasses(r)+1)=delPerClass(trainClasses(r)+1)-1;
        end
    end
    trainClasses(delIndex)=[];
    trainData(delIndex,:)=[];
    assert(size(trainClasses,1)>0)
end