I have two arrays by shape c = (2, 10, 2, 3)
and p = (2, 5, 3)
. I want to pick the first vectors in the 3d dimension of c
and minus it from each vectors in the corresponding row of p
. It will be better understandable by this example, we can do this operation on the first row by c[0, :, None, 0] - p[0]
or c[0, :, 0][:, None] - p[0]
, which will get result with shape (10, 5, 3)
. But I have confused how to apply this operation on all rows at once by advanced indexing, which result will be as shape (2, 10, 5, 3)
.
p = np.array([[[0., -0.05, 0.], [0., -0.05, 0.], [0., 0., 0.], [-0.05, -0.1, 0.05], [0.1, 0.2, 0.1]],
[[0., -0.05, 0.], [0., -0.05, 0.], [0., 0., 0.], [-0.05, -0.1, 0.05], [0.1, 0.2, 0.1]]])
c = np.array([[[[ 0.05, 0. , 0.05], [ 0.05, -0.1, 0.05]], [[ 0.05, -0.1, 0.05], [-0.05, -0.1, 0.05]],
[[-0.05, -0.1, 0.05], [-0.05, 0. , 0.05]], [[ 0.05, 0. , -0.05], [ 0.05, -0.1, -0.05]],
[[ 0.05, -0.1, -0.05], [-0.05, -0.1, -0.05]], [[-0.05, -0.1, -0.05], [-0.05, 0. , -0.05]],
[[ 0.05, 0. , 0.05], [ 0.05, 0. , -0.05]], [[ 0.05, -0.1, 0.05], [ 0.05, -0.1, -0.05]],
[[-0.05, -0.1, 0.05], [-0.05, -0.1, -0.05]], [[-0.05, 0. , 0.05], [-0.05, 0. , -0.05]]],
[[[ 0.05, 0.1, 0.05], [ 0.05, 0. , 0.05]], [[ 0.05, 0. , 0.05], [-0.05, 0. , 0.05]],
[[-0.05, 0. , 0.05], [-0.05, 0.1, 0.05]], [[ 0.05, 0.1, -0.05], [ 0.05, 0. , -0.05]],
[[ 0.05, 0. , -0.05], [-0.05, 0. , -0.05]], [[-0.05, 0. , -0.05], [-0.05, 0.1, -0.05]],
[[ 0.05, 0.1, 0.05], [ 0.05, 0.1, -0.05]], [[ 0.05, 0. , 0.05], [ 0.05, 0. , -0.05]],
[[-0.05, 0. , 0.05], [-0.05, 0. , -0.05]], [[-0.05, 0.1, 0.05], [-0.05, 0.1, -0.05]]]])
additional suggestion:
It is not the main issue, but will be helpful for me and I will be grateful for any alternative way to handle this:
I have broadcasted p
from shape (5, 3)
to (2, 5, 3)
to be corresponded with 1st dimension of c
. Is there a better way, without this broadcasting (may be just by advanced indexing instead), to handle this problem?