<!--
{
  "availability" : [
    "iOS: 14.0.0 -",
    "iPadOS: 14.0.0 -",
    "macCatalyst: -",
    "macOS: 11.0.0 -",
    "tvOS: 14.0.0 -",
    "visionOS: -",
    "watchOS: 7.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/vDSP/compress(_:gatingVector:result:)-2yse4",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "s:10Accelerate4vDSPO8compress_12gatingVector6resultyx_q_q0_ztAA0A6BufferRzAaGR_AA0a7MutableG0R0_Sd7ElementRtzSdAIRt_SdAIRt0_r1_lFZ"
  },
  "title" : "compress(_:gatingVector:result:)"
}
-->

# compress(_:gatingVector:result:)

Compresses the specified double-precision vector using the nonzero values in a gating vector.

```
static func compress<T, U, V>(_ vector: T, gatingVector: U, result: inout V) where T : AccelerateBuffer, U : AccelerateBuffer, V : AccelerateMutableBuffer, T.Element == Double, U.Element == Double, V.Element == Double
```

## Parameters

`vector`

The source vector that the function compresses.

`gatingVector`

The gating vector.

`result`

The destination vector that receives the result.

## Discussion

The following code shows an example of compressing the values in `source` using the nonzero values in `gatingVector`:

```swift
let source: [Double] = [1, 2,
                        3, 4,
                        5, 6,
                        7, 8]

let gatingVector: [Double] = [-1, 0,
                              1, 0,
                              0.001, 0,
                              10, 0]

let count = gatingVector.filter {
    !$0.isZero
}.count

let destination = [Double](unsafeUninitializedCapacity: count) {
    buffer, initializedCount in
    
    vDSP.compress(source,
                  gatingVector: gatingVector,
                  result: &buffer)
    
    initializedCount = count
}

// Prints "[1.0, 3.0, 5.0, 7.0]".
print(destination)
```

---

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)