function [TrainingAccuracy,TestingAccuracy] = ELM_kernel(TrainingData_File, TestingData_File,sigma2,C)

%%%%%%%%%%% Macro definition
REGRESSION=0;
CLASSIFIER=1;

%%%%%%%%%%% Load training dataset
train_data = TrainingData_File;
T=train_data(:,1)';
P=train_data(:,2:size(train_data,2))';
clear train_data;                                   %   Release raw training data array

%%%%%%%%%%% Load testing dataset
test_data =  TestingData_File;
TV.T=test_data(:,1)';
TE0=test_data(:,1)';
TV.P=test_data(:,2:size(test_data,2))';
clear test_data;                                    %   Release raw testing data array

NumberofTrainingData=size(P,2);
NumberofTestingData=size(TV.P,2);
NumberofInputNeurons=size(P,1);

[x,posx1] = find(T==1);[x,posx2] = find(T==2);[x,posx3] = find(T==3);[x,posx4] = find(T==4);
[x,posx5] = find(T==5);[x,posx6] = find(T==6);
W = zeros(length(T),length(T));

% for i = 1:length(T)
%     if T(i) == 1
%         W(i,i)= 1/length(posx);
%     else
%         W(i,i)= 1/(length(T)-length(posx));
%     end 
% end
for i = 1:length(T)
    if T(i) == 1
        W(i,i)= 1/length(posx1);
    elseif T(i)==2
        W(i,i)= 1/length(posx2);
    elseif T(i)==3
        W(i,i)= 1/length(posx3);
    elseif T(i)==4
        W(i,i)= 1/length(posx4);
    elseif T(i)==5
        W(i,i)= 1/length(posx5);
    elseif T(i)==6
        W(i,i)= 1/length(posx6);
    end 
end

    %%%%%%%%%%%% Preprocessing the data of classification
    sorted_target=sort(cat(2,T,TV.T),2);
    label=zeros(1,1);                               %   Find and save in 'label' class label from training and testing data sets
    label(1,1)=sorted_target(1,1);
    j=1;
    for i = 2:(NumberofTrainingData+NumberofTestingData)
        if sorted_target(1,i) ~= label(1,j)
            j=j+1;
            label(1,j) = sorted_target(1,i);
        end
    end
    number_class=j;
    NumberofOutputNeurons=number_class;
    
    %%%%%%%%%% Processing the targets of training
    temp_T=zeros(NumberofOutputNeurons, NumberofTrainingData);
    for i = 1:NumberofTrainingData
        for j = 1:number_class
            if label(1,j) == T(1,i)
                break; 
            end
        end
        temp_T(j,i)=1;
    end
    T=temp_T*2-1;

    %%%%%%%%%% Processing the targets of testing
    temp_TV_T=zeros(NumberofOutputNeurons, NumberofTestingData);
    for i = 1:NumberofTestingData
        for j = 1:number_class
            if label(1,j) == TV.T(1,i)
                break; 
            end
        end
        temp_TV_T(j,i)=1;
    end
    TV.T=temp_TV_T*2-1;
                                              %   end if of Elm_Type

%%%%%%%%%%% Calculate weights & biases
% start_time_train=cputime;
tic;
n = size(T,2);
Omega_train = kernel_matrix(P','RBF_kernel', sigma2);
OutputWeight=((Omega_train+speye(n)/C)\(T')); 

% OutputWeight=mtimesx(H,((mtimesx(H',H)+speye(n)/C)\T')); 
% OutputWeight=inv(H * H') * H * T';                         % faster implementation
% end_time_train=cputime;
% TrainingTime=end_time_train-start_time_train     ;   %   Calculate CPU time (seconds) spent for training ELM
TrainingTime=toc;
%%%%%%%%%%% Calculate the training accuracy
Y=(Omega_train * OutputWeight)';                             %   Y: the actual output of the training data

%%%%%%%%%%% Calculate the output of testing input
% start_time_test=cputime;
tic;
Omega_test = kernel_matrix(P','RBF_kernel', sigma2,TV.P');
TY=(Omega_test' * OutputWeight)';                     %   TY: the actual output of the testing data
% end_time_test=cputime;
% TestingTime=end_time_test-start_time_test      ;     %   Calculate CPU time (seconds) spent by ELM predicting the whole testing data
TestingTime=toc;

%%%%%%%%%% Calculate training & testing classification accuracy
    %MissClassificationRate_Training=0;
    %MissClassificationRate_Testing=0;
Elm_Type=1;
if Elm_Type == CLASSIFIER
%%%%%%%%%% Calculate training & testing classification accuracy
    %MissClassificationRate_Training=0;
    %MissClassificationRate_Testing=0;

    MissClassificationRate_Training=0;
    c12=0;c13=0;c14=0;c15=0;c16=0;
    c21=0;c23=0;c24=0;c25=0;c26=0;
    c31=0;c32=0;c34=0;c35=0;c36=0;
    c41=0;c42=0;c43=0;c45=0;c46=0;
    c51=0;c52=0;c53=0;c54=0;c56=0;
    c61=0;c62=0;c63=0;c64=0;c65=0;
    for i = 1 : size(T, 2)
        [x, label_index_expected]=max(T(:,i));
        [x, label_index_actual]=max(Y(:,i));
        if label_index_actual~=label_index_expected
           MissClassificationRate_Training=MissClassificationRate_Training+1;
        end
        if label_index_expected==1
            if label_index_actual==2
                c12=c12+1;
            elseif label_index_actual==3
                c13=c13+1;
            elseif label_index_actual==4
                c14=c14+1;
            elseif label_index_actual==5
                c15=c15+1;
            elseif label_index_actual==6
                c16=c16+1;
            end
        elseif label_index_expected==2
            if label_index_actual==1
                c21=c21+1;
            elseif label_index_actual==3
                c23=c23+1;
            elseif label_index_actual==4
                c24=c24+1;
            elseif label_index_actual==5
                c25=c25+1;
            elseif label_index_actual==6
                c26=c26+1;
            end
        elseif label_index_expected==3
            if label_index_actual==1
                c31=c31+1;
            elseif label_index_actual==2
                c32=c32+1;
            elseif label_index_actual==4
                c34=c34+1;
            elseif label_index_actual==5
                c35=c35+1;
            elseif label_index_actual==6
                c36=c36+1;
            end
        elseif label_index_expected==4
            if label_index_actual==1
                c41=c41+1;
            elseif label_index_actual==2
                c42=c42+1;
            elseif label_index_actual==3
                c43=c43+1;
            elseif label_index_actual==5
                c45=c45+1;
            elseif label_index_actual==6
                c46=c46+1;
            end
        elseif label_index_expected==5
            if label_index_actual==1
                c51=c51+1;
            elseif label_index_actual==2
                c52=c52+1;
            elseif label_index_actual==3
                c53=c53+1;
            elseif label_index_actual==4
                c54=c54+1;
            elseif label_index_actual==6
                c56=c56+1;
            end
        elseif label_index_expected==6
            if label_index_actual==1
                c61=c61+1;
            elseif label_index_actual==2
                c62=c62+1;
            elseif label_index_actual==3
                c63=c63+1;
            elseif label_index_actual==4
                c64=c64+1;
            elseif label_index_actual==5
                c65=c65+1;
            end
        end
    end
    C_tr=[c12,c13,c14,c15,c16;
    c21,c23,c24,c25,c26;
    c31,c32,c34,c35,c36;
    c41,c42,c43,c45,c46;
    c51,c52,c53,c54,c56;
    c61,c62,c63,c64,c65];
% testing
    MissClassificationRate_Testing=0;
    c12=0;c13=0;c14=0;c15=0;c16=0;
    c21=0;c23=0;c24=0;c25=0;c26=0;
    c31=0;c32=0;c34=0;c35=0;c36=0;
    c41=0;c42=0;c43=0;c45=0;c46=0;
    c51=0;c52=0;c53=0;c54=0;c56=0;
    c61=0;c62=0;c63=0;c64=0;c65=0;
    for i = 1 : size(TV.T, 2)
        [x, label_index_expected]=max(TV.T(:,i));
        [x, label_index_actual]=max(TY(:,i));
        if label_index_actual~=label_index_expected
           MissClassificationRate_Testing=MissClassificationRate_Testing+1;
        end
        if label_index_expected==1
            if label_index_actual==2
                c12=c12+1;
            elseif label_index_actual==3
                c13=c13+1;
            elseif label_index_actual==4
                c14=c14+1;
            elseif label_index_actual==5
                c15=c15+1;
            elseif label_index_actual==6
                c16=c16+1;
            end
        elseif label_index_expected==2
            if label_index_actual==1
                c21=c21+1;
            elseif label_index_actual==3
                c23=c23+1;
            elseif label_index_actual==4
                c24=c24+1;
            elseif label_index_actual==5
                c25=c25+1;
            elseif label_index_actual==6
                c26=c26+1;
            end
        elseif label_index_expected==3
            if label_index_actual==1
                c31=c31+1;
            elseif label_index_actual==2
                c32=c32+1;
            elseif label_index_actual==4
                c34=c34+1;
            elseif label_index_actual==5
                c35=c35+1;
            elseif label_index_actual==6
                c36=c36+1;
            end
        elseif label_index_expected==4
            if label_index_actual==1
                c41=c41+1;
            elseif label_index_actual==2
                c42=c42+1;
            elseif label_index_actual==3
                c43=c43+1;
            elseif label_index_actual==5
                c45=c45+1;
            elseif label_index_actual==6
                c46=c46+1;
            end
        elseif label_index_expected==5
            if label_index_actual==1
                c51=c51+1;
            elseif label_index_actual==2
                c52=c52+1;
            elseif label_index_actual==3
                c53=c53+1;
            elseif label_index_actual==4
                c54=c54+1;
            elseif label_index_actual==6
                c56=c56+1;
            end
        elseif label_index_expected==6
            if label_index_actual==1
                c61=c61+1;
            elseif label_index_actual==2
                c62=c62+1;
            elseif label_index_actual==3
                c63=c63+1;
            elseif label_index_actual==4
                c64=c64+1;
            elseif label_index_actual==5
                c65=c65+1;
            end
        end
    end
    C_te=[c12,c13,c14,c15,c16;
    c21,c23,c24,c25,c26;
    c31,c32,c34,c35,c36;
    c41,c42,c43,c45,c46;
    c51,c52,c53,c54,c56;
    c61,c62,c63,c64,c65];
C_te2=[length(find(TE0==1))-sum([c12,c13,c14,c15,c16]),c12,c13,c14,c15,c16;
    c21,length(find(TE0==2))-sum([c21,c23,c24,c25,c26]),c23,c24,c25,c26;
    c31,c32,length(find(TE0==3))-sum([c31,c32,c34,c35,c36]),c34,c35,c36;
    c41,c42,c43,length(find(TE0==4))-sum([c41,c42,c43,c45,c46]),c45,c46;
    c51,c52,c53,c54,length(find(TE0==5))-sum([c51,c52,c53,c54,c56]),c56;
    c61,c62,c63,c64,c65,length(find(TE0==6))-sum([c61,c62,c63,c64,c65])];
TestingAccuracy=100-MissClassificationRate_Testing/length(TV.T)*100;
TrainingAccuracy=100-MissClassificationRate_Training/length(T)*100;
end
