<!--
{
  "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/bandPart(lowerBandCount:upperBandCount:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Core ML",
      "CoreML"
    ],
    "preciseIdentifier" : "s:6CoreML8MLTensorV8bandPart14lowerBandCount05uppergH0ACSi_SitF"
  },
  "title" : "bandPart(lowerBandCount:upperBandCount:)"
}
-->

# bandPart(lowerBandCount:upperBandCount:)

Returns a new tensor with the same shape where everything outside a central band in each innermost matrix is set to zero.

```
func bandPart(lowerBandCount: Int, upperBandCount: Int) -> MLTensor
```

## Parameters

`lowerBandCount`

The number of diagonals in the lower triangle to keep. If `-1`, keep the entire lower triangle.

`upperBandCount`

The number of diagonals in the upper triangle to keep. If `-1`, keep the entire upper triangle.

## Return Value

The band part of the tensor.

## Discussion

The framework copies a diagonal band of values from the tensor to the result tensor of the same size.
A coordinate `[..., i, j]` is considered in band if

```md
(lowerBandCount < 0 || (i-j) <= lowerBandCount) && (upperBandCount < 0 || (j-i) <= upperBandCount)
```

Values outside of the band are set to `0`.

For example:

```swift
let t = Tensor([
    [ 5,  1,  2, 3],
    [-1,  5,  1, 2],
    [-2, -1,  5, 1],
    [-3, -2, -1, 5]
], scalarType: Float.self)

t.bandPart(lowerBandCount: 0, upperBandCount: 0)
// [[ 5,  0,  0, 0]
//  [ 0,  5,  0, 0]
//  [ 0,  0,  5, 0]
//  [ 0,  0,  0, 5]]

t.bandPart(lowerBandCount: 0, upperBandCount: -1)
// [[ 5,  1,  2, 3]
//  [ 0,  5,  1, 2]
//  [ 0,  0,  5, 1]
//  [ 0,  0,  0, 5]]

t.bandPart(lowerBandCount: -1, upperBandCount: 0)
// [[ 5,  0,  0, 0]
//  [-1,  5,  0, 0]
//  [-2, -1,  5, 0]
//  [-3, -2, -1, 5]]
```

---

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)