MasterYoda
MasterYoda

Reputation: 230

Need efficient method to broadcast a smaller Numpy array into a larger one

TL;DR: I'm looking for a way to shorten the following code without using loops

# x = [m, n] Numpy array
# y = [m, t*n] Numpy array of zeros (placeholder)
for i in range(m):
    for j in range(n):
        y[i, t*j:t*(j+1)] = x[i, j]

More explanation: What I would like to do is to copy/broadcast a 2D array into a larger one that repeats the elements a t number of times in the second dimension. The above code works fine, though I would like to keep it efficient by avoiding the use of loops.

Upvotes: 2

Views: 1186

Answers (2)

gmds
gmds

Reputation: 19885

If I were to replicate your function using numpy, I would do this:

import numpy as np

a = np.arange(float(m * n)).reshape(m, n)

def expand_array(a, t):
    m, n = a.shape
    return np.repeat(a[:, -1], t * n).reshape(m, t * n)

def op_func(a, t):
    m, n = a.shape
    y = np.zeros((m, n * t))
    for i in range(m):
        for j in range(n):
            y[i, :] = x[i, j]

    return y

(expand_array(a, 10) == op_func(a, 10)).all()

Output:

True

However, as I noted in a comment, this effectively skips every column but the last. Therefore, I suspect you want something more like one of these two:

def repeat_expand_array(a, t):
    m, n = a.shape
    return np.repeat(a, t).reshape(m, t * n)

def tile_expand_array(a, t):
    m, n = a.shape
    return np.tile(a, t).reshape(m, t * n)

print(repeat_expand_array(a, 3), '\n')
print(tile_expand_array(a, 3))

Output:

[[0. 0. 0. 1. 1. 1.]
 [2. 2. 2. 3. 3. 3.]
 [4. 4. 4. 5. 5. 5.]] 

[[0. 1. 0. 1. 0. 1.]
 [2. 3. 2. 3. 2. 3.]
 [4. 5. 4. 5. 4. 5.]]

Upvotes: 0

pkapka
pkapka

Reputation: 5356

np.repeat(x,n,axis=1)

This will work without any zero array init! Assuming you just want to repeat the previous array in columns.

Upvotes: 1

Related Questions