# How to create pairwise difference matrix entry?

I have a $16\times 1$ array of integer entries named $H$. How do I create a $16\times 16$ pairwise difference matrix between all entries in array?

• Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory Tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. Commented Aug 30, 2015 at 11:08
• Look up Outer[]. Commented Aug 30, 2015 at 11:10
• Could you post an answer for how to do minus operation?
– user32682
Commented Aug 30, 2015 at 11:12
• Many options work, such as dif = Table[lst[[i]] - lst[[j]], {i, 5}, {j, 5}]. Commented Aug 30, 2015 at 11:13
• Did you do what I said and look at the docs? Commented Aug 30, 2015 at 11:14

Just for the purpose of illustration (the comments of Guesswhoitis. and bbgogfrey). here are some ways (I prefer Outer):

Using:

lst = {15, 15, 1, 14, 10, 14, 4, 8, 8, 14, 11, 5, 13, 0, 5, 4}


then

Outer[Subtract, lst, lst] // MatrixForm
Table[lst[[i]] - lst[[j]], {i, 16}, {j, 16}] // MatrixForm
Partition[Subtract @@@ Tuples[lst, 2], 16] // MatrixForm


all yield:

• Subract is keyword. ok.
– user32682
Commented Aug 30, 2015 at 11:38
• @Arul there are a number of ways to do things in Mathematica. If you want absolute difference, obviously this could would have to be modified. I suggest (as suggested by the other users) you work through the documentation, search the site and play...these are the best ways to learn and in general "do this..." type requests are not met favourably. Commented Aug 30, 2015 at 11:42
• @Arul This guide, guide/ArithmeticFunctions, will introduce you to other keywords, such as Plus and Times, too. Commented Aug 30, 2015 at 12:18