---
title: "ClikaRT::ops::scatter"
sidebar_label: "scatter"
description: "A copy of input with src written at positions given by index along dim, the write mirror of gather: out[index[p]][j][k] = src[p] for dim = 0 (only that axis's coordinate is redirected)."
---

<!-- Generated by tools/doxygen2mdx from Doxygen XML. Do not edit. -->

<span class="api-kind api-kind--function">function</span>

## scatter() {#scatter}

<div class="api-signature">

[`Tensor`](../Tensor.md) **`scatter`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`Tensor`](../Tensor.md) `input`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`int64_t` `dim`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`Tensor`](../Tensor.md) `index`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`ScalarOrTensor`](../ScalarOrTensor.md) `src`<br />
)

</div>

A copy of `input` with `src` written at positions given by [`index`](./index-fn.md#index) along `dim`, the write mirror of [`gather`](./gather.md#gather): `out[`[`index`](./index-fn.md#index)`[p]][j][k] = src[p]` for `dim = 0` (only that axis's coordinate is redirected).

A scalar `src` broadcasts one value to every indexed position; a tensor `src` matches [`index`](./index-fn.md#index)'s shape. On duplicate destinations one write wins; use [`scatter_add`](./scatter_add.md#scatter_add) / [`scatter_reduce`](./scatter_reduce.md#scatter_reduce) for well-defined accumulation. Index dtype law as [`gather`](./gather.md#gather).

**Throws**

- `ClikaRT::Error`: (`INVALID_ARGUMENT`) when `dim` is out of range, the shapes disagree, or the index dtype is wrong (`code_name()` carries the reason).

**Returns:** a new tensor shaped like `input`.

```cpp
auto onehot = ClikaRT::ops::scatter(zeros, 1, ids, 1.0);
```

<div class="api-declared">

Declared in `ClikaRT/compute/ops.h`, line 2650

</div>
