I have a matrix of nested lists that looks like this:
matrix =
{{{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 0}},
{{2, 3}, {4, 5}, {6, 7}, {8, 9}, {0, 1}},
{{1, 3}, {2, 4}, {3, 5}, {4, 6}, {5, 7}}}
and I want to Prepend the row number 'x' to each of the elements within each row like this:
{{{x, 1, 2}, {x, 3, 4}, {x, 5, 6}, {x, 7, 8}, {x, 9, 0}},
{{x, 2, 3}, {x, 4, 5}, {x, 6, 7}, {x, 8, 9}, {x, 0, 1}},
{{x, 1, 3}, {x, 2, 4}, {x, 3, 5}, {x, 4, 6}, {x, 5, 7}}}
so that the final product looks like this:
{{{1, 1, 2}, {1, 3, 4}, {1, 5, 6}, {1, 7, 8}, {1, 9, 0}},
{{2, 2, 3}, {2, 4, 5}, {2, 6, 7}, {2, 8, 9}, {2, 0, 1}},
{{3, 1, 3}, {3, 2, 4}, {3, 3, 5}, {3, 4, 6}, {3, 5, 7}}}
I have played around with something like MapIndexed[Prepend[#,x]&,matrix,{3}]
which successfully gets me to the intermediate matrix as described above where "x" is prepended, but I can't figure out how to make "x" conditionally equal the index of the row.
Your help is very much appreciated! Thanks so much in advance!!!
Best regards, Taylor
MapIndexed[Prepend[#, First[#2]] &, matrix, {2}]
$\endgroup${{1, {1,2}, {3,4}, {5,6}, {7,8}, {9,0}}, {1, {2,3}, {4,5}, {6,7}, {8,9}, {0,1}, {1, {1,3}, {2,4}, {3,5}, {4,6}, {5,7}}}
$\endgroup$