1
$\begingroup$

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)

$\endgroup$
1
  • $\begingroup$ For the first part: (Less @@@ Subsets[#, {2}]) & /@ {{3, 8, 9, 2}, {1, 5, 2, 9}, {4, 6, 10, 1}}. $\endgroup$ Commented Aug 21, 2017 at 14:21

2 Answers 2

2
$\begingroup$
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]
$\endgroup$
2
  • $\begingroup$ Use Boole[#1 < #2] & instead of If[#1 < #2, 1, 0] &. $\endgroup$ Commented Aug 21, 2017 at 14:57
  • $\begingroup$ @J.M. Edited. Thanks. $\endgroup$
    – Alan
    Commented Aug 21, 2017 at 15:02
0
$\begingroup$
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)$

$\endgroup$
3
  • $\begingroup$ I dont have the Catenate function... $\endgroup$
    – Mudy Fa
    Commented Aug 21, 2017 at 14:56
  • $\begingroup$ but this looks right $\endgroup$
    – Mudy Fa
    Commented Aug 21, 2017 at 14:57
  • $\begingroup$ @Mudy, then please edit your question to say what version you are using. $\endgroup$ Commented Aug 21, 2017 at 14:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.