# An error from stacking a new array with an initialization array (empty)

**URL:** <https://discourse.julialang.org/t/an-error-from-stacking-a-new-array-with-an-initialization-array-empty/69955>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 18, 2021, 2:14am UTC](https://discourse.julialang.org/t/an-error-from-stacking-a-new-array-with-an-initialization-array-empty/69955 "2021-10-18T02:14:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 18, 2021, 2:14am UTC](https://discourse.julialang.org/t/an-error-from-stacking-a-new-array-with-an-initialization-array-empty/69955/1 "2021-10-18T02:14:36Z")

</div>

Here is my code:

```julia
A = [];
for ...
A = vcat(A, b);
end

```

I’m getting the below error. Before A is assigned any values, it is an empty array, and the program is complaining that:  
`number of columns of each array must match`

```julia
size(A) = (0,)
size(b) = (26, 29)

```

Clearly, this is a bad habit I brought here from Matlab, so how do Julian folks get around this issue? Many thanks!

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [October 18, 2021, 2:15am UTC](https://discourse.julialang.org/t/an-error-from-stacking-a-new-array-with-an-initialization-array-empty/69955/2 "2021-10-18T02:15:52Z")

</div>

Maybe you want to `push!` into `A`? Note that growing arrays like that is not very performant.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 18, 2021, 2:26am UTC](https://discourse.julialang.org/t/an-error-from-stacking-a-new-array-with-an-initialization-array-empty/69955/3 "2021-10-18T02:26:49Z")

</div>

Many thanks for the reply!

So I should do push!(A, b)? Unfortunately, it does not work either. How do I specify the fact that I want the item being pushed to be appended vertically?

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [October 18, 2021, 2:27am UTC](https://discourse.julialang.org/t/an-error-from-stacking-a-new-array-with-an-initialization-array-empty/69955/4 "2021-10-18T02:27:53Z")

</div>

can you provide a simple example of the code?

---

<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:** [October 18, 2021, 2:47am UTC](https://discourse.julialang.org/t/an-error-from-stacking-a-new-array-with-an-initialization-array-empty/69955/5 "2021-10-18T02:47:48Z")

</div>

The problem is that you can’t `vcat` a matrix and a vector. And even if your `A` were a matrix, you still can’t `vcat` a `26x29` matrix to a `0x0` matrix (for a vertical concatenation, the number of rows must match).

You could create a `0x29` matrix and then use `vcat`, but it should be easier to avoid this entirely using `reduce`:

```julia
julia> my_matrices = [[1 2; 3 4], [5 6; 7 8], [9 10; 11 12]]
3-element Vector{Matrix{Int64}}:
 [1 2; 3 4]
 [5 6; 7 8]
 [9 10; 11 12]

julia> reduce(vcat, my_matrices)
6×2 Matrix{Int64}:
  1 2
  3 4
  5 6
  7 8
  9 10
 11 12

```

`reduce` combines elements of a collection pairwise, and `vcat` does vertical concatenation, so `reduce(vcat, ...)` combines all the matrices in the collection by vertical concatenation. Julia actually has a special implementation (still in Julia, just written carefully) that makes that particular combination of operations more efficient than a naive approach. If you want to see how it works, you can do: `@edit reduce(vcat, my_matrices)`

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 18, 2021, 9:55am UTC](https://discourse.julialang.org/t/an-error-from-stacking-a-new-array-with-an-initialization-array-empty/69955/6 "2021-10-18T09:55:49Z")

</div>

Actually, it did not work either.

Here is my code:

```julia
using MAT
# Read the bathymetric values out of the mat file:
F1 = matopen("/test.mat");
A = read(F1, "A");
b = read(F1, "b");
close(F1);
A = reduce(vcat, [A, b])

```

Here is a link to download the “test.mat” file storing my data:

> **[iCloud.com](https://www.icloud.com/iclouddrive/0TfzuHQFqs5YXPhw7zt4m-ESQ#test)**
>
> Sign in to iCloud to access your photos, videos, documents, notes, contacts, and more. Use your Apple ID or create a new account to start using Apple services.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 18, 2021, 10:20am UTC](https://discourse.julialang.org/t/an-error-from-stacking-a-new-array-with-an-initialization-array-empty/69955/7 "2021-10-18T10:20:17Z")

</div>

Looks like it will work if I define my A as the below, instead of using A=;

`A = Array{Float64}(undef, 0, 29);`
