title: Calculating co-voting within and between groups
body: <p>I have a square matrix with information on the co-voting behavior between individuals (15x15 in toy example below). The rows and columns of the matrix are arranged according to the groups the individuals belong to (A, B or C). The entries indicate whether or not two individuals voted the same way 50% of the time (possible entries: 1, 0, NaN).</p>
  <p>I need to calculate the rate/fraction of co-voting within and between groups. The resulting matrix in the toy example should be a 3x3 matrix with A, B, C on the rows and columns and values ranging from 0 to 1. How can I do this using for loops?</p>
  <pre><code>  A A A A A B B B B B C C C C C
  A 1 1 1 1 1 1 0 0 0 0 0 1 1 1 0
  A 1 1 1 1 1 1 0 0 0 0 0 1 1 1 0
  A 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0
  A 1 1 1 1 1 1 0 0 0 0 0 1 1 1 0
  A 1 1 1 1 1 1 0 0 0 0 0 1 1 1 0
  B 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1
  B 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1
  B 0 0 0 0 0 1 1 1 1 1 1 1 0 1 1
  B 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1
  B 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1
  C 0 0 1 0 0 1 1 1 1 1 1 1 0 0 1
  C 1 1 1 1 1 1 0 1 0 0 1 1 1 0 1
  C 1 1 1 1 1 1 0 0 0 0 0 1 1 1 0
  C 1 1 1 1 1 1 1 1 0 1 0 0 1 1 0
  C 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1
  </code></pre>