What is the most readable and efficient way to write a standard basis vector
e_1 = [1,0,...,0]
...
e_n = [0,...,0,1]
in Julia? (Similar question to this and this matlab question)
On
The most readable version I found to write standard basis vector e_k was
using LinearAlgebra: I
I[1:n, k]
How this works: The identity is of type Uniform Scaling I = LinearAlgebra.UniformScaling(1.) which means it acts as a (dimensionless) abstract matrix (but is not allocated). We can therefore index into it to get a standard basis vector.
The existing answers produce an
Array{Bool}or aBitArray. Depending on what you're doing, you may also be interested in OneHotArrays.jl: