# Reshape vector of odd length to multi-dimensional array

**URL:** <https://discourse.julialang.org/t/reshape-vector-of-odd-length-to-multi-dimensional-array/76465>\
**Category:** New to Julia\
**Tags:** reshaping\
**Created:** [February 14, 2022, 10:53pm UTC](https://discourse.julialang.org/t/reshape-vector-of-odd-length-to-multi-dimensional-array/76465 "2022-02-14T22:53:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lln](https://avatars.discourse-cdn.com/v4/letter/l/ecae2f/32.png) [@lln](https://discourse.julialang.org/u/lln)\
**Post date:** [February 14, 2022, 10:53pm UTC](https://discourse.julialang.org/t/reshape-vector-of-odd-length-to-multi-dimensional-array/76465/1 "2022-02-14T22:53:15Z")

</div>

Hi,

I’m trying to reshape a vector of odd length (15) into a multi-dimensional array and getting a `DimensionMismatch("array size 15 must be divisible by the product of the new dimensions (2, Colon())")` error, which makes sense but I don’t know the workaround this. Numpy automatically takes care of this

Here’s the code:  
Julia

```julia
A = Vector(1:15)
reshape(A, 2, :) # throws an error

```

Python

```julia
import numpy as np
A = [i for i in range(1, 16)]
np.array(A, ndmin=2)

```

Please assist. Thanks

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [February 14, 2022, 10:55pm UTC](https://discourse.julialang.org/t/reshape-vector-of-odd-length-to-multi-dimensional-array/76465/2 "2022-02-14T22:55:49Z")

</div>

> [@lln](#):
>
> Numpy automatically takes care of this.

How does it take care of this?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [February 14, 2022, 10:59pm UTC](https://discourse.julialang.org/t/reshape-vector-of-odd-length-to-multi-dimensional-array/76465/3 "2022-02-14T22:59:20Z")

</div>

```python
>>> import numpy as np
>>> A = [i for i in range(1, 16)]
>>> np.array(A, ndmin=2)
array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]])

```

> **ndmin** int, optional  
> Specifies the minimum number of dimensions that the resulting array should have. Ones will be pre-pended to the shape as needed to meet this requirement.

Does this do what you’re looking for?

```julia
julia> reshape(Vector(1:15), 1, :)
1×15 Matrix{Int64}:
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

```

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [February 14, 2022, 11:06pm UTC](https://discourse.julialang.org/t/reshape-vector-of-odd-length-to-multi-dimensional-array/76465/4 "2022-02-14T23:06:23Z")

</div>

> [@stillyslalom](#):
>
> Ones will be pre-pended to the shape as needed to meet this requirement.

What might be the idea here (vs. zero, ±Inf or NaN)?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [February 14, 2022, 11:32pm UTC](https://discourse.julialang.org/t/reshape-vector-of-odd-length-to-multi-dimensional-array/76465/5 "2022-02-14T23:32:50Z")

</div>

The wording is a bit confusing - ones are not prepended to the _array_, but rather to its shape:

```python
>>> np.shape(A)
(15,)
>>> np.array(A, ndmin=2).shape
(1, 15)

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [February 15, 2022, 12:10am UTC](https://discourse.julialang.org/t/reshape-vector-of-odd-length-to-multi-dimensional-array/76465/6 "2022-02-15T00:10:24Z")

</div>

Yep, and `np.array(range(1, 17), ndmin=2).shape` is `(1, 16)`, so that function does not try to make a shape of `(2, X)` even when it’s possible, it’s just not what `ndmin` means.

There is a Python equivalent of `reshape(1:16, 2, :)`. `np.reshape(range(1, 17), (2, -1))` does make a shape of (2, 8), where `-1` can specify (only) one dimension of unknown length. Either way, `1:15`/`range(1, 16)` will throw an error because `(2, 7.5)` isn’t a valid shape.

---

<div class="post-metadata">

**Author:** ![lln](https://avatars.discourse-cdn.com/v4/letter/l/ecae2f/32.png) [@lln](https://discourse.julialang.org/u/lln)\
**Post date:** [February 15, 2022, 12:17am UTC](https://discourse.julialang.org/t/reshape-vector-of-odd-length-to-multi-dimensional-array/76465/7 "2022-02-15T00:17:43Z")

</div>

Thanks! The wording, especially `ndim`, was confusing me.
