<!--
{
  "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/replacing(with:where:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Core ML",
      "CoreML"
    ],
    "preciseIdentifier" : "s:6CoreML8MLTensorV9replacing4with5whereA2C_ACtF::OverloadGroup"
  },
  "title" : "replacing(with:where:)"
}
-->

# replacing(with:where:)

Returns a new tensor replacing values from `other` with the corresponding element in `self` where the
associated element in `mask` is `true`.

```
func replacing(with replacement: MLTensor, where mask: MLTensor) -> MLTensor
```

## Parameters

`replacement`

The replacement values where `mask` is `true`.

`mask`

The Boolean mask that determines whether the corresponding element / row should be taken from `self`
(if the element in `mask` is `false`) or `other` (if `true`).

## Return Value

A new tensor of the same shape and type as `self`.

## Discussion

For example:

```swift
let x = MLTensor([1, 2, 3], scalarType: Float.self)
let y = MLTensor([4, 5, 6], scalarType: Float.self)
let mask = MLTensor([false, true, false])
let z = x.replacing(with: y, where: mask)
await z.shapedArray(of: Float.self) // is [1, 5, 3]
```

---

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)