Skip to content
Advertisement

How to efficiently create multidimensional arrays?

assuming I have any function such as

f(x, y, z) = xyz

what’s the fastest way of calculating every value for f given three linear input arrays x, y, and z?

Of course I can do something along the lines of,

JavaScript

but this is probably not the best way to do it, especially if the input arrays become larger. Is there a better way of doing it, such as with a list comprehension (but multidimensional) or any other way? Help is appreciated.

Advertisement

Answer

First create the grid mesh array (x,y,z) as 3-tuples using meshgrid then you can apply the function in sequence:

JavaScript

A unit test for the f_values,

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