# Initialize an array (or other structure), add

**URL:** https://discourse.julialang.org/t/initialize-an-array-or-other-structure-add/88064
**Category:** General Usage
**Created:** [September 30, 2022, 9:42pm UTC](https://discourse.julialang.org/t/initialize-an-array-or-other-structure-add/88064 "2022-09-30T21:42:06Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ejlongman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ejlongman/32/36801_2.png) [@ejlongman](https://discourse.julialang.org/u/ejlongman)
#### Post date: [September 30, 2022, 9:42pm UTC](https://discourse.julialang.org/t/initialize-an-array-or-other-structure-add/88064/1 "2022-09-30T21:42:06Z")

</div>

I’m looking to create something like this:

```julia
array = [(0,0) (0,0) ;(0,0) (0,0) ; (0,0) (0,0)]

```

To store the results of a loop where the first iteration returns an array of pairs that I use to overwrite column 1

```julia
[(m,n); (m,n); (m,n)]

```

and the second iteration produces another array of pairs

```julia
[(m,n); (m,n); (m,n)]

```

that I use to overwrite col 2.

I’ve tried initializing using “zeros()”, but that does not work, and it seems like tuples are not initalize-able in Julia?

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [September 30, 2022, 9:46pm UTC](https://discourse.julialang.org/t/initialize-an-array-or-other-structure-add/88064/2 "2022-09-30T21:46:45Z")

</div>

You can initialize it as:

```julia
julia> arr = Array{Tuple{Int, Int}}(undef, 3, 2)
3×2 Matrix{Tuple{Int64, Int64}}:
 (0, 0) (0, 0)
 (0, 0) (0, 0)
 (0, 0) (0, 0)

```

The values aren’t guaranteed to be `(0, 0)` when using the `undef` initializer, but that should be fine here since you’re going to be replacing them anyway. If you do need them to be `(0, 0)`, you can use `fill` instead.

```julia
julia> arr = fill((0, 0), 3, 2)
3×2 Matrix{Tuple{Int64, Int64}}:
 (0, 0) (0, 0)
 (0, 0) (0, 0)
 (0, 0) (0, 0)

```

---

<div class="post-metadata">

### Author: ![ejlongman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ejlongman/32/36801_2.png) [@ejlongman](https://discourse.julialang.org/u/ejlongman)
#### Post date: [September 30, 2022, 10:38pm UTC](https://discourse.julialang.org/t/initialize-an-array-or-other-structure-add/88064/3 "2022-09-30T22:38:44Z")

</div>

Thank you!! And once I have a tuple, say, this: how would I index to the upper right hand corner (7,1)?

```julia
10×2 Matrix{Tuple{Int64, Int64}}:
 (5, 1) (7, 1)
 (5, 2) (7, 2)
 (5, 3) (7, 3)
 (5, 4) (7, 4)
 (5, 5) (7, 5)
 (5, 6) (7, 6)
 (5, 7) (7, 7)
 (5, 8) (7, 8)
 (5, 9) (7, 9)
 (5, 10) (7, 10)

```

---

<div class="post-metadata">

### Author: ![ejlongman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ejlongman/32/36801_2.png) [@ejlongman](https://discourse.julialang.org/u/ejlongman)
#### Post date: [September 30, 2022, 10:40pm UTC](https://discourse.julialang.org/t/initialize-an-array-or-other-structure-add/88064/4 "2022-09-30T22:40:24Z")

</div>

Oh wait it’s like this: Lok[7,2][2]

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [October 1, 2022, 12:05pm UTC](https://discourse.julialang.org/t/initialize-an-array-or-other-structure-add/88064/5 "2022-10-01T12:05:03Z")

</div>

If you want to get to the `(7, 1)` element of the matrix, you can do it as:

```julia
Lok[1, 2]

```

(`Lok` is the name of your matrix here.)

This is because `(7, 1)` is in row `1` and column `2` of the matrix.

```julia
                   Column 2
                     |
                     \/
Row 1 --> (5, 1) (7, 1)
          (5, 2) (7, 2)
          (5, 3) (7, 3)
          (5, 4) (7, 4)
          (5, 5) (7, 5)
          (5, 6) (7, 6)
          (5, 7) (7, 7)
          (5, 8) (7, 8)
          (5, 9) (7, 9)
          (5, 10) (7, 10)

```

`Lok[7, 2]` on the other hand gets you the element in the 7th row, 2nd column, which is `(7, 7)`. So doing `Lok[7, 2][2]` gets you the second element _within_ `(7, 7)`, which would be a `7`.

By the way, if these values from the loop are distinct results that don’t need to be together, it might be better to have them as just two separate matrices, rather than a matrix containing tuples like this. A lot depends on your specific scenario though, but it’s something to consider in case accessing the values the way you want turns out inconvenient with this setup.
