Broadcast two vectors to form a matrix

first off, it’s much easier to just:

julia> (1:9) .+ (1:9)'
9×9 Matrix{Int64}:
  2   3   4   5   6   7   8   9  10
  3   4   5   6   7   8   9  10  11
  4   5   6   7   8   9  10  11  12
  5   6   7   8   9  10  11  12  13
  6   7   8   9  10  11  12  13  14
  7   8   9  10  11  12  13  14  15
  8   9  10  11  12  13  14  15  16
  9  10  11  12  13  14  15  16  17
 10  11  12  13  14  15  16  17  18

whichever is more clear for your use case, Julia is not a language where for-loop would be slow anyway, it doesn’t matter much as long as you try to find the way to do it such that it’s clear and minimize memory stress

3 Likes