 knn_fusion <- function (train,trainLabel,test,trueLabel,k){

# K-NN classifier
   
   source("./paramperf.r")
   
   
   
   nbLabel=max(trueLabel)
   nTrain = nrow(train)
   nTest = nrow(test)
   dst = c()
   ist = c()
   
   for (i in 1:nTest) {
     distE = rowSums((matrix(1, nTrain, 1) %*% test[i, ] - train)^2)
     dss =sort(distE)
     iss = order(distE)
     dst = cbind(dst, dss[1:k])
     ist = cbind(ist, iss[1:k])
   }
   
   matLabel <- matrix(trainLabel[ist],nrow=k)
   
   foundLabel =c()
   
   distERed <- sqrt(dst)
   vect_denom <- colSums(distERed)
   
   matr_sort_classif<-matrix(0,nrow=nbLabel,ncol=nTest)
   for (i in 1:nTest){
     vLabel=c()
     denom <- vect_denom[i]
     for (j in 1:nbLabel){
       ind<-which(matLabel[,i]==j)
       vLabel<-c(vLabel,length(ind))
       
       if (length(ind)==0){
         numer <- 0
       }else{
         numer <- sum(distERed[ind,i])
       }
       matr_sort_classif[j,i] <- numer/denom
         
     }
     foundLabel <- c(foundLabel,which.max(vLabel))  
     
   }
   
   restest=rbind(trueLabel ,foundLabel)
   
   res<-paramperf(restest)
 
   return (list(foundLabel=foundLabel,matr_conf=res[1] ,vect_prob_classif=res[2],vect_prob_erreur=res[3],matr_sort_classif=matr_sort_classif))  
 }
 