# How can I get the chunks of a given array

**URL:** <https://discourse.julialang.org/t/how-can-i-get-the-chunks-of-a-given-array/29737>\
**Category:** New to Julia\
**Created:** [October 10, 2019, 4:43pm UTC](https://discourse.julialang.org/t/how-can-i-get-the-chunks-of-a-given-array/29737 "2019-10-10T16:43:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [October 10, 2019, 4:43pm UTC](https://discourse.julialang.org/t/how-can-i-get-the-chunks-of-a-given-array/29737/1 "2019-10-10T16:43:21Z")

</div>

I’ve a series of 60 elements:

```julia
series = [30,21,29,31,40,48,53,47,37,39,31,29,17,9,20,24,27,35,41,38,
          27,31,27,26,21,13,21,18,33,35,40,36,22,24,21,20,17,14,17,19,
          26,29,40,31,20,24,18,26,17,9,17,21,28,32,46,33,23,28,22,27,
          18,8,17,21,31,34,44,38,31,30,26,32];

```

I want to convert it to an array of 6 elements, where each element is an array of 12 elements

I tried reshape:

```julia
zx = reshape(series, 12, :)

```

That generates another array of elements as:

```bash
12×6 Array{Int64,2}:
 30 17 21 17 17 18
 21 9 13 14 9 8
 29 20 21 17 17 17
 31 24 18 19 21 21
 40 27 33 26 28 31
 48 35 35 29 32 34
 53 41 40 40 46 44
 47 38 36 31 33 38
 37 27 22 20 23 31
 39 31 24 24 28 30
 31 27 21 18 22 26
 29 26 20 26 27 32

```

But this does not looks to be what I want, I need to have it as:

```bash
12-element Array{Array{Int64,1},1}:
 [1 2 … 11 12]
 [1 2 … 11 12]
 [1 2 … 11 12]
 [1 2 … 11 12]
 [1 2 … 11 12]
 [1 2 … 11 12]
 [1 2 … 11 12]
 [1 2 … 11 12]
 [1 2 … 11 12]
 [1 2 … 11 12]
 [1 2 … 11 12]
 [1 2 … 11 12]

```

In some functional programming languages, this is called `chunk`, so I need to have my series converted to chunks of 12 elements each

---

<div class="post-metadata">

**Author:** ![Rajesh\_Talluri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rajesh_talluri/32/10721_2.png) [@Rajesh\_Talluri](https://discourse.julialang.org/u/Rajesh_Talluri)\
**Post date:** [October 10, 2019, 5:05pm UTC](https://discourse.julialang.org/t/how-can-i-get-the-chunks-of-a-given-array/29737/2 "2019-10-10T17:05:40Z")

</div>

One way you could do that is:

```julia
[series[i:i+11] for i in 1:12:length(series)]
6-element Array{Array{Int64,1},1}:
 [30, 21, 29, 31, 40, 48, 53, 47, 37, 39, 31, 29]
 [17, 9, 20, 24, 27, 35, 41, 38, 27, 31, 27, 26]
 [21, 13, 21, 18, 33, 35, 40, 36, 22, 24, 21, 20]
 [17, 14, 17, 19, 26, 29, 40, 31, 20, 24, 18, 26]
 [17, 9, 17, 21, 28, 32, 46, 33, 23, 28, 22, 27]
 [18, 8, 17, 21, 31, 34, 44, 38, 31, 30, 26, 32]

```

---

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [October 10, 2019, 5:22pm UTC](https://discourse.julialang.org/t/how-can-i-get-the-chunks-of-a-given-array/29737/3 "2019-10-10T17:22:20Z")

</div>

Thanks, now I can convert this `Rust` code:

```rust
fn initial_seasonal_components (series: &[i32], slen: usize) -> Vec<f32> {
    let n_seasons = series.len() / slen;
    // # compute season averages
    let season_averages = series //season_averages
        .chunks(slen)
        .map(|chunk| chunk.iter().sum::<i32>() as f32 / chunk.len() as f32)
        .collect::<Vec<f32>>();
    // # compute initial values
    (0..slen).map(|i| {
        let mut sum_of_vals_over_avg = 0.0;
        for j in 0..n_seasons {
            sum_of_vals_over_avg += series[i + j * slen] as f32 - season_averages[j] as f32;
        }
        sum_of_vals_over_avg / n_seasons as f32
    }).collect()
}

```

To `Julia` as:

```julia
season_averages = map(i -> sum(i) / length(i) ,[series[i:i+11] for i in 1:12:length(series)])

map(i -> begin
            sum_of_vals_over_avg = 0.0
            map(j -> # for j in (0:6-1)
                sum_of_vals_over_avg += series[i + j * 12] - season_averages[j+1]
            , (0:6-1)) #end
            sum_of_vals_over_avg / 6
    end
    , (1:12))

```

---

<div class="post-metadata">

**Author:** ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)\
**Post date:** [October 10, 2019, 5:40pm UTC](https://discourse.julialang.org/t/how-can-i-get-the-chunks-of-a-given-array/29737/4 "2019-10-10T17:40:16Z")

</div>

You can also use `Iterators.partition`, which is also lazy.

```julia
Iterators.partition(series,12) |> collect
6-element Array{Array{Int64,1},1}:
 [30, 21, 29, 31, 40, 48, 53, 47, 37, 39, 31, 29]
 [17, 9, 20, 24, 27, 35, 41, 38, 27, 31, 27, 26]
 [21, 13, 21, 18, 33, 35, 40, 36, 22, 24, 21, 20]
 [17, 14, 17, 19, 26, 29, 40, 31, 20, 24, 18, 26]
 [17, 9, 17, 21, 28, 32, 46, 33, 23, 28, 22, 27]
 [18, 8, 17, 21, 31, 34, 44, 38, 31, 30, 26, 32]

```

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [October 10, 2019, 5:40pm UTC](https://discourse.julialang.org/t/how-can-i-get-the-chunks-of-a-given-array/29737/5 "2019-10-10T17:40:31Z")

</div>

or

```julia
using Statistics
zx = 1.0*reshape(series, 12, :)
 zx .-= mean(zx, dims=1)
mean(zx, dims=2)

```

---

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [October 10, 2019, 8:52pm UTC](https://discourse.julialang.org/t/how-can-i-get-the-chunks-of-a-given-array/29737/6 "2019-10-10T20:52:07Z")

</div>

Thanks a lot, but I prefer have it pure depending in the language support, and not using Packages unless there is no way to do it using simple commands from the language design and structure itself,

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [October 10, 2019, 8:56pm UTC](https://discourse.julialang.org/t/how-can-i-get-the-chunks-of-a-given-array/29737/7 "2019-10-10T20:56:09Z")

</div>

[Statistics](https://docs.julialang.org/en/v1/stdlib/Statistics/) is a module in base julia, it’s not a separate package

---

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [October 10, 2019, 9:07pm UTC](https://discourse.julialang.org/t/how-can-i-get-the-chunks-of-a-given-array/29737/8 "2019-10-10T21:07:35Z")

</div>

aha, thanks a lot for the notification, I was not aware
