---
title: "slice(at:)"
framework: coreai
role: symbol
role_heading: Instance Method
path: "coreai/ndarray/view/slice(at:)-32gsh"
---

# slice(at:)

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

## Declaration

```swift
@export(implementation) func slice(at ranges: [any NDArray.RangeExpression]) -> NDArray.View<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

Discussion For example if you have a 2D NDArray and want to compute the sum of a specific row, you can slice that row and then access a span over it (or use withUnsafePointer if not contiguous). /// Returns the sum of the given row. func sumOfRow(   of view: borrowing NDArray.View<Float>,   row: Int ) -> Float {   let rowSlice = view.slice(at: [row])   let elements = rowSlice.contiguousElements! // contiguous row expected in this case

var sum: Float = 0   for i in elements.indices {     sum += elements[i]   }   return sum }
