I am trying to do a function iteratively to an array, and make a matrix composed of what it returns. If this was native python, what I would do is:
[func(x, y) for y in xrange(Y)]
but if I do that, I need to wrap it with numpy.matrix() to vectorize it. What is the numpy way of doing this? Right now I am initializing a zeros matrix and then populating it with elements I get from a for loop, but that seems inefficient.
Take a look at the numpy tutorial, especially the part about Universal Functions or ufuncs. A ufunc is:
which sounds like what you’re asking for. Keep in mind that you probably don’t need to write your own ufunc, but just write
funcin terms of existing ufuncs. For example: