# Best practices - initialize a matrix

**URL:** <https://discourse.julialang.org/t/best-practices-initialize-a-matrix/16863>\
**Category:** New to Julia\
**Created:** [October 27, 2018, 3:15pm UTC](https://discourse.julialang.org/t/best-practices-initialize-a-matrix/16863 "2018-10-27T15:15:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiago](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tiago/32/22567_2.png) [@tiago](https://discourse.julialang.org/u/tiago)\
**Post date:** [October 27, 2018, 3:15pm UTC](https://discourse.julialang.org/t/best-practices-initialize-a-matrix/16863/1 "2018-10-27T15:15:56Z")

</div>

Hello,

I’m doing my first steps in julia and using the v1.0.1, and I wondered what is the best practice to initialize a large matrix.

I’m just initializing in the following way: `b = Array{Float64}(undef,dim)`. But i know there are anothers ways to do that, such as: `b = Array{Union{Nothing, String}}(nothing, dim)`, or use the function `spzeros()` that creates a sparse matrix with zeros.

What is the best practice? Most of time i just want to initialize the matrix structure, not all the matrix entries.

Thank you,  
Tiago

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 27, 2018, 3:33pm UTC](https://discourse.julialang.org/t/best-practices-initialize-a-matrix/16863/2 "2018-10-27T15:33:15Z")

</div>

> [@tiago](#):
>
> What is the best practice? Most of time i just want to initialize the matrix structure, not all the matrix entries.

This really depends on what you are going to do with the matrix down the line, so maybe you can mention something about that?

---

<div class="post-metadata">

**Author:** ![tiago](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tiago/32/22567_2.png) [@tiago](https://discourse.julialang.org/u/tiago)\
**Post date:** [October 27, 2018, 8:34pm UTC](https://discourse.julialang.org/t/best-practices-initialize-a-matrix/16863/3 "2018-10-27T20:34:17Z")

</div>

Yes, you’re right, What I am trying to do is the following:

```julia
x = h:h:L-h
F = 2*sin.(X) - sin.(2*X)

B = Array{Float64}(undef,n,n)  
#or B = Array{Union{Nothing, String}}(nothing, n, n) or B = spzeros(n, n) 

B[n, 1:n] = F

```

I construct a vector f, and I just initialize the last row of B with f. Then I just fill the whole matrix B with a loop.

What is the best way to do such thing? My background it’s Matlab and perhaps I am not thinking in the julia way.

Thank you for the patience,  
Tiago

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 27, 2018, 8:44pm UTC](https://discourse.julialang.org/t/best-practices-initialize-a-matrix/16863/4 "2018-10-27T20:44:08Z")

</div>

It sounds like the goal is to have a matrix of only `Float64` that have all entries stored? Then I would definitely not use a sparse matrix, and I would not use `Union{Float64, Nothing}` either. Your plan with using `Matrix{Float64}(undef, n, n)` and then setting the elements in a loop sounds perfectly reasonable.

Note that

> [@tiago](#):
>
> B[n, 1:n] = F

can be written as

```julia
B[end, :] = F

```

---

<div class="post-metadata">

**Author:** ![tiago](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tiago/32/22567_2.png) [@tiago](https://discourse.julialang.org/u/tiago)\
**Post date:** [October 28, 2018, 9:46am UTC](https://discourse.julialang.org/t/best-practices-initialize-a-matrix/16863/5 "2018-10-28T09:46:35Z")

</div>

Oh yes, thank you. I forgot to use the `end` command and the colon `:`.

So, if I want to use the `spzeros()` function it’s only in the case that you have a lot of zeros in the matrix. Ok!

Appreciate the help, thank you 🙂
