function [X_trn, y_trn, X_val, y_val, X_chk, y_chk] = split_data(data, ratios, seed)
%SPLITSET Splits the data-set to train, eval,check data
%   Randomly split the data set according to ratios   
%

    % Argument check and default values
    if nargin < 2 || isempty(ratios), ratios = [0.6 0.2 0.2]; end
    if nargin == 2, rng(42,  'twister'); end
    if nargin > 2,  rng(seed,'twister'); end

    N = size(data,1);           % Scramble the set
    idx = randperm(N);

    Ntrn = round(ratios(1)*N);  % Split positions
    Nval = round(ratios(2)*N);
    Itrn = idx(1:Ntrn);
    Ival = idx(Ntrn+1:Ntrn+Nval);
    Ichk = idx(Ntrn+Nval+1:end);

    % Pick the data (in X and y)
    X_trn = data(Itrn, 1:end-1);  y_trn = data(Itrn, end);
    X_val = data(Ival, 1:end-1);  y_val = data(Ival, end);
    X_chk = data(Ichk, 1:end-1);  y_chk = data(Ichk, end);

end