Explain making spial numbers with numpy?

Viewed 19

im trying to understand what this statment does as im working on a print a range of numpers is spiral way like this

1 2 3
8 9 4
7 6 5

I checked throw google and I saw that we can solve it by numpy as below

def spiral(n):
    a = np.arange(n*n)
    d = np.reshape([n,n])
    m = None
    for i in range(n, 0, -2):
        m = np.r_[m, b[0, :], b[1:, -1], b[-1, :-1][::-1], b[1:-1, 0][::-1]]
        b = b[1:-1, 1:-1] 
    a[list(m[1:])]=list(a)   
    return a.reshape((n,n)) + 1

and It will do the exact same What I want but What is happening inside the for loop statement as I know that np.r_ will merge the slices together but still I didn't get it well.

Do any one explain it to me?

Thanks on advanced.

0 Answers
Related