Home > OS >  Translate array into x and y direction - Python
Translate array into x and y direction - Python

Time:06-22

We have the following two-dimensional array with x and y coordinates:

x = np.array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]])

We flatten it: x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) )

and our goal is to apply translations into x direction, y direction.

We are dealing with a 4x4 array (lattice), and the first transformation is 1 shift into x direction : so from '[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]' we get '[1, 2, 3, 0, 5, 6, 7, 4, 9, 10, 11, 8, 13, 14, 15, 12]'.

The next transformation is two shifts in x: from '[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]' we get '[2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9, 14, 15, 12, 13]'.

We want to get this (flattened) array:

y = np.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
              [1, 2, 3, 0, 5, 6, 7, 4, 9, 10, 11, 8, 13, 14, 15, 12],
              [2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9, 14, 15, 12, 13],
              [3, 0, 1, 2, 7, 4, 5, 6, 11, 8, 9, 10, 15, 12, 13, 14],
              [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3],
              [5, 6, 7, 4, 9, 10, 11, 8, 13, 14, 15, 12, 1, 2, 3, 0],
              [6, 7, 4, 5, 10, 11, 8, 9, 14, 15, 12, 13, 2, 3, 0, 1],
              [7, 4, 5, 6, 11, 8, 9, 10, 15, 12, 13, 14, 3, 0, 1, 2],
              [8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7],
              [9, 10, 11, 8, 13, 14, 15, 12, 1, 2, 3, 0, 5, 6, 7, 4],
              [10, 11, 8, 9, 14, 15, 12, 13, 2, 3, 0, 1, 6, 7, 4, 5],
              [11, 8, 9, 10, 15, 12, 13, 14, 3, 0, 1, 2, 7, 4, 5, 6],
              [12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
              [13, 14, 15, 12, 1, 2, 3, 0, 5, 6, 7, 4, 9, 10, 11, 8],
              [14, 15, 12, 13, 2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9],
              [15, 12, 13, 14, 3, 0, 1, 2, 7, 4, 5, 6, 11, 8, 9, 10]])

I tried using:

y = np.roll(np.roll(x, -1), -1)  

CodePudding user response:

Can concatenate two vstack operations. First, roll in axis=1 and then, roll in axis=0.

np.vstack([np.roll(np.roll(arr, -i, axis=0), -x, axis=1).flatten() \
           for x in range(arr.shape[0])] \
           for i in range(arr.shape[1]))

array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15],
       [ 1,  2,  3,  0,  5,  6,  7,  4,  9, 10, 11,  8, 13, 14, 15, 12],
       [ 2,  3,  0,  1,  6,  7,  4,  5, 10, 11,  8,  9, 14, 15, 12, 13],
       [ 3,  0,  1,  2,  7,  4,  5,  6, 11,  8,  9, 10, 15, 12, 13, 14],
       [ 4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,  0,  1,  2,  3],
       [ 5,  6,  7,  4,  9, 10, 11,  8, 13, 14, 15, 12,  1,  2,  3,  0],
       [ 6,  7,  4,  5, 10, 11,  8,  9, 14, 15, 12, 13,  2,  3,  0,  1],
       [ 7,  4,  5,  6, 11,  8,  9, 10, 15, 12, 13, 14,  3,  0,  1,  2],
       [ 8,  9, 10, 11, 12, 13, 14, 15,  0,  1,  2,  3,  4,  5,  6,  7],
       [ 9, 10, 11,  8, 13, 14, 15, 12,  1,  2,  3,  0,  5,  6,  7,  4],
       [10, 11,  8,  9, 14, 15, 12, 13,  2,  3,  0,  1,  6,  7,  4,  5],
       [11,  8,  9, 10, 15, 12, 13, 14,  3,  0,  1,  2,  7,  4,  5,  6],
       [12, 13, 14, 15,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11],
       [13, 14, 15, 12,  1,  2,  3,  0,  5,  6,  7,  4,  9, 10, 11,  8],
       [14, 15, 12, 13,  2,  3,  0,  1,  6,  7,  4,  5, 10, 11,  8,  9],
       [15, 12, 13, 14,  3,  0,  1,  2,  7,  4,  5,  6, 11,  8,  9, 10]])
  • Related