% cvpartition_demo.m
% Explain the use of the function 
% Dr. W.D.Pan, UAH

clear all;
close all;

rng(1);
N = 15;
X1 = -3+randn(1,N);
C1 = ones(1,N);  % labels

X2 = 3+randn(1,N);
C2 = 2*ones(1,N);

X = [X1 X2];
C = [C1 C2];

cv = cvpartition(C, 'KFold', 5)
C(cv.test(1))
C(cv.test(2))

cv = cvpartition(C, 'KFold', 5, 'Stratify', false)
C(cv.test(1))
C(cv.test(2))

cv = cvpartition(C, 'HoldOut', 0.2)
(cv.test)'
C(cv.test)
X(cv.test)

cv = cvpartition(C, 'HoldOut', 0.2, 'Stratify', false)
C(cv.test)

% Split into Training, Validation, Test datasets
% Total 30 samples: 
% 20% (6 samples) hold-out for final testing
% Remaning 80% (24 samples) split into 75%/25% for training/validation
% equivalent to 4-fold cross validation

cv = cvpartition(C, 'HoldOut', 0.2)

cv.test+cv.training % all 1 vector

final_test_label = C(cv.test)
final_test_data = X(cv.test)

% Remaining data samples and corresonding labels used for training and
% validation (TV)
X_TV = X(cv.training);
C_TV = C(cv.training);

cv_TV = cvpartition(C_TV, 'KFold', 4)
% For training (1st fold, etc.)
X_V_1 = X_TV(cv_TV.training(1));
C_V_1 = C_TV(cv_TV.training(1));

% For validation 
X_T_1 = X_TV(cv_TV.test(1));
C_T_1 = C_TV(cv_TV.test(1));

% Even smaller dataset 
clear all;
N = 2;
X1 = -3+randn(1,N);
C1 = ones(1,N);  % labels

X2 = 3+randn(1,N);
C2 = 2*ones(1,N);

X = [X1 X2];
C = [C1 C2];

cv = cvpartition(C, 'LeaveOut')
[cv.test(1) cv.test(2) cv.test(3) cv.test(4)]

cv = cvpartition(C,'resubstitution')
cv.test




