<!--
{
  "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:nonZeroGatingCount:)-93v23",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "s:10Accelerate4vDSPO8compress_12gatingVector18nonZeroGatingCountSaySdGx_q_SiSgtAA0A6BufferRzAaIR_Sd7ElementRtzSdAJRt_r0_lFZ"
  },
  "title" : "compress(_:gatingVector:nonZeroGatingCount:)"
}
-->

# compress(_:gatingVector:nonZeroGatingCount:)

Returns a compressed copy of the specified double-precision vector using the nonzero values in a gating vector.

```
static func compress<T, U>(_ vector: T, gatingVector: U, nonZeroGatingCount: Int?) -> [Double] where T : AccelerateBuffer, U : AccelerateBuffer, T.Element == Double, U.Element == Double
```

## Parameters

`vector`

The source vector that the function compresses.

`gatingVector`

The gating vector.

`nonZeroGatingCount`

The number of nonzero elements in `gatingVector`. Set to `nil` to have the operation calculate this value for you.

## Return Value

The result of the compression operation.

## 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 destination = vDSP.compress(source,
                                gatingVector: gatingVector,
                                nonZeroGatingCount: nil)

// 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)