4
$\begingroup$

Excuse my naive question. Is there a quick/efficient way to transform

{{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}}

to a lower-triangular matrix-form

{{1, 0, 0, 0}, {1, 2, 0, 0}, {1, 2, 3, 0, 0}, {1, 2, 3, 4}}

Just looking for an idea, but a sample code or an educational reference are also appreciated. Thanks in advance.

$\endgroup$
4
  • 1
    $\begingroup$ PadRight[#, 4] & /@ {{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}}. I assumed there was a typo in your matrix (there are five elements in the third list of your desired output). $\endgroup$
    – march
    Nov 13, 2015 at 18:45
  • 7
    $\begingroup$ Actually, just plain PadRight[{{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}}] works! $\endgroup$ Nov 13, 2015 at 18:46
  • $\begingroup$ @J.M. The very last usage case in the help docs: pads a ragged array list with zeros to make it full. Never even noticed! $\endgroup$
    – march
    Nov 13, 2015 at 18:49
  • 1
    $\begingroup$ Great comments. Please place them as answers I can favor. $\endgroup$ Nov 13, 2015 at 19:19

0