---
title: "slice(at:)"
framework: coreai
role: symbol
role_heading: Instance Method
path: "coreai/ndarray/rawview/slice(at:)-kd5b"
---

# 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.RawView
```

## 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, reintroduce the scalar type, and then access a span over it (or use withUnsafePointer if not contiguous). /// Returns the sum of the given row. func sumOfRow(   of rawView: borrowing NDArray.RawView,   row: Int ) -> Float {   let rowSlice = rawView.slice(at: [row]).view(as: Float.self)   let elements = rowSlice.contiguousElements! // contiguous row expected in this case

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