function  Init_Index  =  Block_matching(X, par,Neighbor_arr,Num_arr, SelfIndex_arr)
L         =   length(Num_arr);
Init_Index   =  zeros(par.patnum,L);
%Diff_Index  =zeros(par.patnum,L);

for  i  =  1 : L
    Patch = X(:,SelfIndex_arr(i));
    Neighbors = X(:,Neighbor_arr(1:Num_arr(i),i));    
    Dist = sum((repmat(Patch,1,size(Neighbors,2))-Neighbors).^2);    
    [val index] = sort(Dist); 
    indtemp=find(val<par.thresh*par.sigma.^2*par.patsize.^2);
    %fprintf('%f  %d\n',val(10),length(indtemp));
    index1=index(1:par.patnum);
    Rindtemp=length(indtemp);
    if  Rindtemp>=par.patnum
         Init_Index(:,i)=Neighbor_arr(index1,i);
   %      Diff_Index(:,i)=val(1:par.patnum)';
    else
         index2=index1(1:Rindtemp);
         Init_Index(:,i)=Neighbor_arr(padarray(index2,[0,par.patnum-Rindtemp],'post','replicate'),i); 
   %      Diff_Index(:,i)=val(padarray(1:Rindtemp,[0,par.patnum-Rindtemp],'post','replicate'))';
    end
end
