create a vector increased by 0.5 in julia

Viewed 74

Suppose I want to create a vector in Julia. The vector should be [0,0.5,1,1.5,2,....20]. What's the quick command to create a vector like this, from 0 to 20, increasing by 0.5?

2 Answers

This will create a range, which is a kind of vector:

0:0.5:20

This does the same thing

range(0, 20; step=0.5)

If you absolutely need to make a Vector, you can use collect on either of these, but in most cases you should not, and just use 0:0.5:20 directly.

There are many ways to do this. While I prefer the colon array syntax already mentioned, this could be used for more general construction:

[0.5*i for i in 1:20]
Related