title: Arranging rows of a matrix for maximum distance
body: <p>I have two identical matrices of 7 cols x 128 rows with each element containing one of two values (either 0.1 or 0.3).  The matrices are a representation of all the possible permutations of the two values (0.1 and 0.3) within 7 variables (columns). An excerpt of the matrices is shown below and you can see how the possible permutations progress from 7x values of &quot;0.1&quot; to 7x values of &quot;0.3&quot; with every possible permutation in-between.  There are 128 rows because the possible permutations are 2^7 = 128.</p>
  <p>Question.  I would like to be able to rearrange the rows (whole rows) in one of the matrices to maximise the mathematical (or Euclidean) distance between the rows. So for example, row 128 of matrix 2 is as far away as possible (within this set) from row 1 of matrix 1 but there will be rows in the middle of the two sets (matrices) that are close together.  Is there a way of finding the optimal arrangement of rows to maximise the distance?</p>
  <p>I understand that the matrix of distances between elements of two matrices is known as the distance matrix and so I guess that I am asking that I would like to be able to arrange the whole rows of one matrix to maximise the distance matrix between the two matrices.</p>
  <p>I have looked at calculating the possible permutations of 128 rows and running an apply function but there are 3.856204823 E+215 permutations of 128 row numbers and the vector is bigger than my 128GB RAM.</p>
  <p>I would like this to go beyond 2x matrices to n x matrices but at this point I would be happy with a 2x matrix solution.  One step at a time!</p>
  <p>Any thoughts or suggestions would be very much appreciated  Thank you.</p>
  <pre><code>   Var1 Var2 Var3 Var4 Var5 Var6 Var7
  1    0.1  0.1  0.1  0.1  0.1  0.1  0.1
  2    0.3  0.1  0.1  0.1  0.1  0.1  0.1
  3    0.1  0.3  0.1  0.1  0.1  0.1  0.1
  4    0.3  0.3  0.1  0.1  0.1  0.1  0.1
  5    0.1  0.1  0.3  0.1  0.1  0.1  0.1
  .
  .
  .
  125  0.1  0.1  0.3  0.3  0.3  0.3  0.3
  126  0.3  0.1  0.3  0.3  0.3  0.3  0.3
  127  0.1  0.3  0.3  0.3  0.3  0.3  0.3
  128  0.3  0.3  0.3  0.3  0.3  0.3  0.3
  </code></pre>
  <p>The matrices were made using the following code:</p>
  <pre><code>cor.vec=c(0.1,0.3)
  x.lst = list()
  for (i in 1:2)
    {
    x = expand.grid(rep(list(1:length(cor.vec)), 7))
    x[x == 1] = cor.vec[1]
    x[x == 2] = cor.vec[2]
    x.lst[[i]] = x
  }
  </code></pre>