Skip to content
Advertisement

What is the more efficient way to create a pairwise 2D array for a 1D numpy array?

Given 2 numPy arrays of length N, I would like to create a pairwise 2D array (N x N) based on a custom function.

JavaScript

I would like to create an array C of size NxN. such that

JavaScript

Example:

JavaScript

I would like to create

JavaScript

I know I can do this by nested loop:

JavaScript

but looking for a neat and more efficient solution.

Advertisement

Answer

In numpy you can make a nice solution by using + to both add the integers and concatenate the characters.

JavaScript

Or as a single line:

JavaScript

Printing out each line in the above, gives:

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