title: Matlab nextpow2(A) in Python
body: <p>In Matlab, P = nextpow2(A) returns the exponents for the smallest powers of two that satisfy</p>
  
  <p>2^p≥|A|
  for each element in A.</p>
  
  <pre><code>a = [1 -2 3 -4 5 9 519];
  p = nextpow2(a) returns
  </code></pre>
  
  <p>p = 1×7</p>
  
  <pre><code> 0     1     2     2     3     4    10
  </code></pre>
  
  <p>Calculate the positive next powers of 2.</p>
  
  <p><code>np2 = 2.^p</code>  returns
  np2 = 1×7</p>
  
  <p>1           2           4           4           8          16        1024</p>
  
  <p>I need to have the exact functionality with Python so that I can calculate this:</p>
  
  <pre><code> mag = sqrt(flow(:,:,1).^2+flow(:,:,2).^2)*scale+128;
  </code></pre>
  
  <p>where flow is the optical flow, meaning a velocity vector field at the size of the image, where each element shows the disparity as distance from one axes:so flow is (U1,U2) where U1.shape = (227,227) U2.shape=  (227,227)</p>
  
  <p>Is there a function in Python libraries? Or how I can write it the same way as it if (input is a matrix)</p>