# 列的块对角矩阵

16

``````1 2 3
4 5 6
``````

``````1 0 0
4 0 0
0 2 0
0 5 0
0 0 3
0 0 6
``````

### 规则

``````'1' '' ''
'1111' '' ''
'' '11' ''
'' '11111' ''
'' '' '111'
'' '' '111111'
``````

### 测试用例

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

A中的所有数字都会不同吗？

@Nᴮᶻ不，他们可以相等
Luis

7

# MATL，6个字节

``````"@N\$Yd
``````

``````[1, 2, 3; 4, 5, 6]
``````

### 说明

``````"         % implicitly input 2D array and loop over its columns
@       %   push column
N\$Yd    %   build block-diagonal matrix from all stack contents. Stack contents are
%   a single column in the first iteration, or a partially built 2D array
%   and a new column in all other iterations
% end loop
% implicit display
``````

3

## ES6，65个字节

``````a=>[].concat(...a[0].map((_,i)=>a.map(b=>b.map((c,j)=>i-j?0:c))))
``````

1
@WashingtonGuedes内部映射返回原始2D数组的副本，其中除一列外的所有列均被清零。然后需要将这些副本连接起来，而不仅仅是外部3​​D数组的元素。

3

# Mathematica，40 39字节

``````Transpose[DiagonalMatrix/@#]~Flatten~1&
``````

``````{{1,2,3},{4,5,6}}
``````

`DiagonalMatrix`从输入的行生成一个数组，其中每个数组都有对角线元素（3-D数组）。`Transpose`因此该`Flatten`操作会删除正确的括号对，以提供所需的矩阵（现在为二维数组）。

1

seequ '16

IPoiler '16

1

# 果冻，13个字节

``````ZLR’×L0ẋ;"Zz0
``````

### 怎么运行的

``````ZLR’×L0ẋ;"Zz0  Main link. Input: M (matrix)

Z              Transpose M.
L             Get the length of the result. This yields the number of M's columns.
R            Range; for m columns, yield [1, ..., m].
’           Decrement to yield [0, ..., m-1].
×L         Multiply each by the length (number of rows) of M.
This yields [0, n, ..., n(m-1)], where n is the number of rows.
0ẋ       Push a list of lists of zeroes.
First element is [], last is n(m-1) zeroes.
;"Z    Prepend the kth vector of zeroes to the kth column of M.
z0  Zip, filling the non-rectangular 2D array with zeroes.
``````

1

# Mathematica，111个字节

``````Join@@@ReplacePart[Table[0,#2/#3]~Table~#3~Table~#3,Table[{n,n}->#[[n]],{n,#3}]]&[Length@#,Length@Flatten@#,#]&
``````

IPoiler '16

1

## 红宝石，81岁 78 76 62字节

``->a{i=-1;a[0].flat_map{i+=1;a.map{|b|x=b.map{0};x[i]=b[i];x}}}``

``````->a{
i=-1;            # index of the flat_map
a[0]             # duplicate all rows as many times as necessary
.flat_map{       # we're wrapping each "group" of original rows with yet another
#  array, so we need to flat_map to get rid of those
i+=1;            # increment the index of the current subarray
a.map{|b|        # for each sub-subarray (these are the rows)...
x=b.map{0};      # zero everything out
x[i]=b[i];       # replace the desired elements
x}}}             # finally, return the modified array``````

1

# R，41个字节

``````pryr::f(Matrix::.bdiag(plyr::alply(a,2)))
``````

``````(a=matrix(1:6,ncol=3))
#      [,1] [,2] [,3]
# [1,]    1    3    5
# [2,]    2    4    6
pryr::f(Matrix::.bdiag(plyr::alply(a,2)))(a)
# 6 x 3 sparse Matrix of class "dgTMatrix"
#
# [1,] 1 . .
# [2,] 2 . .
# [3,] . 3 .
# [4,] . 4 .
# [5,] . . 5
# [6,] . . 6
``````

`plyr::alply(a,2)` 每列 `a`和返回的将这些结果组合在一个列表中

`Matrix::.bdiag(lst)` 从矩阵列表创建块对角矩阵

`pryr::f` 是创建函数的简便方法。

``````function(a){l=dim(a);diag(l[2])%x%matrix(1,nrow=l[1])*c(a)}
``````

1

## MATLAB，69 68字节

``````   function h=d(a)
[r,c]=size(a);h=repmat(a,c,1).*kron(eye(c),~~(1:r)')
``````

1

# APL（Dyalog Classic），11个字节

``⍉⍪⍉↑(0,⊢)\⎕``