Create matrix, such that each row is the difference of an array with one of its values

Do you want a matrix or a vector of vectors? They’re not the same thing in Julia.

I’d just do a' .- a to create the matrix. Or for the vector of vectors use a comprehension:

julia> using Symbolics

julia> @variables x₁ x₂ x₃ x₄;

julia> a = [x₁, x₂, x₃, x₄]
4-element Vector{Num}:
 x₁
 x₂
 x₃
 x₄

julia> a' .- a
4×4 Matrix{Num}:
       0  x₂ - x₁  x₃ - x₁  x₄ - x₁
 x₁ - x₂        0  x₃ - x₂  x₄ - x₂
 x₁ - x₃  x₂ - x₃        0  x₄ - x₃
 x₁ - x₄  x₂ - x₄  x₃ - x₄        0

julia> [a .- x for x in a]
4-element Vector{Vector{Num}}:
 [0, x₂ - x₁, x₃ - x₁, x₄ - x₁]
 [x₁ - x₂, 0, x₃ - x₂, x₄ - x₂]
 [x₁ - x₃, x₂ - x₃, 0, x₄ - x₃]
 [x₁ - x₄, x₂ - x₄, x₃ - x₄, 0]
6 Likes