2
$\begingroup$

Good morning, I have an input dataset in the following form:

data={{{team1,team4},{{0.0356739,0.0460788,0.0297593,0.048},{0.0728342,0.0940777,0.0607586,0.052},{0.0743518,0.0960378,0.0620245,0.0238}}},{{team3,team2},{{0.0356739,0.0728342,0.0743518,0.0238},{0.0460788,0.0940777,0.0960378,0.0133},{0.0297593, 0.0607586,0.0620245,0.0148}}},{{team5,team6},{{0.0639281,0.0879008,0.0604318,0.01438},{0.0879008,0.120863,0.0830935,0.03438},{0.0604318,0.0830935,0.0571267,0.05438}}}}

I would like to create a function to obtain the following output table:

output={data[[k,1]],1,X,2}

where the kth data[[k,1]]={team_k1,team_k2} represents the kth match and the kth event 1,X,2 represents the events home victory, draw, away victory for the kth match. So, the rules for these events are the following:

1: If[i>j,Sum[data[[k,2,i,j]]...{i,1,Length[data[[1,2,1]]]},{j,1,Length[data[[1,2,1]]]}
X: If[i=j,Sum[data[[k,2,i,j]]...{i,1,Length[data[[1,2,1]]]},{j,1,Length[data[[1,2,1]]]}
2: If[i<j,Sum[data[[k,2,i,j]]...{i,1,Length[data[[1,2,1]]]},{j,1,Length[data[[1,2,1]]]}
$\endgroup$
1
  • $\begingroup$ Mick, please visit tour re voting/accepting answers and FAQs for general questions about stack exchange sites. $\endgroup$
    – kglr
    Commented Feb 9, 2020 at 16:29

1 Answer 1

1
$\begingroup$
ClearAll[tabulate]
tabulate = Flatten[{#, Total[Join @@ UpperTriangularize[#2, 1]], Tr@#2, 
       Total[Join @@ UpperTriangularize[#2, -1]]}] & @@@ # &;

headers = {"home", "away", "1", "X", "2"};

TableForm[tabulate@data, TableHeadings -> {None, headers}]

enter image description here

Grid[Prepend[tabulate@data, headers], Dividers -> {All, {All, 2 -> Thick}}]

enter image description here

$\endgroup$
3
  • $\begingroup$ Thank you kglr! Would there be any way to associate a color scale depending on the value of the pecentage on the table? $\endgroup$
    – Nate
    Commented Feb 9, 2020 at 17:27
  • 1
    $\begingroup$ @Mick, that's another good question (with many possible answers). It is generally expected that new requirements posted in comments should be edited into the original question or, better yet, posted as a new question. A quick way is to color the numbers in tabulate[data] using ReplaceAll: e.g., styledtable= tabulate[data] /. x_?NumericQ :> Style[x, ColorData["Rainbow"]@x] $\endgroup$
    – kglr
    Commented Feb 9, 2020 at 18:05
  • $\begingroup$ Hi kglr, can you help me please? mathematica.stackexchange.com/questions/218408/… $\endgroup$
    – Nate
    Commented Apr 9, 2020 at 10:07

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.