# I Want to Prepend the Row Number of the Matrix to All Elements within That Row

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.

Best regards, Taylor

• MapIndexed[Prepend[#, First[#2]] &, matrix, {2}] Jan 20, 2021 at 18:55
• @andre314 That gives me a "1" Prepended to each row, but outside the existing elements: {{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}}} Jan 21, 2021 at 2:07
• Jan 22, 2021 at 19:20

One possibility is to use PadLeft:

PadLeft[
matrix,
Dimensions[matrix] + {0, 0, 1},
List /@ List /@ Range[Length[matrix]]
]


{{{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}}}

• This seemed to give me errors: "Thread: Objects of unequal length in {1}+{0,0,1} cannot be combined." and "PadLeft: List of machine-sized integers expected at position 2 in PadLeft" This was the output when I ran the exact code you posted: PadLeft[ (matrix), {1} + {0,0,1}, {{{1}}} ] Jan 21, 2021 at 3:51

### Updated

Here are three other ways of doing it.

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}}};

indxs = Range @ Length @ matrix;

MapThread[Table[{#2, Splice @ pair}, {pair, #1}]&, {matrix, indxs}]

MapThread[Function[pair, {#2, Splice @ pair}] /@ #1&, {matrix, indxs}]

MapIndexed[Function[pair, {#2[[1]], Splice @ pair}] /@ #1&, matrix]


All give the result

{{{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}}}


This combines andre's idea in the comments and Carl's idea:

MapIndexed[PadLeft[#1, {Automatic, 3}, #2] &, matrix]
{{{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}}}

• The {Automatic, 3} truncates the first two columns from matrix with no other change {{{5,6}, {7,8}, {9,0}}, {{6,7}, {8,9}, {0,1}, {{3,5}, {4,6}, {5,7}}} Changing to {Automatic, 6} gives me a "1" Prepended to each row, but outside the existing elements as happened above with other solutions: {{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}}} Thanks for the effort though! Still trying things out. Jan 21, 2021 at 4:04
• I was assuming the matrices in your list all had two columns, which is why I used the {Automatic, 3} setting in PadLeft[]. The example in your comment, @Taylor, does not match this pattern, but MapIndexed[PadLeft[#1, {Automatic, 3}, #2] &, {{{5, 6}, {7, 8}, {9, 0}}, {{6, 7}, {8, 9}, {0, 1}}, {{3, 5}, {4, 6}, {5, 7}}}] works as expected. Jan 21, 2021 at 4:32
ClearAll[matrix]
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}}};

MapIndexed[Prepend[First @ #2] /@ # &] @ matrix

{{{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}}}


You can also use

MapIndexed[Flatten /@ Thread[{First@#2, #}] &, matrix]

% == %%

True

• As what happened with @andre314's solution, that also only gives me a "1" Prepended to each row, but outside the existing elements: {{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}}} Thanks for the effort though! Still trying things out. Jan 21, 2021 at 4:00
• @TaylorMinckley, posted methods give the desired result in versions 11.3.0 (Windows 10 84 bit) and 12.2.0 (Wolfram Cloud). You might want to try starting with a fresh kernel.
– kglr
Jan 21, 2021 at 4:10
• thank you! Sorry, I'm new to Mathematica. Ill try refreshing my kernel (? gotta go google what that means...). I really appreciate your help and patience. I did figure out how to get it to work on my end, though using MapIndexed[Prepend[#, Part[#2, 2]] &, matrix, {3}] Jan 21, 2021 at 4:14

I was able to get this to work, using: MapIndexed[Prepend[#, Part[#2, 2]] &, matrix, {3}]

and as @kglr mentioned in the comments here, other posted solutions may work on a fresh kernel.

• If MapIndexed[Prepend[#, Part[#2, 2]] &, matrix, {3}] gives you the desired result; then matrix in your question should have an additional pair of braces; i.e., 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}}}}.
– kglr
Jan 21, 2021 at 4:48

Two other possibilities:

Block[{i = 1, p}, p = Prepend[i++] /@ # &; p /@ matrix]


and

ReplacePart[matrix, {i_, j_} :> Prepend[i] @ matrix[[i, j]]]


Both produce

{
{{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}}
}

MapIndexed[Flatten[{First@#2, #1}] &, matrix, {2}]

(* {
{{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}}
} *)


MapIndexed[ArrayFlatten@{{First@#2, #1}} &]@matrix