1

I'm trying to create a np array storing the indexes of the array itself. For example, in a 1D case, I would like to have

[[0],[1],[2],[3]]

while in a 2d case for a 2x2 array I would like to have

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

The only way of doing it I found so far is a bunch of loops - not too efficient considering that the array I have to handle have 1 million+ entries:

    idx_array = np.empty(shape=kernel_shape+[4])

    for ker_x in range(kernel_shape[0]):
        for ker_y in range(kernel_shape[1]):
            for in_ch in range(kernel_shape[2]):
                for out_ch in range(kernel_shape[3]):
                    idx_array[ker_x][ker_y][in_ch][out_ch] = [ker_x, ker_y, in_ch, out_ch]

Is there a better way of implementing this?

Thanks!

asa
  • 39
  • 1
  • 2
  • I linked to one duplicate: I'd use moveaxis as [here](https://stackoverflow.com/questions/42302348/create-an-array-where-each-element-stores-its-indices/42302496#42302496) instead of stack, but it amounts to the same thing. – DSM Nov 04 '18 at 15:25

0 Answers0