# How to use push!() in a two-dimensional matrix

**URL:** <https://discourse.julialang.org/t/how-to-use-push-in-a-two-dimensional-matrix/66790>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 21, 2021, 8:47pm UTC](https://discourse.julialang.org/t/how-to-use-push-in-a-two-dimensional-matrix/66790 "2021-08-21T20:47:47Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [August 21, 2021, 11:45pm UTC](https://discourse.julialang.org/t/how-to-use-push-in-a-two-dimensional-matrix/66790/21 "2021-08-21T23:45:53Z")

</div>

Can someone explain what a 2 rows 0 column 2D array is?  
I have trouble visualizing a Matrix with 2 rows and 0 columns

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 21, 2021, 11:53pm UTC](https://discourse.julialang.org/t/how-to-use-push-in-a-two-dimensional-matrix/66790/22 "2021-08-21T23:53:23Z")

</div>

it’s just an empty Matrix

---

<div class="post-metadata">

**Author:** ![soldin](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@soldin](https://discourse.julialang.org/u/soldin)\
**Post date:** [August 21, 2021, 11:58pm UTC](https://discourse.julialang.org/t/how-to-use-push-in-a-two-dimensional-matrix/66790/23 "2021-08-21T23:58:45Z")

</div>

It was initialized with no column (instead of a column of zeros) because push!() (which I (wrongly) thought could work) does not replace the existing values, and thus the first column would be having zeros.

---

<div class="post-metadata">

**Author:** ![soldin](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@soldin](https://discourse.julialang.org/u/soldin)\
**Post date:** [August 22, 2021, 3:00am UTC](https://discourse.julialang.org/t/how-to-use-push-in-a-two-dimensional-matrix/66790/24 "2021-08-22T03:00:51Z")

</div>

```julia
array1 = [1; 1; 1]
array2 = [2 3; 2 3; 2 3]

array3 = hcat((array1,array2))

```

However, what I get is a tuple, which is immutable. Is there any way to get a matrix, because the process happens in a loop.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 22, 2021, 3:24am UTC](https://discourse.julialang.org/t/how-to-use-push-in-a-two-dimensional-matrix/66790/25 "2021-08-22T03:24:31Z")

</div>

```julia
julia> array3 = hcat(array1,array2)
3×3 Matrix{Int64}:
 1 2 3
 1 2 3
 1 2 3

```

---

<div class="post-metadata">

**Author:** ![soldin](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@soldin](https://discourse.julialang.org/u/soldin)\
**Post date:** [August 22, 2021, 3:37am UTC](https://discourse.julialang.org/t/how-to-use-push-in-a-two-dimensional-matrix/66790/26 "2021-08-22T03:37:34Z")

</div>

Oh, I had enclosed the arrays in double parenthesis. Thanks a lot jling. I have a long way to go in learning this language. It’s not gonna be a cakewalk like MATLAB, it seems.

---

<div class="post-metadata">

**Author:** ![soldin](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@soldin](https://discourse.julialang.org/u/soldin)\
**Post date:** [August 22, 2021, 5:28am UTC](https://discourse.julialang.org/t/how-to-use-push-in-a-two-dimensional-matrix/66790/27 "2021-08-22T05:28:14Z")

</div>

Finally, I’ve been able to get the thing done!

```julia
y=zeros(Float16,2,1)
# or y=Array{Float16}(undef,2,1)

for i1 in 1:1:10

     global y #without this the code threw a scoping error

     y = hcat(y,[f(i1); g(i1)]) #some functions of i1    

end

```

Thanks all.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 22, 2021, 6:31am UTC](https://discourse.julialang.org/t/how-to-use-push-in-a-two-dimensional-matrix/66790/28 "2021-08-22T06:31:23Z")

</div>

Usually it’s best to skip the “growing” step and create the matrix with the correct size right away and only modify its contents - e.g. in this case, with

```julia
f(x) = 2x
g(x) = 3x

function build_mat()
    y = zeros(Float16, 2, 11)
    for i1 in 1:10
        y[:,i1+1] .= f(i1), g(i1)
    end
    return y
end

```

This will be much faster than repeatedly growing/reallocating your matrix:

```julia
julia> function build_mat_hcat()                              
           y = zeros(Float16, 2, 1)                           
           for i1 in 1:1:10                                   
               y = hcat(y, [f(i1), g(i1)])                    
           end                                                
           return y                                           
       end                                                    
build_mat_hcat (generic function with 1 method)               
                                                              
julia> using BenchmarkTools                                   
                                                              
julia> @btime build_mat()                                    
  65.542 ns (1 allocation: 96 bytes)                         
2×11 Matrix{Float16}:                                        
 0.0 2.0 4.0 6.0 8.0 10.0 12.0 14.0 16.0 18.0 20.0
 0.0 3.0 6.0 9.0 12.0 15.0 18.0 21.0 24.0 27.0 30.0
                                                              
julia> @btime build_mat_hcat()                                
  744.828 ns (21 allocations: 1.62 KiB)                       
2×11 Matrix{Float16}:                                         
 0.0 2.0 4.0 6.0 8.0 10.0 12.0 14.0 16.0 18.0 20.0 
 0.0 3.0 6.0 9.0 12.0 15.0 18.0 21.0 24.0 27.0 30.0 

```

This is on the order of nanoseconds (“just” a factor 10x) here because the loop in the function is super tiny, but with longer loops it will be much more (I’m passing in the requested size as a parameter to make it easier to run):

```julia
julia> function build_mat_hcat(n)                
           y = zeros(Float16, 2, 1)              
           for i1 in 1:1:n                       
               y = hcat(y, [f(i1), g(i1)])       
           end                                   
           return y                              
       end                                       
build_mat_hcat (generic function with 2 methods) 
                                                 
julia> function build_mat(n)                     
           y = zeros(Float16, 2, n+1)            
           for i1 in 1:n                         
               y[:,i1+1] .= f(i1), g(i1)         
           end                                   
           return y                              
       end                                       
build_mat (generic function with 2 methods)      
                                                 
julia> @btime build_mat_hcat(100);               
  8.367 μs (201 allocations: 33.67 KiB)          
                                                 
julia> @btime build_mat(100);                    
  357.075 ns (1 allocation: 496 bytes)           
                                                 
julia> build_mat(100) ≈ build_mat_hcat(100)      
true                                             

```

As you can see, the repeatedly allocating function is ~23x slower, for the same result. This will become worse with even longer loops, because allocating is a comparatively expensive operation:

```julia
julia> @btime build_mat_hcat(1000);        
  340.100 μs (2001 allocations: 2.11 MiB)  
                                           
julia> @btime build_mat_hcat(10_000);      
  21.113 ms (25906 allocations: 192.58 MiB)
                                           
julia> @btime build_mat(10_000);           
  32.300 μs (2 allocations: 39.17 KiB)     

```

That’s ~653 times slower! That ratio is going to continue to get worse.

Reducing or even eliminating allocations is a key idiom for achieving extremely fast julia code - it’s best to try to be conscious of this from the beginning, instead of trying to translate other languages one-to-one. What’s good in language A may not be good in language B, after all.

* * *

Additionally, putting your code into functions and passing arguments in explicitly will make sure you don’t have problems with scoping of variables (global variables are kind of an anti-pattern in julia - they can lead to huge losses in performance).

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 22, 2021, 10:09am UTC](https://discourse.julialang.org/t/how-to-use-push-in-a-two-dimensional-matrix/66790/29 "2021-08-22T10:09:52Z")

</div>

> [@Sukera](#):
>
> What’s good in language A may not be good in language B, after all

Also, that is not good in Matlab either, and will be very very slow. It is just that Matlab allows a simple syntax for the bad code.

---

<div class="post-metadata">

**Author:** ![soldin](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@soldin](https://discourse.julialang.org/u/soldin)\
**Post date:** [August 22, 2021, 8:55pm UTC](https://discourse.julialang.org/t/how-to-use-push-in-a-two-dimensional-matrix/66790/30 "2021-08-22T20:55:42Z")

</div>

> [@Sukera](#):
>
> Usually it’s best to skip the “growing” step and create the matrix with the correct size right away and only modify its contents - e.g. in this case, with

Yes, now I’m gonna allocate a large memory and, after the completion of the filling of the array, will remove the trailing part. Thanks a lot for the exposition.

---

<div class="post-metadata">

**Author:** ![soldin](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@soldin](https://discourse.julialang.org/u/soldin)\
**Post date:** [August 22, 2021, 9:11pm UTC](https://discourse.julialang.org/t/how-to-use-push-in-a-two-dimensional-matrix/66790/31 "2021-08-22T21:11:00Z")

</div>

> [@Sukera](#):
>
> Additionally, putting your code into functions and passing arguments in explicitly will make sure you don’t have problems with scoping of variables (global variables are kind of an anti-pattern in julia - they can lead to huge losses in performance).

Yes, will do that. But in my VSC not declaring y as a global variable inside the for loop throws an error.

[Previous page](https://discourse.julialang.org/t/how-to-use-push-in-a-two-dimensional-matrix/66790.md?page=1)
