title: Ordering a matrix to have the maximum value in each diagonal
body: <p>I've looked around StackOverflow and other forums for a solution but am yet to find a solution that just uses Base-R - which is what I need to be using for this. </p>
  
  <p>I'm trying to maximise the diagonal in an NxN matrix by row, so the maximum value from that row is on the diagonal (I hope this makes sense - specifically in the context of Cluster analysis) </p>
  
  <p>So far, I have tried hard coding this but it feels counter productive - this was done by trying to create a 'swap' function, using a for-loop down the columns and swapping the required entries. 
  i.e. on row 1:
  1,3,4,7 -> 7,3,4,1
  row 2: 
  4,10,14,22 -> 4,22,14,10 
  etc. </p>
  
  <p>Here is an example of the matrix I would try to use: </p>
  
  <pre><code>CM &lt;- table(expected, predicted)
  CM
       1   2  3  4  
    A  11  1  0  66
    B  0   22 77 18  
    C  10  49 34 48  
    D  0   46 31 49  
  
  #Desired:
  CM
  
       1   2  3  4  
    A  66  1  0  11  #max 66
    B  0   77 22 18  #max 77
    C  10  34 49 48  #max 49
    D  0   46 31 49  #max 49
  
  </code></pre>
  
  <p>Here is an extract of the (awful) swap function which did not work.</p>
  
  <pre><code>swap &lt;- function(x,y){
    a = c()       #create an empty vector
    a &lt;- x        #assign x value to a (where x = diagonal location)
    x &lt;- y        #assign y to x (where y = maximum value 
    y &lt;- a        #re-assign a value back to y
  
  }
  </code></pre>
  
  <p>above shows how I would expect the results - I was hoping someone could provide me with some useful pseudo code or to let me know if there is anything I have been missing out. </p>
  
  <p>Sorry if this is an easy or obvious question!</p>