在Julia中平铺或重复n维数组

jto*_*rca 9 matrix julia

我正在寻找一个通用函数来沿任意数量的维度平铺或重复矩阵任意次数.Python和Matlab在NumPy的tile和Matlab的repmat函数中都有这些特性.Julia的repmat函数似乎只支持二维数组.

该函数应该看起来像repmatnd(a,(n1,n2,...,nk)).a是任意维度的数组.第二个参数是一个元组,指定每个维度k重复数组的次数.

知道如何在大于2维上平铺Julia数组吗?在Python中我会使用np.tile和matlab repmat,但Julia中的repmat函数只支持2维.

例如,

x = [1 2 3]
repmatnd(x, 3, 1, 3)
Run Code Online (Sandbox Code Playgroud)

会导致:

1 2 3
1 2 3
1 2 3

1 2 3
1 2 3
1 2 3

1 2 3
1 2 3
1 2 3
Run Code Online (Sandbox Code Playgroud)

并为

x = [1 2 3; 1 2 3; 1 2 3]
repmatnd(x, (1, 1, 3))
Run Code Online (Sandbox Code Playgroud)

会导致与以前相同的事情.我想Julia开发人员会在标准库中实现这样的东西,但在那之前,修复它会很好.

Joh*_*ite 10

用途repeat:

julia> X = [1 2 3]
1x3 Array{Int64,2}:
 1  2  3

julia> repeat(X, outer = [3, 1, 3])
3x3x3 Array{Int64,3}:
[:, :, 1] =
 1  2  3
 1  2  3
 1  2  3

[:, :, 2] =
 1  2  3
 1  2  3
 1  2  3

[:, :, 3] =
 1  2  3
 1  2  3
 1  2  3
Run Code Online (Sandbox Code Playgroud)

  • 因为`repmat`是原来的函数,后来添加了`repeat`。Repeat 也比 `repmat` 更通用,因为它支持内部重复和外部重复。 (2认同)