3
$\begingroup$

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

$\endgroup$
3
  • 5
    $\begingroup$ MapIndexed[Prepend[#, First[#2]] &, matrix, {2}] $\endgroup$
    – andre314
    Jan 20, 2021 at 18:55
  • $\begingroup$ @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}}} $\endgroup$ Jan 21, 2021 at 2:07
  • $\begingroup$ Related: mathematica.stackexchange.com/questions/194395/… $\endgroup$
    – Michael E2
    Jan 22, 2021 at 19:20

5 Answers 5

3
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ 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}}} ] $\endgroup$ Jan 21, 2021 at 3:51
1
$\begingroup$
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
$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$ Jan 21, 2021 at 4:00
  • $\begingroup$ @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. $\endgroup$
    – kglr
    Jan 21, 2021 at 4:10
  • $\begingroup$ 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}] $\endgroup$ Jan 21, 2021 at 4:14
1
$\begingroup$

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}}}
$\endgroup$
0
$\begingroup$

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}}}
$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$ Jan 21, 2021 at 4:04
  • 1
    $\begingroup$ 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. $\endgroup$ Jan 21, 2021 at 4:32
0
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ 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}}}}. $\endgroup$
    – kglr
    Jan 21, 2021 at 4:48

Your Answer

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

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