how to calculate all pairwise distances in two dimensions and transform them into a matrix

Hello to all,

I am very new in the shell scripting and I need help. I have data for several individuals in several rows followed by a tag and by 5 values per row, with the name of the individual in the first column, e.g.:

  IND1 H1 12 13 12 15 14
  IND2 H2 12 12 15 14 14
  IND3 H1 12 15 12 14 11

I would like to calculate the sum of the absolute values of the pairwise differences between individuals. For instance:

  Distance between IND1 and IND2=|12-12|+|13-12|+|12-15|+|15-14|+|14-14|= 5
  Distance between IND1 and IND3=|12-12|+|13-15|+|12-12|+|15-14|+|14-11|= 6
  Distance between IND2 and IND3=|12-12|+|12-15|+|15-12|+|14-14|+|14-11|= 9

Additionally, if the tags of two individuals are different, I would like to sum 100 to the final number. So finally the distance between:
IND1 and IND2 would be = 5 + 100 = 105
IND1 and IND3 would be = 6 + 0 = 6
IND2 and IND3 would be = 9 + 100 = 109

After this, I would like to transform this list of distances into a matrix:

  IND1   IND2   IND3
  IND1   
  IND2   105
  IND3   6        109      

Could some one help me with this? Thanks a lot in advance! Best!

Here is a solution using awk:

awk '
{ for(i=1;i<=NF;i++) D[NR,i]=$i }
END {
  for(i=1;i<=NR;i++) printf "\t" D[i,1];
  print ""
  for(i=1;i<=NR;i++) {
      printf D[i,1];
      for(j=1;j<=NR;j++) {
           TOT=(D[i,2]==D[j,2])?0:100
           for(k=3;k<8;k++) TOT+=D[i,k]>D[j,k]?D[i,k]-D[j,k]:D[j,k]-D[i,k]
           printf "\t" TOT;
      }
      print ""
  }
}' infile

Output for example infile is:

        IND1    IND2    IND3
IND1    0       105     6
IND2    105     0       109
IND3    6       109     0

Hi,

It works great, thanks a lot!

Best,

B