1

Hoping this is an easy problem and I just don't know the correct syntax.

I currently have a small 3D volume that is defined by a numpy array of 100,100,100.

For the problem I am testing I want to put this volume into a larger array (doesn't matter how big right now but I am testing on a 1000,1000,100 array).

Currently I am just making an empty numpy array using the following:

BigArray = np.zeros((1000,1000,100),np.float16)

Then I have my smaller array that for the purpose of this example can just be a randomly filled array.:

SmallArray = np.random.rand(100,100,100)

From here I want to loop through and fill the 1000,1000,100 array with the 100,100,100 array placing each cube next to one another. The large array starts with '0' values so it should be as simple as just adding the small array to the correct coordinates of the larger array however have no idea the syntax to do this. Could someone help?

Thanks

2 Answers 2

1

This should do it -- just use a standard nested for loop and numpy array assignment syntax:

small = np.random.rand(100, 100, 100)
big = np.zeros((1000, 1000, 100), dtype=np.int16)

for i in range(0, 1000, 100):
    for j in range(0, 1000, 100):
        big[i:i+100, j:j+100, :] = small 

For generic sized 3D arrays:

def inset_into(small, big):

    sx, sy, sz = small.shape
    bx, by, bz = big.shape

    # make sure values work
    assert bx % sx == 0
    assert by % sy == 0
    assert bz == sz

    for i in range(0, bx, sx):
        for j in range(0, by, sy):
            big[i:i+sx, j:j+sy, :] = small

    return big
Sign up to request clarification or add additional context in comments.

5 Comments

Just for awareness this is 80 times slower than using numpy slicing. Use the tools that they give you.
@nanobennett Err - this uses numpy slicing
It says "a" small cube into "a" large cube. That's one array of each ...then you just place one into the other. I don't think that's possible to misread. The word "a" does not indicate more than one. You're using slicing but you're running through a for loop for no reason whatsoever.
@NanoBennett "I want to loop through and fill the 1000,1000,100 array with the 100,100,100 array placing each cube next to one another" - they also accepted my answer.
Nevermind, I see what you're saying now. It's essentially 3D tiles. I was confused by the wording. Upvoted.
0

This should just be numpy slicing.

small = np.random.rand(100, 100, 100)
big = np.zeros((1000, 1000, 100), dtype=np.int16)

If you want to make big out of a bunch of smalls here is another way.

big = np.concatenate([small] * (big.shape[0] // small.shape[0]), axis=1)
big = np.concatenate([big] * (big.shape[1] // small.shape[1]), axis=0)

There is a speed difference. Looping is better.

2 Comments

I think you misread the question. OP is asking how you fill an bigger array with repeated smaller arrays - not just inserting it once.
There we go. Now it's a valid other approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.