Skip to content
Advertisement

How to create a numpy 2-D array whose elements depend on elements of two lists already fixed

Example:

Let

JavaScript

and I would like to create a matrix C of size (5,5) whose elements are

JavaScript

where f is a fixed function for example f(x,y) = x*y + x + y which means

JavaScript

In the case where c_ij depends on i and j only and does not depend on the lists A and B, we can use np.fromfunction(lambda i,j: f(i,j), (5,5)) but it’s not the case.

I would like to know how we can do that ?

Advertisement

Answer

Is this what you want:

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