<!--
{
  "availability" : [
    "iOS: 27.0.0 -",
    "iPadOS: 27.0.0 -",
    "tvOS: 27.0.0 -",
    "visionOS: 27.0.0 -",
    "watchOS: 27.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreAI",
  "identifier" : "/documentation/CoreAI/NDArray/init(scalars:shape:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Core AI",
      "CoreAI"
    ],
    "preciseIdentifier" : "s:13CoreAIRuntime7NDArrayV7scalars5shapeACq__SaySiGtcs15BitwiseCopyableRz7ElementQy_RszSTR_r0_lufc"
  },
  "title" : "init(scalars:shape:)"
}
-->

# init(scalars:shape:)

Initialize an ndArray with a copy of some sequence of scalars, stored in the ndArray in row-major order.

```
init<Scalar>(scalars: some Sequence, shape: [Int]) where Scalar : BitwiseCopyable
```

## Parameters

`scalars`

A sequence of scalars to be copied into the new ndArray.
Note that `Scalar` must be a type that corresponds to a scalar type found on the [`NDArray.ScalarType`](/documentation/CoreAI/NDArray/ScalarType-swift.enum) enum.

`shape`

The shape of the new ndArray. The ndArray will be stored in row-major order and the scalars will be assigned in row-major order.

## Discussion

This utility will construct an ndArray with a copy of the contents of some sequence. For example to make an int32 ndArray with increasing values:

```swift
var ndArray = NDArray(scalars: (0..<4) as Range<Int32>, shape: [2, 2])
// The resulting NDArray has contents:
[[0, 1], [2, 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)