function selectedFeatures = JMIM(k, featureMatrix, classColumn,folder_path)

%Computes Jiont Mutual Information Maximisation algorithm, which has been published 
%Bennasar et al."Feature selection using joint mutual information maximisation." 
%Expert Systems with Applications 42.22 (2015): 8520-8532.

% Input:
% k : is the size of the subset of the  most significant features. 
% featureMatrix: a dataset features matrix, the features should be numarical, 
% it is preferred to discretise the continuous, the size of featureMatrix
% is n X m.
% classColumn: the classes label, it should be numarical value. 
%Output:
% selectedFeatures: is one dimentional array with size k, it contains 
% the selected subset of features, it will contain  the nunbers of k significant 
% features ranked in descending order, starting  most significant selectedFeatures(1).


save('C:\Temp\Temp.mat');
  CMIT(folder_path)

load('C:\Temp\Temp.mat');

noOfTraining = size(classColumn,1);
noOfFeatures = size(featureMatrix,2);

partialScore = zeros(noOfFeatures,1);
m = zeros(noOfFeatures,1);
score = 0;
answerFeatures = zeros(k,1);
highestMI = 0;
highestMICounter = 0;

for n = 1 : noOfFeatures
  partialScore(n) = mi(featureMatrix(:,n),classColumn);
  if partialScore(n) > highestMI
    highestMI = partialScore(n);
    highestMICounter = n;
  end
end

answerFeatures(1) = highestMICounter;
flag=0;
for i = 2 : k
  score = 0;
  flag=flag+1
  for n = 1 : noOfFeatures
     Itemp=10000;
    if (( n ~= answerFeatures(:)) )  
        JointInfo=zeros(i-1,1);
%         featureMatrixGPU=gpuArray(featureMatrix);
%         answerFeaturesGPU=gpuArray(answerFeatures);
%         JointInfoGPU=gpuArray(JointInfo);
%         classColumnGPU=gpuArray(classColumn);
    for j=1:i-1    
       JointInfo(j) = mi(featureMatrix(:,answerFeatures(j)),classColumn)+ cmi(featureMatrix(:,n),classColumn,featureMatrix(:,featureMatrix(j)));
       
%       if Itemp > JointInfo
%          Itemp = JointInfo;
%       end
     end
    
    Itemp=max(JointInfo);
    if Itemp >= score
      score = Itemp;
      answerFeatures(i) = n;
    end
    end
 end
end

selectedFeatures = answerFeatures;
