<!--
{
  "availability" : [
    "iOS: 27.0.0 -",
    "iPadOS: 27.0.0 -",
    "tvOS: 27.0.0 -",
    "visionOS: 27.0.0 -",
    "watchOS: 27.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreAI",
  "identifier" : "/documentation/CoreAI/NDArray/MutableView/slice(at:)-qyjq",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Core AI",
      "CoreAI"
    ],
    "preciseIdentifier" : "s:13CoreAIRuntime7NDArrayV11MutableViewV5slice2atAEy_xGSayAC15RangeExpression_pG_tF"
  },
  "title" : "slice(at:)"
}
-->

# slice(at:)

Returns a sub-view with the same rank as this view by slicing the dimensions at the provided ranges.

```
@export(implementation) consuming func slice(at ranges: [any NDArray.RangeExpression]) -> NDArray.MutableView<Element>
```

## Parameters

`ranges`

The range expressions describing where to slice along each dimension.
`ranges.count` must be ≤ `rank`. Unspecified trailing dimensions are assumed to be `.all`.

## Discussion

For example if you have a 3D NDArray and want to increment a specific region, you can slice that region
and then access a span over it (or use `withUnsafeMutablePointer` if not contiguous).

```swift
/// Updates the desired channel and range of rows
func incrementRegion(
  of mutableView: consuming NDArray.MutableView<Float>,
  channel: Int,
  startRow: Int,
  endRow: Int
) {
  var region = mutableView.slice(at: [channel, startRow..<endRow, .all])
  var mutableSpan = region.contiguousElements! // contiguous region expected in this case

  for i in mutableSpan.indices {
    mutableSpan[i] += 1
  }
}
```

> Note: If you need to avoid `consuming` the view which this is called on, you can use ``doc://com.apple.coreai/documentation/CoreAI/NDArray/MutableView/mutatingSlice(at:)-9pmi4``.

---

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)