getting the opposite diagonal of a numpy array

后端 未结 3 1729
天命终不由人
天命终不由人 2020-12-30 19:26

So in numpy arrays there is the built in function for getting the diagonal indices, but I can\'t seem to figure out how to get the diagonal starting from the top right rathe

3条回答
  •  鱼传尺愫
    2020-12-30 20:16

    There is

    In [47]: np.diag(np.fliplr(array))
    Out[47]: array([ 4,  8, 12, 16, 20])
    

    or

    In [48]: np.diag(np.rot90(array))
    Out[48]: array([ 4,  8, 12, 16, 20])
    

    Of the two, np.diag(np.fliplr(array)) is faster:

    In [50]: %timeit np.diag(np.fliplr(array))
    100000 loops, best of 3: 4.29 us per loop
    
    In [51]: %timeit np.diag(np.rot90(array))
    100000 loops, best of 3: 6.09 us per loop
    

提交回复
热议问题