Consider array a
whose columns hold random values taken from 1, 2, 3:
a = np.array([[2, 3, 1, 3],
[3, 2, 1, 3],
[1, 1, 1, 2],
[1, 3, 2, 3],
[3, 3, 1, 3],
[2, 1, 3, 2]])
Now, consider array b
whose first 2 columns hold the 9 possible pairs of values taken from 1, 2, 3 (the order of the pair elements is important). The 3rd column of b
associates a non-negative integer with each pairing.
b = np.array([[1, 1, 6],
[1, 2, 0],
[1, 3, 9],
[2, 1, 6],
[2, 2, 0],
[2, 3, 4],
[3, 1, 1],
[3, 2, 0],
[3, 3, 8]])
I need help with code that produces array c
where vertically adjacent elements in a
are replaced with matching values from the 3rd column of b
.
For example, the first column of 'a' moves down from 2 to 3 to 1 to 1 to 3 to 2. So, the first column of c
would hold values 4, 1, 6, 9, 0. The same idea applies to every column of a
. We see that pair order is important (moving from 3 to 1 produces value 1, while moving from 1 to 3 produces value 9.
The output of this small example would be:
c = np.array([[4, 0, 6, 8],
[1, 6, 6, 0],
[6, 9, 0, 4],
[9, 8, 6, 8],
[0, 1, 9, 0]])
Because this code will be executed a vast number of times, I'm hoping there is a speedy vectorized solution. Thanks.