title: Creating a new matrix from elements of existing matrix use python
body: <p>I want to get a new matrix <em>S</em>. The elements of <em>S</em> are calculated from the elements of another matrix <em>W</em>. equation is</p>
  <pre><code>$$S_{i j}=\sum_{m}\left\{\left(\frac{W_{m i}+W_{m j}}{W_{w}}\right) \times\left[1-\frac{\left|\left(\frac{W_{m i}}{W_{i}}\right)-\left(\frac{W_{m j}}{W_{j}}\right)\right|}{\left(\frac{W_{m i}}{W_{i}}\right)+\left(\frac{W_{m j}}{W_{j}}\right)}\right]\right\} \times 100$$
  </code></pre>
  <p>you can also click this &quot;<a href="https://i.stack.imgur.com/aD76b.png" rel="nofollow noreferrer">figure</a>&quot; to see equation.
  Now W is</p>
  <pre><code>[[0 1 1 0]
   [0 1 0 0]
   [0 0 0 1]
   [0 0 1 0]]
  </code></pre>
  <p>and expected S is</p>
  <pre><code>   [[0    0    0    0]
      [0    0   40    0]
      [0   40    0    0]
      [0    0    0    0]]
  </code></pre>
  <p>Where:</p>
  <ul>
  <li><p><em>W<sub>mi</sub></em> is the element of <em>W</em> in row <em>m</em>, column <em>i</em></p>
  </li>
  <li><p><em>W<sub>mj</sub></em> is the element of <em>W</em> in row <em>w</em>, column <em>j</em>.</p>
  </li>
  <li><p><em>W<sub>mj</sub></em> and <em>W<sub>mi</sub></em> must not both be zero, loop all row to get Sij,if any of two element in m row,i column,j column is 0,for example, W12=1,W13=1,that is m=1,i=2,j=3  ,I can get S23 by loop all row,but W12=W22=1,W11=W21=W31=W41=W32=W42=0,so in order to get S12 by loop all row,in this case,I did not calculate given equation, just S12=0+0+0+0=0,that is I only compute equation in the picture when Smi and Smj all not 0</p>
  </li>
  <li><p><em>W<sub>w</sub></em> is the sum of all elements of <em>W</em>.</p>
  </li>
  <li><p><em>W<sub>i</sub></em> and <em>W<sub>j</sub></em> are the sums of column <em>i</em> and <em>j</em>, respectively.</p>
  </li>
  <li><p><em>m</em> is the row; in this sample input matrix there are 4 rows across which to sum.</p>
  </li>
  </ul>
  <p>My code is as follows:</p>
  <pre><code>import numpy as np
  W= np.matrix([[0,1,1,0],[0,1,0,0],[0,0,0,1],[0,0,1,0]])
  print(W)
  W_sum=np.sum(W)# sum of all the element
  
  for i in range(W.shape[0]):
      for j in range(W.shape[0]):
          S = np.empty(W.shape)
          if i ==j:
              S[i,j]=0
          else:
              s=0
              for m in range(W.shape[0]):
                  if W[m,i] != 0 &amp;  W[m,j]!=0:
                      a1 = (W[m, i] + W[m, j])/W_sum
                      a2 = W[m, i] / W[:, i].sum(axis=0)-W[m, j] / W[:, j].sum(axis=0)
                      a3 = W[m, i] / W[:, i].sum(axis=0)+W[m, j] / W[:, j].sum(axis=0)
                      q = 100*(a1*(1-abs(a2)/a3))
                  else:
                      q = 0
                  s = s + q
              S[i, j] = s
  print(S)
  </code></pre>
  <p>however,my print is</p>
  <pre><code>[[3.03331816e+180 0.00000000e+000 5.04620658e+180 0.00000000e+000]
   [3.68777421e+180 0.00000000e+000 7.58612245e+170 0.00000000e+000]
   [9.15323418e-095 0.00000000e+000 1.39208628e-258 0.00000000e+000]
   [1.13073491e+277 0.00000000e+000 2.89152448e-014 0.00000000e+000]]
  </code></pre>
  <p>I did not know how to get true answer</p>