put_along_axis

paddle. put_along_axis ( arr, indices, values, axis, reduce='assign' ) [source]

Put values into the destination array by given indices matrix along the designated axis.

Parameters
  • arr (Tensor) – The Destination Tensor. Supported data types are float32 and float64.

  • indices (Tensor) – Indices to put along each 1d slice of arr. This must match the dimension of arr, and need to broadcast against arr. Supported data type are int and int64.

  • axis (int) – The axis to put 1d slices along.

  • reduce (string | optinal) – The reduce operation, default is ‘assign’, support ‘add’, ‘assign’, ‘mul’ and ‘multiply’.

Returns :

Tensor: The indexed element, same dtype with arr

Examples

import paddle
import numpy as np

x_np = np.array([[10, 30, 20], [60, 40, 50]])
index_np = np.array([[0]])
x = paddle.to_tensor(x_np)
index = paddle.to_tensor(index_np)
value = 99
axis = 0
result = paddle.put_along_axis(x, index, value, axis)
print(result)
# [[99, 99, 99],
# [60, 40, 50]]