<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreML",
  "identifier" : "/documentation/CoreML/MLShapedArraySlice",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Core ML"
    ],
    "preciseIdentifier" : "s:6CoreML18MLShapedArraySliceV"
  },
  "title" : "MLShapedArraySlice"
}
-->

# MLShapedArraySlice

A multidimensional subset of elements from a shaped array type.

```
struct MLShapedArraySlice<Scalar> where Scalar : MLShapedArrayScalar
```

## Topics

### Creating a shaped array slice

[`init(scalar: Scalar)`](/documentation/CoreML/MLShapedArraySlice/init(scalar:))

Creates a shaped array slice with exactly one value and zero dimensions.

[`init<S>(scalars: S, shape: [Int])`](/documentation/CoreML/MLShapedArraySlice/init(scalars:shape:))

Initialize with a sequence and the shape.

[`init(mutating: CVPixelBuffer, shape: [Int])`](/documentation/CoreML/MLShapedArraySlice/init(mutating:shape:))

Creates a new `MLShapedArraySlice` using a pixel buffer as the backing storage.

### Creating a shaped array slice from another type

[`init(MLMultiArray)`](/documentation/CoreML/MLShapedArraySlice/init(_:))

Creates a new MLShapedArraySlice using a `MLMultiArray` as a backing storage.

[`init<S>(concatenating: S, alongAxis: Int)`](/documentation/CoreML/MLShapedArraySlice/init(concatenating:alongAxis:))

Merges a sequence of shaped arrays into one shaped array along an axis.

### Creating a shaped array slice with pointers to memory

[`init(unsafeUninitializedShape: [Int], initializingWith: (inout UnsafeMutableBufferPointer<Scalar>, [Int]) throws -> Void) rethrows`](/documentation/CoreML/MLShapedArraySlice/init(unsafeUninitializedShape:initializingWith:))

Creates a shaped array slice from a shape and a closure that initializes its memory.

### Creating a shaped array slice with data

[`init(data: Data, shape: [Int])`](/documentation/CoreML/MLShapedArraySlice/init(data:shape:))

Creates a shaped array with a defined data and shape.

[`init(data: Data, shape: [Int], strides: [Int])`](/documentation/CoreML/MLShapedArraySlice/init(data:shape:strides:))

Creates a shaped array with defined data, shape, and strides.

### Shaping the array slice

[`func changingLayout(to: MLShapedArrayBufferLayout) -> MLShapedArraySlice<Scalar>`](/documentation/CoreML/MLShapedArraySlice/changingLayout(to:))

Returns a copy with the specified buffer layout.

[`func expandingShape(at: Int) -> MLShapedArraySlice<Scalar>`](/documentation/CoreML/MLShapedArraySlice/expandingShape(at:))

Returns a new shaped array with expanded dimensions

[`func reshaped(to: [Int]) -> MLShapedArraySlice<Scalar>`](/documentation/CoreML/MLShapedArraySlice/reshaped(to:))

Returns a new reshaped shaped array.

[`func squeezingShape() -> MLShapedArraySlice<Scalar>`](/documentation/CoreML/MLShapedArraySlice/squeezingShape())

Returns a new squeezed shaped array.

[`func transposed() -> MLShapedArraySlice<Scalar>`](/documentation/CoreML/MLShapedArraySlice/transposed())

Returns a new transposed shaped array

[`func transposed(permutation: [Int]) -> MLShapedArraySlice<Scalar>`](/documentation/CoreML/MLShapedArraySlice/transposed(permutation:))

Returns a new transposed shaped array using a custom permutation.

### Modifying a shaped array type

[`func withUnsafeMutableShapedBufferPointer<R>(using: MLShapedArrayBufferLayout, (inout UnsafeMutableBufferPointer<Scalar>, [Int], [Int]) throws -> R) rethrows -> R`](/documentation/CoreML/MLShapedArraySlice/withUnsafeMutableShapedBufferPointer(using:_:))

Calls the given closure with a pointer to the array’s mutable storage that has a
specified buffer layout.

### Encoding and decoding an array slice

[`init(from: any Decoder) throws`](/documentation/CoreML/MLShapedArraySlice/init(from:))

Creates an array slice from the passed decoder.

[`func encode(to: any Encoder) throws`](/documentation/CoreML/MLShapedArraySlice/encode(to:))

Encodes the array slice.

### Default Implementations

[Decodable Implementations](/documentation/CoreML/MLShapedArraySlice/Decodable-Implementations)

[Encodable Implementations](/documentation/CoreML/MLShapedArraySlice/Encodable-Implementations)

## Relationships

### Conforms To

[`Copyable`](/documentation/Swift/Copyable)

[`BidirectionalCollection`](/documentation/Swift/BidirectionalCollection)

[`Encodable`](/documentation/Swift/Encodable)

[`ExpressibleByArrayLiteral`](/documentation/Swift/ExpressibleByArrayLiteral)

[`MutableCollection`](/documentation/Swift/MutableCollection)

[`RandomAccessCollection`](/documentation/Swift/RandomAccessCollection)

[`MLShapedArrayProtocol`](/documentation/CoreML/MLShapedArrayProtocol)

[`Collection`](/documentation/Swift/Collection)

[`Equatable`](/documentation/Swift/Equatable)

[`Sequence`](/documentation/Swift/Sequence)

[`Escapable`](/documentation/Swift/Escapable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`Decodable`](/documentation/Swift/Decodable)

[`Sendable`](/documentation/Swift/Sendable)

---

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)