Python: Justifying NumPy array

后端 未结 2 2176
时光说笑
时光说笑 2020-11-22 00:29

Please I am a bit new to Python and it has been nice, I could comment that python is very sexy till I needed to shift content of a 4x4 matrix which I want to u

2条回答
  •  清歌不尽
    2020-11-22 01:21

    Thanks to all this is what I later use

    def justify(a, direction):
        mask = a>0
        justified_mask = numpy.sort(mask,0) if direction == 'up' or direction =='down' else numpy.sort(mask, 1)
        if direction == 'up':
            justified_mask = justified_mask[::-1]
        if direction =='left':
            justified_mask = justified_mask[:,::-1]
        if direction =='right':
            justified_mask = justified_mask[::-1, :]    
        out = numpy.zeros_like(a) 
        out.T[justified_mask.T] = a.T[mask.T]
        return out
    

提交回复
热议问题