Given a multidimensional, rectangular array and a list of dimensions, such as:
[ [1, 2],
[3, 4] ]
[3, 4]
Your challenge is to extend the matrix to the dimensions given in the list.
To extend an array to a length in a single direction, simply repeat its elements; for example, if we want to extend [1, 2, 3]
to length 7, the result would be [1, 2, 3, 1, 2, 3, 1]
.
For example, with the above, we can extend it to length 3:
[ [1, 2],
[3, 4],
[1, 2] ]
Then extend each row to length 4:
[ [1, 2, 1, 2],
[3, 4, 3, 4],
[1, 2, 1, 2] ]
However, your code should work for arrays with arbitrary dimensions. For example, if we take the 3D array
[ [ [1, 2],
[3, 4] ],
[ [5, 6],
[7, 8] ] ]
And dimensions [3, 3, 5]
, the result is:
[ [ [1, 2, 1, 2, 1],
[3, 4, 3, 4, 3],
[1, 2, 1, 2, 1] ],
[ [5, 6, 5, 6, 5],
[7, 8, 7, 8, 7],
[5, 6, 5, 6, 5] ],
[ [1, 2, 1, 2, 1],
[3, 4, 3, 4, 3],
[1, 2, 1, 2, 1] ] ]
You can assume that the dimensions will be strictly greater than the corresponding dimensions of the array, and that there will be the same amount as the list has dimensions. You may take dimensions in reverse.
This is code-golf, shortest wins!
Testcases
[[1, 2], [3, 4]], [3, 4] -> [[1,2,1,2],[3,4,3,4],[1,2,1,2]]
[[[1,2],[3,4]],[[5,6],[7,8]]], [3, 3, 5] -> see above
[1, 2, 3], [5] -> [1, 2, 3, 1, 2]
[[4], [3], [2]], [4,3] -> [[4,4,4],[3,3,3],[2,2,2],[4,4,4]]