# How do I append row vectors into an array when I don't know the exact dimension of the array before calculations end?

**URL:** <https://discourse.julialang.org/t/how-do-i-append-row-vectors-into-an-array-when-i-dont-know-the-exact-dimension-of-the-array-before-calculations-end/103762>\
**Category:** General Usage\
**Tags:** loops, while-for-scope\
**Created:** [September 11, 2023, 9:06pm UTC](https://discourse.julialang.org/t/how-do-i-append-row-vectors-into-an-array-when-i-dont-know-the-exact-dimension-of-the-array-before-calculations-end/103762 "2023-09-11T21:06:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discourse.julialang.org/u/Hugo)\
**Post date:** [September 11, 2023, 9:06pm UTC](https://discourse.julialang.org/t/how-do-i-append-row-vectors-into-an-array-when-i-dont-know-the-exact-dimension-of-the-array-before-calculations-end/103762/1 "2023-09-11T21:06:57Z")

</div>

I’m using a `while` statement to carry out calculations for as long as a condition holds. At every iteration, I calculate a vector `v`. I want to append every new `v` vector as a row vector to the previous `v` vector.

The problem is that my condition doesn’t have a fixed number of iterations. With every new data point, the condition can change and the size of `myArray` will change. I have tried creating an array of type `Any`; one item in `v` has a different type `String` while all other items have type `Float64`.

I don’t know how many iterations will take for the `while` loop to stop, therefore I don’t know the number of rows of my array. Because of size difference, I get an error when I try to append `v` to `myArray`.

I have tried different variations of `vcat` and `hcat`, either transposing `v` or not — none worked. The following piece of code explains what I’m trying to do with simpler objects.

```julia
myArray = [];
myNumber = rand(1)*100;

while myNumber > 0 
   # Compute vector v
   v = ["a", 1, 2, 3]
   # ..............................................
   # Assign v to myArray as a row vector 
   # in every iteration. How do I do this?
   # ..............................................
   myNumber = myNumber - 1
end

```

All help is appreciated.

P.S.: At one point, I created `A = Array{Any}(undef, n,4)`, with `n` large enough that it wouldn’t be a problem. At every iteration, I incremented a counter `i` and assigned the freshly computed `v` to row `i`, `A[i,:] = v`. The problem with that, besides the many unused rows, is that I tried to subset from `A` the part that only contained data I wanted. I relied on the type in the columns as a parameter to filter for, but I couldn’t make it work either.

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [September 11, 2023, 9:16pm UTC](https://discourse.julialang.org/t/how-do-i-append-row-vectors-into-an-array-when-i-dont-know-the-exact-dimension-of-the-array-before-calculations-end/103762/2 "2023-09-11T21:16:48Z")

</div>

You could use a vector of tuples? I am assuming that you know the size of the records you would be appending?

```julia
In [1]: a = Vector{NTuple{3, Float64}}()
Out[1]: Tuple{Float64, Float64, Float64}[]

In [2]: for i ∈ 1:4
             push!(a, (rand(), rand(), rand()))
         end

In [3]: a
Out[3]: 4-element Vector{Tuple{Float64, Float64, Float64}}:
 (0.5245041461916572, 0.556349994815155, 0.9146538258170984)
 (0.07877443211157664, 0.5609529419162536, 0.7461623172366005)
 (0.5086419102034306, 0.1830715697795372, 0.6984704005092253)
 (0.060321039676802024, 0.8323461219441477, 0.1747723930534809)

```

---

<div class="post-metadata">

**Author:** ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discourse.julialang.org/u/Hugo)\
**Post date:** [September 11, 2023, 9:22pm UTC](https://discourse.julialang.org/t/how-do-i-append-row-vectors-into-an-array-when-i-dont-know-the-exact-dimension-of-the-array-before-calculations-end/103762/3 "2023-09-11T21:22:12Z")

</div>

Hi, frylock

Thanks for your answer. I imagine I could insert different types inside of the `Tuple`, so that could be a way out. But from the output, it seems that in the end you don’t have an array you could subset from, so using the `push!` command wouldn’t work in the way your presented. I’ll look into it, though. Maybe I can adapt it to what I need.

Thank you.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [September 11, 2023, 9:38pm UTC](https://discourse.julialang.org/t/how-do-i-append-row-vectors-into-an-array-when-i-dont-know-the-exact-dimension-of-the-array-before-calculations-end/103762/4 "2023-09-11T21:38:45Z")

</div>

```julia
julia> myArray = [];

julia> myNumber = trunc(Int,(rand(1)*10)[1])
3

julia> while myNumber > 0
          # Compute vector v
          v = ["a", 1, 2, 3]
          # ..............................................
          # Assign v to myArray as a row vector 
          # in every iteration. How do I do this?
          # ..............................................
          push!(myArray,v)
          myNumber = myNumber - 1
       end

julia> myArray
3-element Vector{Any}:
 Any["a", 1, 2, 3]
 Any["a", 1, 2, 3]
 Any["a", 1, 2, 3]

```

But maybe you want to get this?  
the expected result is not very clear.

```julia
julia> stack(myArray,dims=1)
5×4 Matrix{Any}:
 "a" 1 2 3
 "a" 1 2 3
 "a" 1 2 3
 "a" 1 2 3
 "a" 1 2 3

```

or

```julia
julia> permutedims(reduce(hcat,myArray))
5×4 Matrix{Any}:
 "a" 1 2 3
 "a" 1 2 3
 "a" 1 2 3
 "a" 1 2 3
 "a" 1 2 3

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 11, 2023, 11:27pm UTC](https://discourse.julialang.org/t/how-do-i-append-row-vectors-into-an-array-when-i-dont-know-the-exact-dimension-of-the-array-before-calculations-end/103762/5 "2023-09-11T23:27:24Z")

</div>

You may be looking for concatenation. Look into the `cat` functions, and its various flavors, like `hcat` or `vcat`:  
[Arrays · The Julia Language](https://docs.julialang.org/en/v1/base/arrays/#Base.cat)

A few more things:

- Consider appending your data as _columns_, not rows. This should be more efficient.
- `myArray = []` creates a _vector_, not a matrix.
- Arrays with mixed types are not terribly efficient. Using a vector of tuples, as suggested above, may be better. Or perhaps store the strings and the numbers separately.
