1

Consider 3D tensor of T(w x h x d).

The goal is to create a tensor of R(w x h x K) where K = d x k by tiling along 3rd dimension in a unique way.

The tensor should repeat each slice in 3rd dimension k times, meaning :

T[:,:,0]=R[:,:,0:k] and T[:,:,1]=R[:,:,k:2*k]

There's a subtle difference with standard tiling which gives T[:,:,0]=R[:,:,::k], repeats at every kth in 3rd dimension.

Divakar
  • 218,885
  • 19
  • 262
  • 358
user2527599
  • 69
  • 10

1 Answers1

1

Use np.repeat along that axis -

np.repeat(T,k,axis=2) 

Sample run -

In [688]: # Setup
     ...: w,h,d = 2,3,4
     ...: k = 2
     ...: T = np.random.randint(0,9,(w,h,d))
     ...: 
     ...: # Original approach
     ...: R = np.zeros((w,h,d*k),dtype=T.dtype)
     ...: for i in range(4):
     ...:     R[:,:,i*k:(i+1)*k] = T[:,:,i][...,None]
     ...:  

In [692]: T
Out[692]: 
array([[[4, 5, 6, 4],
        [5, 4, 4, 3],
        [8, 0, 0, 8]],

       [[7, 3, 8, 0],
        [8, 7, 0, 8],
        [3, 6, 8, 5]]])


In [690]: R
Out[690]: 
array([[[4, 4, 5, 5, 6, 6, 4, 4],
        [5, 5, 4, 4, 4, 4, 3, 3],
        [8, 8, 0, 0, 0, 0, 8, 8]],

       [[7, 7, 3, 3, 8, 8, 0, 0],
        [8, 8, 7, 7, 0, 0, 8, 8],
        [3, 3, 6, 6, 8, 8, 5, 5]]])

In [691]: np.allclose(R, np.repeat(T,k,axis=2))
Out[691]: True

Alternatively with np.tile and reshape -

np.tile(T[...,None],k).reshape(w,h,-1)
Divakar
  • 218,885
  • 19
  • 262
  • 358