function [coeffs]=kFoldRidge(kin,data,k,ridgeParams)
    if any(size(ridgeParams)>1)
        
        correlationPerParam=zeros(size(ridgeParams));
        
        for param=ridgeParams
            
            randomMapping=transpose(randperm(size(data,1)));
            correlations=zeros(k,1);

            parfor i=1:k
                trainData=data(mod(randomMapping,k)+1~=i,:,:);
                testData=data(mod(randomMapping,k)+1==i,:,:);
                trainKin=kin(mod(randomMapping,k)+1~=i);
                testKin=kin(mod(randomMapping,k)+1==i);
                
                coeffs=ridge(trainData,trainKin,param,0);
                correlations(i)=ridgeCorrelation(testKin,testData,coeffs);
            end

           correlationPerParam(ridgeParams==param)=mean(correlations);
        end
        [~,maxRidgeParamIndex]=max(correlationPerParam);
        coeffs=ridge(data,kin,ridgeParams(maxRidgeParamIndex),0);
        
        
    else
        coeffs=ridge(data,kin,ridgeParams(1),0);
    end
end