function [final_output] = featuremap(reg_map,link_map)

[ m n ] = size(reg_map);
K_MAX = max(max(reg_map));
output = zeros(K_MAX,6);

for i=1:m
  for j=1:n
     if reg_map(i,j) ~= 0
        output(link_map(reg_map(i,j)),1) = link_map(reg_map(i,j));
        output(link_map(reg_map(i,j)),2) = output(link_map(reg_map(i,j)),2) + 1;
        output(link_map(reg_map(i,j)),3) = output(link_map(reg_map(i,j)),3) + i;
        output(link_map(reg_map(i,j)),4) = output(link_map(reg_map(i,j)),4) + j;
     end
  end
end
          

for k=1:K_MAX
   if output(k,2) ~= 0
     output(k,3) = output(k,3)/output(k,2);
     output(k,4) = output(k,4)/output(k,2);
   end
end

for i=1:m
  for j=1:n

     if reg_map(i,j) ~= 0
        output(link_map(reg_map(i,j)),5) = output(link_map(reg_map(i,j)),5) + ...
                                (output(link_map(reg_map(i,j)),3)-i)^2 ;
        output(link_map(reg_map(i,j)),6) = output(link_map(reg_map(i,j)),6) + ...
                                (output(link_map(reg_map(i,j)),4)-j)^2 ;
     end
  end
end

for k=1:K_MAX
   if output(k,2) >= 10 
     output(k,5) = sqrt(output(k,5)/output(k,2));
     output(k,6) = sqrt(output(k,6)/output(k,2));
   else
     output(k,:) = zeros(1,6);   
   end
end

if K_MAX ~= 0
 [output(:,1) INDEX] = sort(output(:,1),1);
 output(:,2:6) = output(INDEX,2:6); 

 i=1;
 while output(i,1) == 0
   i = i + 1;
 end

 final_output = output(i:K_MAX,:);
else
 final_output = zeros(1,6);
end









