Tile or repeat n-dimensional arrays in Julia

I am looking for a general function for alternating or repeating matrices along an arbitrary number of dimensions an arbitrary number of times. Python and Matlab have these functions in the NumPy tile and Matlab repmat functions. The Julia repmat function only supports up to 2-dimensional arrays.

The function should look like repmatnd (a, (n1, n2, ..., nk)). a is an array of arbitrary dimension. The second argument is a tuple that determines the number of times the array is repeated for each dimension k.

Any idea how to draw a Julia array on more than 2 dimensions? In Python, I would use np.tile in matlab repmat as well, but the repmat function in Julia only supports 2 dimensions.

For instance,

x = [1 2 3] repmatnd(x, 3, 1, 3) 

Result:

 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 

And for

 x = [1 2 3; 1 2 3; 1 2 3] repmatnd(x, (1, 1, 3)) 

will lead to the same as before. I suppose Julia developers will implement something similar in the standard library, but until then it would be nice to have a fix.

+6
source share
1 answer

Use 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 
+6
source

Source: https://habr.com/ru/post/972521/


All Articles