1

I want to convert binary numpy array to decimal. Is there a similar function to this numpy.binary_repr that works the other way around?

x = array([ 1., 1., 0., 1., 0., 0., 0., 1., 0.])

I know I can do this:

int("110100010",2) = 418

But here I need to extract the elements of the array and put them into string? Is there an easier way? if not, then how can I extract the elements of the array and create a string out of them?

0

1 Answer 1

1

You can use reduce

>>> from functools import reduce
>>> reduce(lambda a,b: 2*a+b, x)
418.0

Alternatively, as you said, you can construct a string and then convert it to int (base 2)

>>> int(''.join(map(lambda x: str(int(x)), x)), 2)
418
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.