title: Broadcasting (N,2,2) array and (N,2) array through a for loop
body: <p>I am trying to migrate some code from MATLAB to python and I am having trouble understanding why the following code does not work.</p>
  <pre><code>import numpy as np
  
  ngrid = 56
  A = np.random.randint(10, size =(ngrid*ngrid,2))
  A_tmp = A
  B = np.random.randint(10,size =(ngrid*ngrid,2,2) )
  
  
  for jj in range(ngrid*ngrid):
      A[jj,:] = A_tmp[jj,:]*B[jj,:,:].conj()
  </code></pre>
  <p>When I execute this code I get the error.</p>
  <pre><code>ValueError: could not broadcast input array from shape (2,2) into shape (2,)
  </code></pre>
  <p>I am not understanding why this is giving me a one dimensional array vs a <code>(ngrid*ngrid,2)</code> array.</p>
  <p>The MATLAB code I am trying to recreate for is</p>
  <pre><code>for jj = 1:ngrid^2
      Psi0(jj, :) = Psi0_tmp(jj, :)*dia2adi(:,:, jj)';
  end
  </code></pre>
  <p>Any guidance on theory and how to correct my code would be very helpful.</p>
  <p>Thanks</p>