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

Asked

Viewed 65 times

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.

A formula é 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 matrix are within the range of possible [i] and [j], in the case of example 3x3 with i and j ranging from 0 to 2.

Ideia e exemplo

  • 1

    It is difficult to understand what you need. You could do an example of A and B values and put what should be C?

  • Just a moment..

  • Do this by editing the question, please. Just below it is the [Edit] button. Use the button {} editor to properly format the codes.

1 answer

1

Only use the 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]]

Then we make:

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]]
  • Thanks! I adapted here to my problem and solved. Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.