# Array of Arrays for months?

**URL:** <https://discourse.julialang.org/t/array-of-arrays-for-months/27022>\
**Category:** General Usage\
**Created:** [July 31, 2019, 8:52am UTC](https://discourse.julialang.org/t/array-of-arrays-for-months/27022 "2019-07-31T08:52:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MuadhF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muadhf/32/20389_2.png) [@MuadhF](https://discourse.julialang.org/u/MuadhF)\
**Post date:** [July 31, 2019, 8:52am UTC](https://discourse.julialang.org/t/array-of-arrays-for-months/27022/1 "2019-07-31T08:52:58Z")

</div>

Hi,

I want to create an array of arrays, where each sub-array contains the period t (month). For example,  
`t[1] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12]`  
`t[2] = [13, 14, 15, 16 ...]`

and so on. I managed to do this using the following code,

```julia
T = fill(Int[], 5)
t = 1

while t <= 60
    for i = 1:5
            T[i] = [t, t+1, t+2, t+3, t+4, t+5, t+6, t+7, t+8, t+9, t+10, t+11]
           global t = t + 12
    end
end

```

But of course, if the number of elements in the array is higher such as 20 or 30, it’s a very tedious process. Is there a better way to do this?

Thanks!

---

<div class="post-metadata">

**Author:** ![kdyrhage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdyrhage/32/2326_2.png) [@kdyrhage](https://discourse.julialang.org/u/kdyrhage)\
**Post date:** [July 31, 2019, 9:02am UTC](https://discourse.julialang.org/t/array-of-arrays-for-months/27022/2 "2019-07-31T09:02:45Z")

</div>

Is this what you’re after?

```julia
T = [collect(t:t+11) for t in 1:12:60]

```

---

<div class="post-metadata">

**Author:** ![MuadhF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muadhf/32/20389_2.png) [@MuadhF](https://discourse.julialang.org/u/MuadhF)\
**Post date:** [July 31, 2019, 9:13am UTC](https://discourse.julialang.org/t/array-of-arrays-for-months/27022/3 "2019-07-31T09:13:19Z")

</div>

Yess that’s exactly what I wanted! Thank you, forgive my ignorance

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [July 31, 2019, 10:45am UTC](https://discourse.julialang.org/t/array-of-arrays-for-months/27022/4 "2019-07-31T10:45:53Z")

</div>

Are you sure you actually need to collect these into an Array of Arrays? If you don’t need to mutate the elements you could use:

```julia
julia> T = reshape(1:12*5, 12, 5)
12×5 reshape(::UnitRange{Int64}, 12, 5) with eltype Int64:
  1 13 25 37 49
  2 14 26 38 50
  3 15 27 39 51
  4 16 28 40 52
  5 17 29 41 53
  6 18 30 42 54
  7 19 31 43 55
  8 20 32 44 56
  9 21 33 45 57
 10 22 34 46 58
 11 23 35 47 59
 12 24 36 48 60

```

and index into it just like a Matrix:

```julia
julia> T[2,3]
26

```

Then again, you could just calculate these values on the fly.

---

<div class="post-metadata">

**Author:** ![MuadhF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muadhf/32/20389_2.png) [@MuadhF](https://discourse.julialang.org/u/MuadhF)\
**Post date:** [August 1, 2019, 5:33am UTC](https://discourse.julialang.org/t/array-of-arrays-for-months/27022/5 "2019-08-01T05:33:06Z")

</div>

So I have an optimisation model where in some constraints I need to sum over the months for each year, for example:

```julia
@constraint(m, con[y = 1:Y], sum(x[t] for t in T[y])

```

So for each year I need a set with the corresponding month numbers. I’m guessing I need to collect the numbers for this case yeah?

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [August 1, 2019, 10:37am UTC](https://discourse.julialang.org/t/array-of-arrays-for-months/27022/6 "2019-08-01T10:37:47Z")

</div>

No, you don’t, since you don’t mutate anything in `T`. If I understand correctly, you want the sums of each 12th entries of `x`. A more efficient way to do this, would be to write this sum as:

```julia
sum(reshape(x, 12, :), dims=2)

```

Here `reshape` first shapes your vector `x` into a matrix with 12 rows. Then `sum(..., dims=2)` sums along the 2nd dimension, so you get the sums of each row. You might still have to call `vec` on the result, because this returns a 12x1-matrix, but some code might explicitly require a vector.

---

<div class="post-metadata">

**Author:** ![MuadhF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muadhf/32/20389_2.png) [@MuadhF](https://discourse.julialang.org/u/MuadhF)\
**Post date:** [August 1, 2019, 11:42am UTC](https://discourse.julialang.org/t/array-of-arrays-for-months/27022/7 "2019-08-01T11:42:37Z")

</div>

Oh yes that works as well, but I just had to change `dims = 2` to `dims = 1`, because the reshape function arranges the elements from top to bottom instead of left to right, so I had to make it get the sum of each column instead of row, if that makes any sense. Thanks for that tip.
