# Extend a matrix in all directions

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 , 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]]

• Can't wait for the APL/BQN answer to be like 1-4 bytes long lmao Commented Jun 19, 2022 at 7:25
• ... and for Wolfram to have the obligatory builtin (remember upgoat?).
– ojdo
Commented Jun 19, 2022 at 22:32
• can i take the input as a single list, with the matrix as the last element? Commented Jun 20, 2022 at 3:11
• @Jonah That seems reasonable Commented Jun 20, 2022 at 3:36

# BQN, 13 8 bytesSBCS

A bit closer to 4 bytes. Takes the array as left argument and the shape vector on the right.

(⍉⁼⥊⎉1)´


Run online! <- This uses the new array notation using [].

The reduction ´ iterates over the shape vector from right to left, using the large array as a starting value. ⥊⎉1 resizes the trailing axis by cycling elements, then ⍉⁼ rotates this axis to the front.

• I think you can remove ⁼
Commented Jun 19, 2022 at 9:39
• @Adám that would rotate the axes in the wrong direction (see [[[1]],[[2]]] F [2,3,4] for an example where that matters)
– ovs
Commented Jun 19, 2022 at 9:58
• Probably not worth a separate answer, but here's the J translation, 4 more bytes assuming i can take everything as a single list of boxes: (0|:$"1)&.>/ Try it online! Commented Jun 20, 2022 at 3:15 # Wolfram Language (Mathematica), 14 bytes ##~PadRight~#&  Try it online! # Factor + sequences.repeating, 65 63 bytes [ [ swap '[ _ cycle ] [ '[ _ map ] ] repeat call ] each-index ]  Try it online! This is very unlike how my Factor golfs usually turn out (what with constructing multiple functions and calling them in turn). I noticed that every solution follows this pattern: • [[1, 2], [3, 4]], [3, 4] -> 3 cycle [ 4 cycle ] map • [[[1,2],[3,4]],[[5,6],[7,8]]], [3, 3, 5] -> 3 cycle [ 3 cycle ] map [ [ 5 cycle ] map ] map So curry each dimension into the innermost quotation with cycle, then build up the nested map quotations around it based on its index in the list of dimensions and call it. Since recursion is extremely verbose in Factor, this iterative approach is all but guaranteed to be shorter. # Python 3, 51 bytes f=lambda m,d:d and[*map(f,m*d[0],d[0]*[d[1:]])]or m  Try it online! # 05AB1E, 7 bytes „∍ε«J.V  First input is the list of lengths; second the matrix. Explanation: „∍ε # Push string "∍ε" « # Append it to each integer of the first (implicit) input-list J # Join this list together to a single string .V # Evaluate and execute it as 05AB1E code # (after which the result is output implicitly)  ∍ is the extend buildin, which does exactly what this challenges asks: given a list and integer arguments, extend the list to the given integer's length. ε is the map builtin, which stays open until we close it with } or ] (which we don't want to do in this challenge). For example: „∍ε«J will convert the list of lengths [3,3,5] from the example of the challenge to string "3∍ε3∍ε5∍ε". Evaluated and executed as 05AB1E code, this will: 3∍ # Extend the (implicit) 3D input-matrix to 3 inner matrices ε # Map over each inner matrix: 3∍ # Extend the matrix to 3 inner rows ε # Map over each inner row: 5∍ # Extend the row to 5 values ε # No-op map  Luckily '∍ε isn't a dictionary word, otherwise the „∍ε would had to be "∍ε" instead. # Charcoal, 36 bytes ⊞υθＦη«≔⟦⟧ζＦυ«≔⁺ζκζＦ…κ⁻ιＬκ⊞κλ»≔ζυ»⭆¹θ  Try it online! Link is to verbose version of code. Explanation: ⊞υθ  Start by considering the first dimension. Ｆη«  Loop over the new dimensions. ≔⟦⟧ζ  Start collecting the elements of that dimension. Ｆυ«  Loop over all of the lists of that dimension. ≔⁺ζκζ  Collect the elements of the list. Ｆ…κ⁻ιＬκ⊞κλ  Cyclically append sufficient elements to reach the desired length. »≔ζυ  Save the elements as the list of lists of the next dimension. »⭆¹θ  Pretty-print the expanded array. # Python 3 with numpy, 5957 56 bytes lambda m,d:tile(m,d)[[*map(slice,d)]] from numpy import*  Try it online! −2 thanks to loopy walt, and another −1 by changing back to the deprecated use of a list for indexing. • -2 using map instead of comprehension. Commented Jun 19, 2022 at 18:14 # R, 6255 51 bytes Edit: -7 bytes thanks to Giuseppe, and -4 bytes thanks to pajonk \(a,d,*=array)a[t(t(which(T*d,T)-1)%%dim(a)+1)]*d Attempt This Online! # R, 49 bytes \(a,d)array(a[t(t(which(!a*0,T)-1)%%dim(a)+1)],d) Attempt This Online! Works with arrays of finite numbers. In this case, we don't need to create a new array of TRUE elements to get the indices, as we can just multiply the elements by zero and take the logical NOT. • @Giuseppe - Oh, much better, what was I thinking...? Thanks! Commented Jun 23, 2022 at 15:23 • -4 bytes by reassigning array. Commented Jun 24, 2022 at 4:48 • @pajonk - Nice, thanks! Commented Jun 24, 2022 at 6:47 # BQN, 22 bytesSBCS {⟨⟩𝕊𝕩:𝕩;(1↓𝕨)⊸𝕊¨𝕩⥊˜⊑𝕨}  Run online! recursive is shorter ¯\_(⌾‿⌾)_/¯ # BQN, 26 bytesSBCS {𝕩{a‿b𝕊𝕩:a⥊⚇b𝕩}´𝕨⋈¨⌽1+↕≠𝕨}  Run online! • Dang it razetime I said 1-4 bytes not 26 of them. Smh Commented Jun 19, 2022 at 7:44 • maybe there is a smarter way lmao Commented Jun 19, 2022 at 7:48 # Brev, 99 bytes (define(j l . m)(j((over(apply j x(cdr m)))l)(car m))) (define(j l k)(take(apply circular-list l)k))  (Displayed with one unnessecary newline for clarity.) Example usage: (pp (j '(((1 2) (3 4)) ((5 6) (7 8))) 3 3 5))  # jq, 58 bytes def r($r):[.[range($r[0])%length]|r($r[1:])? //.];r(input)


# Pip-xp, 20 bytes

aH:PObb?fMZa[b]RL#aa


Attempt This Online!

### Explanation

Kinda ugly, too bad Pip doesn't have any way to do currying or partial application.

aH:PObb?fMZa[b]RL#aa
b                ; Second argument, the list of dimensions
PO                 ; Pop the first element (call it N)
a                     ; First argument, the matrix
H:                   ; Take the first N elements (cyclically) and assign back to a
b?              ; Is b still nonempty?
; If so:
a          ;   Take the array
[b]       ;   and a list containing b
RL#a   ;   repeated len(a) times
MZ           ;   Zip together and map to each pair
f             ;   a recursive call to the main function
a  ; If not, just return a


# UiuaSBCS, 8 bytes

∧(⍉↙⊃∘▽)


Try it!

Pretty much the same thing as ovs' BQN answer.