<!--
{
  "availability" : [
    "iOS: 11.0.0 -",
    "iPadOS: 11.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.13.0 -",
    "tvOS: 11.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 4.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreML",
  "identifier" : "/documentation/CoreML/MLMultiArray/subscript(_:)-2hh91",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Subscript",
  "symbol" : {
    "kind" : "Instance Subscript",
    "modules" : [
      "Core ML",
      "CoreML"
    ],
    "preciseIdentifier" : "c:objc(cs)MLMultiArray(im)objectAtIndexedSubscript:"
  },
  "title" : "subscript(_:)"
}
-->

# subscript(_:)

Accesses the multiarray by using a linear offset.

```
subscript(idx: Int) -> NSNumber { get set }
```

## Parameters

`idx`

A linear offset index that represents a position the multiarray.

## Return Value

A number.

## Discussion

Use the subscript to linearly index a one-dimensional multiarray in the same way as a conventional array.

```swift
let oneDimensionalArray = try MLMultiArray(shape: [42], dataType: .int32)
let numberValue = oneDimensionalArray[7]
```

Multiarrays with more than one dimension order their elements in row-major order. In these cases, calculate a linear offset by summing the products of each dimension’s index with the dimension’s stride (See `MLMultiArray/strides`).

```swift
let multiArray = try MLMultiArray(shape: [5, 22, 17], dataType: .double)

let dimension0 = 3
let dimension1 = 5
let dimension2 = 7

var linearIndex = 0
linearIndex += dimension0 * multiArray.strides[0].intValue
linearIndex += dimension1 * multiArray.strides[1].intValue
linearIndex += dimension2 * multiArray.strides[2].intValue

let previousNumberValue = multiArray[linearIndex]
multiArray[linearIndex] = 2.718_28
```

---

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)