0

If given a bunch of 1-d arrays, how does one fill a 2-dimensional array with values repeating in cycles, so as to cover all possible values in order?

Toy example :

input : a = np.arange(4), b = np.arange(5), c = np.arange(2)
output:[[0 , 0 , 0 ],
        [0 , 0 , 1 ],
        [0 , 1 , 0 ],
        [0 , 1 , 1 ],
        [0 , 2 , 0 ],
        [0 , 2 , 1 ],
        ............,
        [3 , 4 , 1 ],
        [3 , 4 , 0 ]]

        40 elements in total. 

Is it possible to do this using numpy broadcasting?

Alternatives I could think of were using the itertools module's cycle/ repeat generators, but I suspect it will be slow since I will have to concatenate lists together after generating them.

Purpose: I'm trying to solve a specific case of the Frobenius problem with integer values (which is a subtask of another problem I have), and I have figured out a way to do it using numpy broadcasting - if only I have the above 2-d array/matrix, where I'm currently stuck.

Ambareesh
  • 324
  • 1
  • 4
  • 18

0 Answers0