%说明:下面是我自己写的matlab代码,其实matlab有自带的交叉验证代码crossvalind,见Chunhou Zheng师兄的Metasample Based Sparse Representation for Tumor提供的代码
%说明:Main_gene10FOLD_1.m有,用法非常简单,和自己编写的实现的是同样的功能
% 10-fold cross validation
%This code is written by Gui Jie in the afternoon 2009/06/08.
%If you have find some bugs in the codes, feel free to contract me
% Reference:
%1. Algorithm 2 of "Shuiwang Ji and Jieping Ye. Generalized Linear Discriminant Analysis: A
% Unified Framework and Efficient Model Selection. IEEE Transactions on Neural Networks.
% Vol. 19, No. 10, pp. 1768-1782, 2008."
%2.Foot note 4 of "F.Wang,et al.,marginFace:A novel face recognition method by average neighborhood
% margin maximization,Pattern Recognition (2009)"
v=10;% If v=4,it means 4-fold cross validation.
step=floor(size(fea_train,1)/v);
for j =1:v
if j~= v
startpoint=(j-1)*step+1;
endpoint=(j)*step;
else
startpoint=(j-1)*step+1;
endpoint=size(fea_train,1);
end
cv_p=startpoint:endpoint; %%%% test set position
%%%%%%%%%%%%%% test set
Test_data=fea_train(cv_p,:);
Test_lab=gnd_train(cv_p,:); %%%%label
%%%%%%%%%%%%%% training data
Train_data=fea_train;
Train_data(cv_p,:)='';
Train_lab=gnd_train;
Train_lab(cv_p,:)='';
end