# Create a pair matrix

I have a question about creating an $4 \times 4$ matrix, in which you can compare all elements of an vector.

For example:

I have 3 lists with 4 which includes in each of them 4 elements:

{{3, 8, 9, 2}, {1, 5, 2, 9},{4, 6, 10, 1}}


Call the first element of an vector is $R_1$, the second $R_2$, the third $R_3$, and the fourth $R_4$.

Now I want to check which element each list is smaller than other elements in the same list. For example:

$3<8,\, 3<9,\, 3<2,\, 8<9,\, 8 \nless 2, 9 \nless 8 \ldots$

I want a $4 \times 4$ matrix containing how often $R_1$ is smaller than $R_2$, or $R_2$ smaller than $R_3$, et c.

http://cewebs.cs.univie.ac.at/topics/RisikoManagement/index.php?m=F&t=info&c=aresource&CEWebS_type=image&CEWebS_file=Abb4_14.JPG&CEWebS_what=Doppelter~32~paarweiser~32~Vergleich&CEWebS_rev=2

But there could be a 5th element in a list, or $n$ lists.

(Mathematica Version 9.0)

• For the first part: (Less @@@ Subsets[#, {2}]) & /@ {{3, 8, 9, 2}, {1, 5, 2, 9}, {4, 6, 10, 1}}. – J. M. is away Aug 21 '17 at 14:21

## 2 Answers

lsts = {{3, 8, 9, 2}, {1, 5, 2, 9}, {4, 6, 10, 1}}
cmp[lst_] := Outer[Boole[#1 < #2] &, lst, lst]   (* edit: use Boole *)
Total[cmp /@ lsts]

• Use Boole[#1 < #2] & instead of If[#1 < #2, 1, 0] &. – J. M. is away Aug 21 '17 at 14:57
• @J.M. Edited. Thanks. – Alan Aug 21 '17 at 15:02
L = {{3, 8, 9, 2}, {1, 5, 2, 9}, {4, 6, 10, 1}};

pos = Transpose[{#, Reverse[#, 2]}] &[Subsets[Range[sz = Length[First[L]]], {2}]];

MatrixForm[SparseArray[Rule @@@ DeleteCases[Tally[Catenate[
MapThread[Part, {pos, Transpose[Map[-Sign[Subtract @@@
Subsets[#, {2}]] &, L]]}]]], {List, _}], {sz, sz}]]


$\left( \begin{array}{cccc} 0 & 3 & 3 & 1 \\ 0 & 0 & 2 & 1 \\ 0 & 1 & 0 & 1 \\ 2 & 2 & 2 & 0 \\ \end{array} \right)$

• I dont have the Catenate function... – Mudy Fa Aug 21 '17 at 14:56
• but this looks right – Mudy Fa Aug 21 '17 at 14:57
• @Mudy, then please edit your question to say what version you are using. – J. M. is away Aug 21 '17 at 14:58