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

num_view = 2;
Label_multiview = cell(num_view,1);
%X_train = cell(num_view,1);
X_test = cell(num_view,1);
%Label_train = cell(num_view,1);
%Label_test = cell(num_view,1);
accurate = zeros(1,num_view+1);
accurate_M = zeros(1,num_view+2);
accurate_MU = zeros(1,num_view+2);
accurate_D = zeros(1,num_view+2);
lamda = cell(3,1);
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
randidx = load('RandIdx.txt');

lamda{1,1} = [3 3 3 17 0 0 9 1 1 0 1 1 1 1 1];
lamda{2,1} = [3 1 20 7 3 3 20 15 1 20 7 7 20 20 20];
lamda{3,1} = [7 13 5 5 17 1 5 1 1 0 1 1 1 1 5];
%as = ['1','2','3'];
lamda_scaled = [1 1 1 1 1 1 3 1 1 1 1 1 1 1 1];%a1
%lamda_scaled = [3 5 1 1 1 2 2 1 1 1 1 1 1 1 4];%a3

id = 0;

for v1 = 1:5
    for v2 = v1+1:6

id = id+1;
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;

%A = zeros(num_view+1);
%A_M = zeros(num_view+2);
%A_MU = zeros(num_view+2);
%A_D = zeros(num_view+2);

for iter = 1:10
    
    X_train_v1 = []; %zeros(250,size(X_v1,2));
    X_train_v2 = []; %zeros(250,size(X_v2,2));
    X_test_v1 = []; %zeros(750,size(X_v1,2));
    X_test_v2 = []; %zeros(750,size(X_v2,2));
    Y_train = []; %zeros(1,250);
    Y_test = []; %zeros(1,750);
    
    fprintf('experiment between view %s and view %s in iter %d\n',[view{v1,1}],[view{v2,1}],iter);
    rand = randidx(iter,:);
    idx_train = rand(1:100);
    idx_test = rand(101:200);
    num_train = length(idx_train);
    
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,:)']';
    X_test_v1 = [X_test_v1' X_v1(200*(i-1)+idx_test,:)']';
    X_test_v2 = [X_test_v2' X_v2(200*(i-1)+idx_test,:)']';
    Y_train = [Y_train Y(:,200*(i-1)+idx_train)];
    Y_test = [Y_test Y(:,200*(i-1)+idx_test)];
end
    a = 3;  %a=1 represents the results of MULDA and MLDA. a=3 represents the results of MULDA-m and MLDA-m. 
    %la = lamda{a,1}(id);
    la = lamda_scaled(id);
        
X_train = cell(num_view,1);
X_train{1,1} = X_train_v1;
X_train{2,1} = X_train_v2;
X_train_total = [X_train_v1 X_train_v2];
Label_multiview{1,1} = Y_train';
Label_multiview{2,1} = Y_train';
X_test{1,1} = X_test_v1;
X_test{2,1} = X_test_v2;
X_test{3,1} = [X_test_v1 X_test_v2];

%MLDA MULDA
[W_mlda W_mulda] = MULDA(X_train,Label_multiview,a,la,num_train);%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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,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{3,1} = X_train_total;
X_train_M{1,1} = X_train_v1 * W1;
X_train_M{2,1} = X_train_v2 * W2;
X_train_M{3,1} = X_train_total * W_I;
X_train_M{4,1} = X_train_total * W_II;
X_test_M{1,1} = X_test_v1 * W1;
X_test_M{2,1} = X_test_v2 * W2;
X_test_M{3,1} = X_test{3,1} * W_I;
X_test_M{4,1} = X_test{3,1} * W_II;

X_train_MU{1,1} = X_train_v1 * W1_mu;
X_train_MU{2,1} = X_train_v2 * W2_mu;
X_train_MU{3,1} = X_train_total * W_I_mu;
X_train_MU{4,1} = X_train_total * W_II_mu;
X_test_MU{1,1} = X_test_v1 * W1_mu;
X_test_MU{2,1} = X_test_v2 * W2_mu;
X_test_MU{3,1} = X_test{3,1} * W_I_mu;
X_test_MU{4,1} = X_test{3,1} * W_II_mu;

X_train_D{1,1} = X_train_v1 * W1_D;
X_train_D{2,1} = X_train_v2 * W2_D;
X_train_D{3,1} = X_train_total * W_I_D;
X_train_D{4,1} = X_train_total * W_II_D;
X_test_D{1,1} = X_test_v1 * W1_D;
X_test_D{2,1} = X_test_v2 * W2_D;
X_test_D{3,1} = X_test{3,1} * W_I_D;
X_test_D{4,1} = X_test{3,1} * W_II_D;

% perform classification
results = knn_classify(X_train,X_test,kn,Y_train',num_view);
results_M = knn_classify(X_train_M,X_test_M,kn,Y_train',num_view);
results_MU = knn_classify(X_train_MU,X_test_MU,kn,Y_train',num_view);
results_D = knn_classify(X_train_D,X_test_D,kn,Y_train',num_view);


%accuracy
for i = 1:num_view
    accurate(i) = length(find(Y_test == results{i,1}))/length(Y_test);
    fprintf('View = %d, Accuracy by KNN is: %f\n',i,accurate(i));
    accurate_M(i) = length(find(Y_test == results_M{i,1}))/length(Y_test);
    fprintf('View = %d, Accuracy by KNN after MLDA is: %f\n',i,accurate_M(i));
    accurate_MU(i) = length(find(Y_test == results_MU{i,1}))/length(Y_test);
    fprintf('View = %d, Accuracy by KNN after MULDA is: %f\n',i,accurate_MU(i));
    accurate_D(i) = length(find(Y_test == results_D{i,1}))/length(Y_test);
    fprintf('View = %d, Accuracy by KNN after DCCA is: %f\n',i,accurate_D(i));
end
%Y_test_total = [Y_test Y_test];
accurate(num_view+1) = length(find(Y_test == results{num_view+1,1})) / length(Y_test);
fprintf('Accuracy by KNN is: %f\n', accurate(num_view+1));
accurate_M(num_view+1) = length(find(Y_test == results_M{num_view+1,1})) / length(Y_test);
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 == results_M{num_view+2,1})) / length(Y_test);
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 == results_MU{num_view+1,1})) / length(Y_test);
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 == results_MU{num_view+2,1})) / length(Y_test);
fprintf('Accuracy by KNN with FFS_II after MULDA is: %f\n', accurate_MU(num_view+2)); % we choose these results
accurate_D(num_view+1) = length(find(Y_test == results_D{num_view+1,1})) / length(Y_test);
fprintf('Accuracy by KNN with FFS_I after DCCA is: %f\n', accurate_D(num_view+1));
accurate_D(num_view+2) = length(find(Y_test == results_D{num_view+2,1})) / length(Y_test);
fprintf('Accuracy by KNN with FFS_II after DCCA is: %f\n', accurate_D(num_view+2));


%A(:,iter) = accurate;
A = [accurate accurate_D accurate_M accurate_MU];
%A_MU(:,iter) = accurate_MU;
%A_D(:,iter) = accurate_D;

dlmwrite(['./MULDAresults/',view{v1,1},'-',view{v2,1},'.scaled.a1.txt'],A,'-append','delimiter','\t');
%dlmwrite([view{v1,1},'-',view{v2,1},'.well.a',ad,'.txt'],A_MU,'-append','delimiter','\t');
%dlmwrite([view{v1,1},'-',view{v2,1},'.well.a',ad,'.txt'],A_D,'-append','delimiter','\t');

end

%dlmwrite([view{v1,1},'-',view{v2,1},'.well.a',as(a),'.txt'],A,'\t');

    end
end