arrays - How to rotate a binary vector to minimum in Python -


if have arbitrary binary vector (numpy array) in python, e.g.

import numpy np  vector = np.zeros((8,1)) vector[2,1] = 1 vector[3,1] = 1  

this give me binary array 00001100. have 00000000 or 00010100 etc. how make such script when give binary vector input, script gives minimum right-rotated binary numpy array output? few examples:

00010000 --> 00000001 10100000 --> 00000101 11000001 --> 00000111 00000000 --> 00000000 11111111 --> 11111111 10101010 --> 01010101 11110000 --> 00001111 00111000 --> 00000111 10001111 --> 00011111 

etc. suggestions / optimized python implementations in mind? =) thank assistance. need local binary pattern implementation =)

the fastest way create table first , can use ndarray indexing result, here code:

you need create table yourself, code here demo

import numpy np np.random.seed(0)  #create table def rotated(s):     in range(len(s)):         s2 = s[i:] + s[:i]         if s2[-1] == "1":             yield int(s2, 2)  bitmap = [] in range(256):     s = "{:08b}".format(i)     try:         r = min(rotated(s))     except valueerror:         r =     bitmap.append(r)  bitmap = np.array(bitmap, np.uint8) 

then can use bitmap , numpy.packbits() , numpy.unpackbits():

a = np.random.randint(0, 2, (10, 8)) = np.vstack((a, np.array([[1,1,0,0,0,0,0,1]]))) b = np.unpackbits(bitmap[np.packbits(a, axis=1)], axis=1) print print print b 

here output:

[[0 1 1 0 1 1 1 1]  [1 1 1 0 0 1 0 0]  [0 0 0 1 0 1 1 0]  [0 1 1 1 1 0 1 0]  [1 0 1 1 0 1 1 0]  [0 1 0 1 1 1 1 1]  [0 1 0 1 1 1 1 0]  [1 0 0 1 1 0 1 0]  [1 0 0 0 0 0 1 1]  [0 0 0 1 1 0 1 0]  [1 1 0 0 0 0 0 1]]  [[0 1 1 0 1 1 1 1]  [0 0 1 0 0 1 1 1]  [0 0 0 0 1 0 1 1]  [0 0 1 1 1 1 0 1]  [0 1 0 1 1 0 1 1]  [0 1 0 1 1 1 1 1]  [0 0 1 0 1 1 1 1]  [0 0 1 1 0 1 0 1]  [0 0 0 0 0 1 1 1]  [0 0 0 0 1 1 0 1]  [0 0 0 0 0 1 1 1]] 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -