A common method to generate and initialize vectors using ones, zeros, or random values, is, e.g.,
However, in Julia, such initializations result in a matrix type (!) rather than a vector:
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.