function main_10_5fold_MULDA
% multiple feature data set, the label of handwritten '0' is 1.

num_view = 2;
Label_multiview = cell(num_view,1);
accurate = zeros(1,num_view+1);
accurate_M = zeros(1,num_view+2);
accurate_MU = zeros(1,num_view+2);
kn=5;% the neighbor number of KNN
view{1,1} = 'fou'; % 200 * 76
view{2,1} = 'kar'; % 200 * 64
view{3,1} = 'fac'; % 200 * 216
view{4,1} = 'pix'; % 200 * 240
view{5,1} = 'zer'; % 200 * 47
view{6,1} = 'mor'; % 200 * 6

lamda = [1,5,10,15,20];
%id_lam = ['1','3','5','7','9'];
a = 1; %a=1 represents the results of MULDA and MLDA. a=3 represents the results of MULDA-m and MLDA-m. 
aa = num2str(a);

for v1 = 1:5
    for v2 = v1+1:6
       
        
%X_v1 = load(['./multifeat/mfeat-',view{v1,1},'.txt']); 
%X_v2 = load(['./multifeat/mfeat-',view{v2,1},'.txt']);
X_v1 = load(['./multifeat/',view{v1,1},'.scaled.txt']);
X_v2 = load(['./multifeat/',view{v2,1},'.scaled.txt']);
Y = [repmat(1,1,200) repmat(2,1,200) repmat(3,1,200) repmat(4,1,200) repmat(5,1,200) repmat(6,1,200) repmat(7,1,200) repmat(8,1,200) repmat(9,1,200) repmat(10,1,200)];

num_class = 10;

for iter = 1:10   %repeat 10 times random experiments
    X_train_v1 = [];
    X_train_v2 = [];
    Y_train = []; 

    fprintf('experiment between view %s and view %s in iter %d\n',[view{v1,1}],[view{v2,1}],iter);
    rand = randperm(200);
    idx_train = rand(1:100);
    
for i = 1:num_class
    X_train_v1 = [X_train_v1' X_v1(200*(i-1)+idx_train,:)']';
    X_train_v2 = [X_train_v2' X_v2(200*(i-1)+idx_train,:)']';
    Y_train = [Y_train Y(:,200*(i-1)+idx_train)];
end

ix = 1:100;
indices = crossvalind('Kfold',ix',5);  %5-fold validation lamda = {1,3,5,7,9}
for val = 1:5  %5-fold validation
    
    X_train_vali_v1 = []; 
    X_train_vali_v2 = []; 
    X_test_vali_v1 = []; 
    X_test_vali_v2 = [];
    Y_train_vali = []; 
    Y_test_vali = []; 

    % rand = randperm(200);
    test_vali = (indices == val);
    train_vali = ~test_vali; 
    idx_test_vali = find(test_vali);
    idx_train_vali = find(train_vali); 
    %num_test_vali = length(idx_test_vali);
    
for i = 1:num_class
    X_train_vali_v1 = [X_train_vali_v1' X_train_v1(100*(i-1)+idx_train_vali,:)']';
    X_train_vali_v2 = [X_train_vali_v2' X_train_v2(100*(i-1)+idx_train_vali,:)']';
    X_test_vali_v1 = [X_test_vali_v1' X_train_v1(100*(i-1)+idx_test_vali,:)']';
    X_test_vali_v2 = [X_test_vali_v2' X_train_v2(100*(i-1)+idx_test_vali,:)']';
    Y_train_vali = [Y_train_vali Y_train(:,100*(i-1)+idx_train_vali)];
    Y_test_vali = [Y_test_vali Y_train(:,100*(i-1)+idx_test_vali)];
end

for id = 1:5
   
    la = lamda(id); 
    l = num2str(la);
    num_trainvali = length(idx_train_vali);
X_train_vali = cell(num_view,1);
X_train_vali{1,1} = X_train_vali_v1;
X_train_vali{2,1} = X_train_vali_v2;
X_train_vali_total = [X_train_vali_v1 X_train_vali_v2];
Label_multiview{1,1} = Y_train_vali';
Label_multiview{2,1} = Y_train_vali';
X_test_vali{1,1} = X_test_vali_v1;
X_test_vali{2,1} = X_test_vali_v2;
X_test_vali{3,1} = [X_test_vali_v1 X_test_vali_v2];

%MLDA MULDA
[W_mlda W_mulda] = MULDA(X_train_vali,Label_multiview,a,la,num_trainvali);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
W1 = W_mlda{1,1}; 
W2 = W_mlda{2,1};
W_I = [W1;W2];
W_II = [W1 zeros(size(W1,1),size(W2,2));zeros(size(W2,1),size(W1,2)) W2]; 
W1_mu = W_mulda{1,1};
W2_mu = W_mulda{2,1};
W_I_mu = [W1_mu;W2_mu];
W_II_mu = [W1_mu zeros(size(W1_mu,1),size(W2_mu,2));zeros(size(W2_mu,1),size(W1_mu,2)) W2_mu];

%W_dcca = DCCA(X_train_vali,Label_multiview);
%W1_D = W_dcca{1,1};
%W2_D = W_dcca{2,1};
%W_I_D = [W1_D;W2_D];
%W_II_D = [W1_D zeros(size(W1_D,1),size(W2_D,2));zeros(size(W2_D,1),size(W1_D,2)) W2_D];

X_train_vali{3,1} = X_train_vali_total;
X_train_vali_M{1,1} = X_train_vali_v1 * W1;
X_train_vali_M{2,1} = X_train_vali_v2 * W2;
X_train_vali_M{3,1} = X_train_vali_total * W_I;
X_train_vali_M{4,1} = X_train_vali_total * W_II;
X_test_vali_M{1,1} = X_test_vali_v1 * W1;
X_test_vali_M{2,1} = X_test_vali_v2 * W2;
X_test_vali_M{3,1} = X_test_vali{3,1} * W_I;
X_test_vali_M{4,1} = X_test_vali{3,1} * W_II;

X_train_vali_MU{1,1} = X_train_vali_v1 * W1_mu;
X_train_vali_MU{2,1} = X_train_vali_v2 * W2_mu;
X_train_vali_MU{3,1} = X_train_vali_total * W_I_mu;
X_train_vali_MU{4,1} = X_train_vali_total * W_II_mu;
X_test_vali_MU{1,1} = X_test_vali_v1 * W1_mu;
X_test_vali_MU{2,1} = X_test_vali_v2 * W2_mu;
X_test_vali_MU{3,1} = X_test_vali{3,1} * W_I_mu;
X_test_vali_MU{4,1} = X_test_vali{3,1} * W_II_mu;

%X_train_vali_D{1,1} = X_train_vali_v1 * W1_D;
%X_train_vali_D{2,1} = X_train_vali_v2 * W2_D;
%X_train_vali_D{3,1} = X_train_vali_total * W_I_D;
%X_train_vali_D{4,1} = X_train_vali_total * W_II_D;
%X_test_vali_D{1,1} = X_test_vali_v1 * W1_D;
%X_test_vali_D{2,1} = X_test_vali_v2 * W2_D;
%X_test_vali_D{3,1} = X_test_vali{3,1} * W_I_D;
%X_test_vali_D{4,1} = X_test_vali{3,1} * W_II_D;

% perform classification
results_M = knn_classify(X_train_vali_M,X_test_vali_M,kn,Y_train_vali',num_view);
results_MU = knn_classify(X_train_vali_MU,X_test_vali_MU,kn,Y_train_vali',num_view);

%accuracy
for i = 1:num_view  
    accurate_M(i) = length(find(Y_test_vali == results_M{i,1}))/length(Y_test_vali);
    fprintf('View = %d, Accuracy by KNN after MLDA is: %f\n',i,accurate_M(i));
    accurate_MU(i) = length(find(Y_test_vali == results_MU{i,1}))/length(Y_test_vali);
    fprintf('View = %d, Accuracy by KNN after MULDA is: %f\n',i,accurate_MU(i));
end

fprintf('Accuracy by KNN is: %f\n', accurate(num_view+1));
accurate_M(num_view+1) = length(find(Y_test_vali == results_M{num_view+1,1})) / length(Y_test_vali);
fprintf('Accuracy by KNN with FFS_I after MLDA is: %f\n', accurate_M(num_view+1));
accurate_M(num_view+2) = length(find(Y_test_vali == results_M{num_view+2,1})) / length(Y_test_vali);
fprintf('Accuracy by KNN with FFS_II after MLDA is: %f\n', accurate_M(num_view+2)); % we choose these results.
accurate_MU(num_view+1) = length(find(Y_test_vali == results_MU{num_view+1,1})) / length(Y_test_vali);
fprintf('Accuracy by KNN with FFS_I after MULDA is: %f\n', accurate_MU(num_view+1));
accurate_MU(num_view+2) = length(find(Y_test_vali == results_MU{num_view+2,1})) / length(Y_test_vali);
fprintf('Accuracy by KNN with FFS_II after MULDA is: %f\n', accurate_MU(num_view+2)); % we choose these results.


accu = [accurate_M accurate_MU];

dlmwrite(['./MULDA/',view{v1,1},'-',view{v2,1},'.la=',l,'.a=',aa,'.txt'],accu,'-append','delimiter','\t');
end
end
end
    end
end