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?
1 Answer
$\begingroup$
$\endgroup$
3
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:
-
$\begingroup$ Subract is keyword. ok. $\endgroup$– user32682Commented 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$– ubpdqnCommented 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$ Commented Aug 30, 2015 at 12:18
Outer[]
. $\endgroup$dif = Table[lst[[i]] - lst[[j]], {i, 5}, {j, 5}]
. $\endgroup$