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

**URL:** <https://discourse.julialang.org/t/create-matrix-such-that-each-row-is-the-difference-of-an-array-with-one-of-its-values/86040>\
**Category:** General Usage\
**Tags:** question, package, arrays\
**Created:** [August 19, 2022, 7:12pm UTC](https://discourse.julialang.org/t/create-matrix-such-that-each-row-is-the-difference-of-an-array-with-one-of-its-values/86040 "2022-08-19T19:12:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elii17](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@elii17](https://discourse.julialang.org/u/elii17)\
**Post date:** [August 19, 2022, 7:12pm UTC](https://discourse.julialang.org/t/create-matrix-such-that-each-row-is-the-difference-of-an-array-with-one-of-its-values/86040/1 "2022-08-19T19:12:06Z")

</div>

Hey ! I was wondering if there was an efficient if given an array

> a = [x\_1,...,x\_i]

to get a matrix that is given by

> M = [[x\_1-x\_1,x\_2-x\_1,...,x\_i - x\_1],[x\_1-x\_2,x\_2-x\_2,...,x\_i - x\_2] ,etc.]

The only thing I have in my mind is

> t\_array = [x1,x2,x3,etc]  
> a = Array{Array}(undef, 1, length(t\_array))  
> for i in 1:length(t\_array)  
> a[i] = t\_array[:] .- t\_array[i]  
> end  
> Is there a package of any use that could be used ??

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [August 19, 2022, 7:20pm UTC](https://discourse.julialang.org/t/create-matrix-such-that-each-row-is-the-difference-of-an-array-with-one-of-its-values/86040/2 "2022-08-19T19:20:20Z")

</div>

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
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]

```

---

<div class="post-metadata">

**Author:** ![elii17](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@elii17](https://discourse.julialang.org/u/elii17)\
**Post date:** [August 23, 2022, 2:43pm UTC](https://discourse.julialang.org/t/create-matrix-such-that-each-row-is-the-difference-of-an-array-with-one-of-its-values/86040/3 "2022-08-23T14:43:49Z")

</div>

Great shortcut thanks !

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 21, 2023, 10:06am UTC](https://discourse.julialang.org/t/create-matrix-such-that-each-row-is-the-difference-of-an-array-with-one-of-its-values/86040/4 "2023-05-21T10:06:02Z")

</div>

I ran a quick test with the following expressions.  
I would have expected a different result for the last one.  
Can someone explain to me what and how is checked (or even transformed!?) to have  
`A+A'==zeros(4,4)` true?

```julia
julia> A=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+A'==zeros(eltype(A),size(A))
true

julia> A+A'==zeros(4,4)
true

```
