The following is an example code which compute array B
from A
:
import numpy as np
idx1 = np.array([
[3, 0, 0],
[2, 1, 0],
[2, 0, 1],
[1, 2, 0],
[1, 1, 1],
[1, 0, 2],
[0, 3, 0],
[0, 2, 1],
[0, 1, 2],
[0, 0, 3]])
idx2 = np.arange(3)
A = np.arange(10*4*3).reshape(10, 4, 3)
B = np.prod(A[:, idx1, idx2], axis=2)
Notice the line
B = np.prod(A[:, idx1, idx2], axis=2)
Is this line memory efficent? Or does numpy
will generate some internal array for A[:, idx1, idx2]
?
One can image that if len(A)
is very large, and numpy
generate some internal array for A[:, idx1, idx2]
, it is not memory efficient. Does there exist any better way to do such thing?