0

In Matlab, P = nextpow2(A) returns the exponents for the smallest powers of two that satisfy

2^p≥|A| for each element in A.

a = [1 -2 3 -4 5 9 519];
p = nextpow2(a) returns

p = 1×7

 0     1     2     2     3     4    10

Calculate the positive next powers of 2.

np2 = 2.^p returns np2 = 1×7

1 2 4 4 8 16 1024

I need to have the exact functionality with Python so that I can calculate this:

 mag = sqrt(flow(:,:,1).^2+flow(:,:,2).^2)*scale+128;

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)

Is there a function in Python libraries? Or how I can write it the same way as it if (input is a matrix)

dusa
  • 840
  • 3
  • 14
  • 31
  • 9
    `nextpow2(x)` is just `ceil(log2(abs(x)))` – Ander Biguri Jan 18 '19 at 15:01
  • 1
    See [here](https://stackoverflow.com/q/14267555/2586922) for some ideas (non-matrix input) – Luis Mendo Jan 18 '19 at 15:43
  • what about: ```def nextpow2(p): n = 2; while p < n: n *= 2; return n ``` – s.ouchene May 26 '19 at 16:53
  • 3
    Does this answer your question? [Find the smallest power of 2 greater than or equal to n in Python](https://stackoverflow.com/questions/14267555/find-the-smallest-power-of-2-greater-than-or-equal-to-n-in-python) – Jack_P Jan 01 '21 at 22:17

0 Answers0