# Reshape a 1-D array into an array of different-size arrays

**URL:** <https://discourse.julialang.org/t/reshape-a-1-d-array-into-an-array-of-different-size-arrays/25999>\
**Category:** General Usage\
**Created:** [July 4, 2019, 1:09am UTC](https://discourse.julialang.org/t/reshape-a-1-d-array-into-an-array-of-different-size-arrays/25999 "2019-07-04T01:09:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![keeler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keeler/32/7303_2.png) [@keeler](https://discourse.julialang.org/u/keeler)\
**Post date:** [July 4, 2019, 1:09am UTC](https://discourse.julialang.org/t/reshape-a-1-d-array-into-an-array-of-different-size-arrays/25999/1 "2019-07-04T01:09:38Z")

</div>

I have two vectors x and n of equal length, and I want to create an array of arrays y from x, where the lengths for the subarrays of y corresponds to the elements of n.

For example, x=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8] and n=[3,1,4]. Then y=[[0.1,0.2,0.3],[0.4],[0.5,0.6,0.7,0.8]].

Is there a nice/fast way to do this? I tried searching the forum, but I couldn’t find anything directly relevant.

In Python (with NumPy) this would work:

```julia
indexPoints=np.cumsum(n[0:-1]);
y=np.split(x,indexPoints,axis=0);

```

My current stopgap in Julia:

```julia
nCumSum=cumsum(n);
numbTotal=nCumSum[end];
indexFirst=copy(nCumSum);
indexFirst[2:end]=indexFirst[1:end-1].+1;indexFirst[1]=1;
indexSecond=copy(nCumSum);
y=[(x[indexFirst[ii]:indexSecond[ii]]) for ii in 1:length(n)];

```

**Update** : I had a couple of typos in the Julia code and a mistake in the Python code.

---

<div class="post-metadata">

**Author:** ![MaximilianJHuber](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maximilianjhuber/32/2579_2.png) [@MaximilianJHuber](https://discourse.julialang.org/u/MaximilianJHuber)\
**Post date:** [July 4, 2019, 2:12am UTC](https://discourse.julialang.org/t/reshape-a-1-d-array-into-an-array-of-different-size-arrays/25999/2 "2019-07-04T02:12:31Z")

</div>

[This](https://discourse.julialang.org/t/converting-a-matrix-into-an-array-of-arrays/17038) seems applicable. Just adjust for the non-constant `n`.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 4, 2019, 3:15am UTC](https://discourse.julialang.org/t/reshape-a-1-d-array-into-an-array-of-different-size-arrays/25999/3 "2019-07-04T03:15:05Z")

</div>

It seems like you’re trying to do this in a very vectorized style, but that’s neither necessary nor optimal in Julia. Loops in Julia are fast, and doing this operation in a loop is pretty straightforward:

```julia
julia> x=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8];

julia> n=[3,1,4];

julia> function split(x, n)
         result = Vector{Vector{eltype(x)}}()
         start = firstindex(x)
         for len in n
           push!(result, x[start:(start + len - 1)])
           start += len
         end
         result
       end
split (generic function with 1 method)

julia> split(x, n)
3-element Array{Array{Float64,1},1}:
 [0.1, 0.2, 0.3]     
 [0.4]               
 [0.5, 0.6, 0.7, 0.8]

```

```julia
julia> using BenchmarkTools

julia> @btime split($x, $n)
  133.058 ns (5 allocations: 448 bytes)

```

I tried comparing this with the code in the original post, but I get an index error when running your code:

```julia
julia> function split2(x, n)
         nCumSum=cumsum(n)
         numbTotal=nCumSum[end]
         indexFirst=copy(nCumSum)
         indexFirst[2:end]=indexFirst[1:end-1].+1;indexFirst[1]=1
         indexSecond=copy(nCumSum)
         y=[(x[indexFirst[ii]:indexSecond[ii]]) for ii in 1:numbTotal]
       end
split2 (generic function with 1 method)

julia> split2(x, n)
ERROR: BoundsError: attempt to access 3-element Array{Int64,1} at index [4]

```

I also tried comparing with Numpy, but `np.split(x, n, axis=0)` does not actually produce the result you’re asking for:

```julia
In [16]: x=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8]

In [17]: n=[3,1,4]

In [18]: np.split(x, n, axis=0)
Out[18]: 
[array([0.1, 0.2, 0.3]),
 array([], dtype=float64),
 array([0.2, 0.3, 0.4]),
 array([0.5, 0.6, 0.7, 0.8])]

```

---

<div class="post-metadata">

**Author:** ![keeler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keeler/32/7303_2.png) [@keeler](https://discourse.julialang.org/u/keeler)\
**Post date:** [July 4, 2019, 3:28am UTC](https://discourse.julialang.org/t/reshape-a-1-d-array-into-an-array-of-different-size-arrays/25999/4 "2019-07-04T03:28:10Z")

</div>

OK, thanks.

Yes, I had a couple of typos in the Julia code. Sorry about that. And the Python code should have been:

```julia
indexPoints=np.cumsum(n[0:-1]);
y=np.split(x,indexPoints,axis=0);

```

---

<div class="post-metadata">

**Author:** ![keeler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keeler/32/7303_2.png) [@keeler](https://discourse.julialang.org/u/keeler)\
**Post date:** [July 4, 2019, 3:29am UTC](https://discourse.julialang.org/t/reshape-a-1-d-array-into-an-array-of-different-size-arrays/25999/5 "2019-07-04T03:29:54Z")

</div>

And yes, I was looking for a (succinct) vectorized way.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 4, 2019, 3:38am UTC](https://discourse.julialang.org/t/reshape-a-1-d-array-into-an-array-of-different-size-arrays/25999/6 "2019-07-04T03:38:31Z")

</div>

Gotcha. For what it’s worth, the Julia version (with a loop) is ~46 times faster than the vectorized numpy version:

```julia
julia> using BenchmarkTools

julia> @btime split($x, $n)
  133.058 ns (5 allocations: 448 bytes)

```

```julia
In [22]: def mysplit(x, n):
    ...: indexPoints = np.cumsum(n[0:-1])
    ...: return np.split(x, indexPoints, axis=0)
    ...: 
    ...: 

In [23]: mysplit(x, n)
Out[23]: [array([0.1, 0.2, 0.3]), array([0.4]), array([0.5, 0.6, 0.7, 0.8])]

In [24]: %timeit mysplit(x, n)
6.24 µs ± 59.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [July 4, 2019, 3:25pm UTC](https://discourse.julialang.org/t/reshape-a-1-d-array-into-an-array-of-different-size-arrays/25999/7 "2019-07-04T15:25:41Z")

</div>

If you want a pithy two-liner akin to your numpy code, you could just use a comprehension:

```python
indexPoints=np.cumsum(n[0:-1]);
y=np.split(x,indexPoints,axis=0);

```

```julia
index_points = cumsum(n)
[x[i+1:j] for (i,j) in zip([0;index_points[1:end-1]], index_points)]

```

Robin’s `for` loop, though, will certainly still be fastest. That’s one of the big benefits of Julia — you don’t need to bend over backwards to write your algorithm in a vectorized style, and you’re not out of luck if Julia doesn’t have the exact vectorized method you’re looking for.

---

<div class="post-metadata">

**Author:** ![keeler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keeler/32/7303_2.png) [@keeler](https://discourse.julialang.org/u/keeler)\
**Post date:** [July 6, 2019, 10:48pm UTC](https://discourse.julialang.org/t/reshape-a-1-d-array-into-an-array-of-different-size-arrays/25999/8 "2019-07-06T22:48:01Z")

</div>

I see. No need for vectorization always.

Well, I guess I’ll use a for loop, as I need to do the same thing to a couple of arrays.

Thanks the advice.
