David Halley
David Halley

Reputation: 457

In python, repeat a 2d array different amount of times

I am working in python. I have an array stud with shape (3, 11, 16) and another array times = np.array[740, 560, 600]. I want to repeat the first slice of a (0 ,11 ,16) 740 times. Then I want to repeat the next slice of a (1, 11, 16) 560 times and the same with the third slice.

This is my attempt

new_array = []
for i in times:
    for j in range(len(stud)):
        rep = np.repeat(stud[j,:,:], i, axis=0)

The problem here is that all slices j is repeated i times. I want to make it work so that, for the first i then only the first j is executed. For the second i only the second j is executed and so forth. Any idea of how to do this?

Upvotes: 0

Views: 105

Answers (1)

bouletta
bouletta

Reputation: 525

I believe what you are looking for is :

new_array = []
for i, j in zip(times, range(len(stud))):
    rep = np.repeat(stud[j,:,:], i, axis=0)

This will go through the loop 3 times:

First time, i=740, j=0

Second time, i=560, j=1

Third time, i=600, j=2

Please correct me if I misunderstood your question.

Upvotes: 1

Related Questions