Inspired by Copied from this question at Stack Overflow.
Given a matrix A
, create a matrix B
such that the columns of A
are arranged in a block-diagonal fashion. For example, given
1 2 3
4 5 6
the output would be
1 0 0
4 0 0
0 2 0
0 5 0
0 0 3
0 0 6
Rules
Input and output may be in the form of 2D arrays, nested arrays, or strings with different separators for rows and columns.
Numbers in the input (matrix A
) will be positive integers.
Unary format is allowed, as long as zeros in the output are displayed in some reasonable way. For example, the above result could be displayed using quotation marks to enclose each number:
'1' '' ''
'1111' '' ''
'' '11' ''
'' '11111' ''
'' '' '111'
'' '' '111111'
Test cases
Input, output:
1 2 3
4 5 6
1 0 0
4 0 0
0 2 0
0 5 0
0 0 3
0 0 6
10 20
10 0
0 20
10
20
10
20
1 2 3
10 20 30
100 200 300
1 0 0
10 0 0
100 0 0
0 2 0
0 20 0
0 200 0
0 0 3
0 0 30
0 0 300
2 4
6 8
10 12
2 0
6 0
10 0
0 4
0 8
0 12