How to use indices of two arrays to form a third array?

0

Basically what I want to do is C[i][j] = B[A[i][j]][i] . In the case A and B are 2 arrays 5x5.

The formula is C [i, j] = B [A [i] [j]] [i] that is, C is the value of B [X] [i], where X is the value of A [i] [j]. Note that the values I have here in the array are within the range of possible [i] and [j], in case of example 3x3 with i and j ranging from 0 to 2.

    
asked by anonymous 15.08.2018 / 16:33

1 answer

1

Only use for ...

Let A and B be:

A = [[0,2,1],[1,2,0],[2,1,0]]
B = [[1,0,2],[2,1,0],[2,0,1]]

Here we do:

C = []
for i in range(len(B)):
    linha_C = []
    for j in range(len(B[i])):
      linha_C.append(B[A[i][j]][i])
    C.append(linha_C)

>>> print C
[[1, 2, 2], [1, 0, 0], [1, 0, 2]]
    
15.08.2018 / 19:17