<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "tvOS: 18.0.0 -",
    "visionOS: 2.0.0 -",
    "watchOS: 11.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreML",
  "identifier" : "/documentation/CoreML/MLTensor/init(stacking:alongAxis:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Core ML",
      "CoreML"
    ],
    "preciseIdentifier" : "s:6CoreML8MLTensorV8stacking9alongAxisACx_SitcSlRzAC7ElementRtzlufc"
  },
  "title" : "init(stacking:alongAxis:)"
}
-->

# init(stacking:alongAxis:)

Stacks the given tensors along the `axis` dimension into a new tensor with rank one higher than the current tensor and
each tensor.

```
init(stacking tensors: some Collection<MLTensor>, alongAxis axis: Int = 0)
```

## Parameters

`tensors`

The tensors to stack. All tensors must have the same shape and scalar type.

`axis`

The axis along which to stack. Negative values wrap around but must be in the range `[-rank, rank]`, where `rank`
is the rank of the provided tensors.

## Discussion

Given that `tensors` all have shape `[A, B, C]`, and `tensors.count = N`, then:

- if `axis == 0` then the resulting tensor will have the shape `[N, A, B, C]`.
- if `axis == 1` then the resulting tensor will have the shape `[A, N, B, C]`.
- etc.

For example:

```swift
// 'x' is [1, 4]
// 'y' is [2, 5]
// 'z' is [3, 6]
MLTensor(stacking: [x, y, z]) // is [[1, 4], [2, 5], [3, 6]]
MLTensor(stacking: [x, y, z], alongAxis: 1) // is [[1, 2, 3], [4, 5, 6]]
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)