Skip to main content
edited tags
Link
Source Link

Substitution and replacement in a matrix according to the position of an item in another matrix

I have the following problem and obviously I need help

  1. a list of choices --- in fact journals but this doesn't matter

    votes = { {ff, tt, ii, "-", "-", "-"}, {ii, tt, gg, dd, hh, ff}, {ii, tt, gg, "-", "-", "-"}, {ff, tt, ii, gg, dd, hh}, {hh, ii, gg, "-", "-", "-"}, {hh, ff, ii, "-", "-", "-"}, {ff, tt, ii, hh, dd, gg}, {tt, ff, "-", "-", "-", "-"}, {gg, ii, "-", "-", "-", "-"}, {dd, ii, tt, "-", "-", "-"}, {gg, dd, tt, "-", "-", "-"}, {ff, hh, ii, gg, tt, dd}, {ff, ii, gg, hh, "-", "-"}, {ff, gg, hh, tt, "-", "-"}, {tt, "-", "-", "-", "-", "-"}, {ii, dd, gg, ff, "-", "-"}, {ii, hh, gg, tt, dd, ff}, {ii, hh, tt, gg, "-", "-"}, {tt, dd, ii, hh, gg, ff}, {ii, hh, gg, "-", "-", "-"}, {ff, gg, ii, "-", "-", "-"}, {hh, tt, ii, gg, ff, dd}, {ii, tt, dd, "-", "-", "-"}, {ii, tt, gg, "-", "-", "-"}, {dd, tt, ii, gg, ff, hh}, {tt, dd, ii, gg, hh, ff}, {gg, dd, ii, tt, ff, hh}, {ii, tt, gg, dd, "-", "-"}, {ff, tt, ii, "-", "-", "-"}, {hh, ff, tt, "-", "-", "-"}, {tt, ii, hh, gg, "-", "-"}, {ii, gg, ff, tt, dd, hh} };

I have constructed a table

tab = Table[{dd, ff, gg, hh, ii, tt}, {i, 1, 33}]

The two tables seem to have the same structure but votes is about the preferences and tab about the ranking of the journals.

I want to substitute in tab, for each line, the true position of each item in the corresponding line of votes. For instance as the line 2 of votes is ii, tt, gg, dd, hh, ff in tab the second line must be 4, 3, 6, 5, 1, 2.

I have tried some thing like that

ReplacePart[tab, {6, 2} -> Position[votes[[6, All]], ff][[1, 1]]]

which, according to what I think to have understood, replaces the second element of the line 6 of tab by the position of ff in the line 6 of votes. But, I have not found how to iterate --- by functional programming --- or to loop by --- procedural programming --- on all elements of the table. For undocumented answers --- "-" ---, one can set a very large number.

Thanks for the help