Skip to content
Advertisement

How to length the last dimension of a numpy array and fill it up with another array?

I have a numpy array of shape (5, 4, 3) and another numpy array of shape (4,) and what I want to do is expand the last dimension of the first array (5, 4, 3) -> (5, 4, 4) and then broadcast the other array with shape (4,) such that it fills up the new array cells respectively.

Example:

JavaScript

becomes

JavaScript

And then I have another array

JavaScript

which I somehow broadcast with the first one to fill the zeros:

JavaScript

How can I accomplish this?

Advertisement

Answer

You can use numpy.c_ and numpy.tile:

JavaScript

output:

JavaScript

How it works:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement