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?

  • $\begingroup$ 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. $\endgroup$
    – bbgodfrey
    Commented Aug 30, 2015 at 11:08
  • 2
    $\begingroup$ Look up Outer[]. $\endgroup$ Commented Aug 30, 2015 at 11:10
  • $\begingroup$ Could you post an answer for how to do minus operation? $\endgroup$
    – user32682
    Commented Aug 30, 2015 at 11:12
  • $\begingroup$ Many options work, such as dif = Table[lst[[i]] - lst[[j]], {i, 5}, {j, 5}]. $\endgroup$
    – bbgodfrey
    Commented Aug 30, 2015 at 11:13
  • $\begingroup$ Did you do what I said and look at the docs? $\endgroup$ Commented Aug 30, 2015 at 11:14

1 Answer 1


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


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


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

all yield:

enter image description here

  • $\begingroup$ Subract is keyword. ok. $\endgroup$
    – user32682
    Commented Aug 30, 2015 at 11:38
  • $\begingroup$ @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. $\endgroup$
    – ubpdqn
    Commented Aug 30, 2015 at 11:42
  • $\begingroup$ @Arul This guide, guide/ArithmeticFunctions, will introduce you to other keywords, such as Plus and Times, too. $\endgroup$
    – Michael E2
    Commented Aug 30, 2015 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.