TransWikia.com

I want to Prepend the Row Number of the Matrix to all Elements within that row

Mathematica Asked by Taylor Minckley on February 8, 2021

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

5 Answers

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

Answered by m_goldberg on February 8, 2021

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.

Answered by Taylor Minckley on February 8, 2021

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

Answered by kglr on February 8, 2021

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

Answered by J. M.'s ennui on February 8, 2021

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

Answered by Carl Woll on February 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP