# Best practices for generating vectors with ones, zeros, rand, etc

**URL:** https://discourse.julialang.org/t/best-practices-for-generating-vectors-with-ones-zeros-rand-etc/40197
**Category:** General Usage
**Tags:** linearalgebra
**Created:** [May 26, 2020, 2:34pm UTC](https://discourse.julialang.org/t/best-practices-for-generating-vectors-with-ones-zeros-rand-etc/40197 "2020-05-26T14:34:53Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![ark](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@ark](https://discourse.julialang.org/u/ark)
#### Post date: [May 26, 2020, 2:34pm UTC](https://discourse.julialang.org/t/best-practices-for-generating-vectors-with-ones-zeros-rand-etc/40197/1 "2020-05-26T14:34:53Z")

</div>

A common method to generate and initialize vectors using ones, zeros, or random values, is, e.g., `ones(N,1)`.

However, in Julia, such initializations result in a matrix type (!) rather than a vector:  
julia\> VERSION  
v"1.4.1"  
julia\> ones(2,1)  
**2×1 Array{Float64,2}:**  
1.0  
1.0  
julia\> ones(1,2)  
**1×2 Array{Float64,2}:**  
1.0 1.0

This is also true for other functions such as zeros and randn. Usually this must not cause any problems. However, functions such as `Diagonal` change their behaviour based on the input type. This causes newbie (perhaps also for experienced users) problems. Furthermore, each call to ones, etc. has to be wrapped in a vec: `Diagonal(vec(ones(2,1)))` which is against Julia’s aim of better code clarity.

What are the best practices for generating vectors of zeros, ones, etc., so that the result is of vector type?

Definitely, I would prefer the functions to return vectors when the requested dimension unambiguously says so.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [May 26, 2020, 2:37pm UTC](https://discourse.julialang.org/t/best-practices-for-generating-vectors-with-ones-zeros-rand-etc/40197/2 "2020-05-26T14:37:28Z")

</div>

`ones(2)`, `rand(2)` etc all do what you want and return a vector. The way to think about it is that the number of arguments you pass to them is the number of dimensions you want the output to have.

---

<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: [May 26, 2020, 2:54pm UTC](https://discourse.julialang.org/t/best-practices-for-generating-vectors-with-ones-zeros-rand-etc/40197/3 "2020-05-26T14:54:08Z")

</div>

> [@ark](#):
>
> Definitely, I would prefer the functions to return vectors when the requested dimension unambiguously says so.

But that’s actually what it does. If you want to make a 2x1 matrix, how would you otherwise write it?

I’m guessing you learnt this from Matlab (I’m a Matlab user too), but it’s the same there; `ones(2,1)` returns a 2x1 matrix, just like Julia and numpy. The difference is just that in Matlab you _cannot_ make a proper vector, only matrices.

---

<div class="post-metadata">

### Author: ![ark](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@ark](https://discourse.julialang.org/u/ark)
#### Post date: [May 26, 2020, 2:59pm UTC](https://discourse.julialang.org/t/best-practices-for-generating-vectors-with-ones-zeros-rand-etc/40197/4 "2020-05-26T14:59:18Z")

</div>

Thank you. I think this is a consistent interface.

Nevertheless, what does a 3x1 array with dimension `Array{Float62,2}` mean?

When applied to the function `Diagonal` the dimension `Array{Float62,2}` or `Array{Float62,1}` seems to indicate the behaviour the function needs to adopt.

Personally to me, 3x1 arrays of type `Array{Float62,2}` and `Array{Float62,1}` are the same (the distinction is due to programming rather than mathematical expressivity) and I would be uncomfortable using the 2 vs 1 info to change the behaviour of the functions. This is because the inputs to Diagonal can come from anywhere (i.e. out of our control) and need to by typecast (with a vec, etc) each time.

Is it better to take this question up on LinearAlgebra GitHub issues list rather than discuss here? Perhaps this is a bit of a deviation from the original title of the question, but I think still relevant in scope.

---

<div class="post-metadata">

### Author: ![ark](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@ark](https://discourse.julialang.org/u/ark)
#### Post date: [May 26, 2020, 3:00pm UTC](https://discourse.julialang.org/t/best-practices-for-generating-vectors-with-ones-zeros-rand-etc/40197/5 "2020-05-26T15:00:19Z")

</div>

Yes, indeed. The confusion is because I’m used to MATLAB where `ones(2)` is a 2x2 matrix. Please also see my comments to the earlier reply.

---

<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: [May 26, 2020, 3:19pm UTC](https://discourse.julialang.org/t/best-practices-for-generating-vectors-with-ones-zeros-rand-etc/40197/6 "2020-05-26T15:19:44Z")

</div>

> [@ark](#):
>
> Is it better to take this question up on LinearAlgebra GitHub issues list rather than discuss here?

It’s better to discuss it here. It will _definitely_ not be changed, since it has been _extremely_ thoroughly discussed, and is without doubt the right design.

I’m going to say “trust me on this”. The confusion between vectors and matrices is one of the _worst_ things about Matlab. The behavior of Julia and numpy is just vastly better.

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [May 26, 2020, 3:30pm UTC](https://discourse.julialang.org/t/best-practices-for-generating-vectors-with-ones-zeros-rand-etc/40197/7 "2020-05-26T15:30:57Z")

</div>

MATLAB dosn’t have true (flat) vectors, even a scalar is a 1x1 matrix. So, both 3x1 and 1x3 are called vectors in MATLAB, and when they are input to `diag`, you get a matrix with the main diagonal as the entered vector elements. In Julia, `n`, `1xn`, and `nx1` are three different things. So, `Diagonal` sees `ones(n)` as a vector, `nx1` and `1xn` as 2D arrays, hence you get different results.

Julia’s approach is by far more accurate and consistent. The developers of Julia take such fundamental design decisions only after serious research and discussions. After a few days of using Julia all the misconceptions of MATLAB will naturally go away. [Watch this talk, e.g., from Julia conference](https://www.youtube.com/watch?v=C2RO34b_oPM), to see how much study is done before any important design decision is taken in Julia.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [May 26, 2020, 3:36pm UTC](https://discourse.julialang.org/t/best-practices-for-generating-vectors-with-ones-zeros-rand-etc/40197/8 "2020-05-26T15:36:28Z")

</div>

For another example of how seriously we’ve taken this, you could read [https://github.com/JuliaLang/julia/issues/4774](https://github.com/JuliaLang/julia/issues/4774). It was the 400+ comment issue where tlots of this was figured out.

---

<div class="post-metadata">

### Author: ![ark](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@ark](https://discourse.julialang.org/u/ark)
#### Post date: [May 26, 2020, 3:54pm UTC](https://discourse.julialang.org/t/best-practices-for-generating-vectors-with-ones-zeros-rand-etc/40197/9 "2020-05-26T15:54:49Z")

</div>

Ok, guys. I think I have good pointers to read up now. Perhaps, as one of you mentions, it’s only a matter of getting used to Julia. I’ll report back if I have anything interesting to add besides the information in the supplied links and video.

PS: It’s a knowledgeable and considerate community here. Thank you!

---

<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: [May 26, 2020, 4:14pm UTC](https://discourse.julialang.org/t/best-practices-for-generating-vectors-with-ones-zeros-rand-etc/40197/10 "2020-05-26T16:14:02Z")

</div>

Don’t hesitate to ask if things are still unclear. This is the right forum for it.

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [May 26, 2020, 4:46pm UTC](https://discourse.julialang.org/t/best-practices-for-generating-vectors-with-ones-zeros-rand-etc/40197/11 "2020-05-26T16:46:55Z")

</div>

I suggest you watch this excellent talk by Jiahao Chen on the subject from JuliaCon a couple of years ago:

[![](https://global.discourse-cdn.com/julialang/original/3X/e/5/e55a2a411b08eba0d6e2cdb7b9392b0f4a8a5eee.jpeg "JuliaCon 2017 | Taking Vector Transposes Seriously | Jiahao Chen") ](https://www.youtube.com/watch?v=C2RO34b_oPM)

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [May 26, 2020, 4:47pm UTC](https://discourse.julialang.org/t/best-practices-for-generating-vectors-with-ones-zeros-rand-etc/40197/12 "2020-05-26T16:47:08Z")

</div>

> [@ark](#):
>
> When applied to the function `Diagonal` the dimension `Array{Float62,2}` or `Array{Float62,1}` seems to indicate the behaviour the function needs to adopt.

On this point strongly I agree — I really wish the `Diagonal` constructor didn’t have this confusion.

It is important to remember that 1-dimensional arrays are indeed different from 2-d arrays that happen to only have one column, but in the context of linear algebra we do try to make 1-d arrays participate in the algebra of matrices as consistently as possible.
