How to efficiently create a table/array through known function?

Because each matrix entry is a two-element Vector, you’ll have one newly allocated vector times the number of elements in your matrix, the multiplications and addition you’re doing even increases the allocations. Instead you can work with tuples, for example:

function func()
    [m .* (2*pi,(2pi)/sqrt(3)) .+ n .* (0.0,(4*pi)/sqrt(3)) for m in range(0.,1.,1001), n in range(0.,1.,1001)]
end

@time func()
0.005967 seconds (2 allocations: 15.289 MiB)

Edit: A tad slower than @lmiq :slight_smile:

2 Likes