Skip to main content
Became Hot Network Question
edited tags
Link
rhermans
  • 37.4k
  • 4
  • 61
  • 152
Source Link
eldo
  • 83.1k
  • 6
  • 72
  • 202

Fill an empty matrix with the depth of its elements

Given an arbitrarily nested empty matrix like for example

mat =
  {
   {},
   {{}, {}},
   {{}, {{}}, {}},
   {{}, {{}}, {}, {{{}}}}
   };

and knowing that

Depth /@ mat

{2, 3, 4, 5}

Question 1

how can I produce the following result?

res =
  {
   {2},
   {{3}, {3}},
   {{3}, {{4}}, {3}},
   {{3}, {{4}}, {3}, {{{5}}}}
   };

Question 2

Given

{{2}, {3, 3}, {3, 4, 3}, {3, 4, 3, 5}};

how can I produce the above empty matrix mat ?