Python (numpy) question:(adsbygoogle = window.adsbygoogle || []).push({});

I have two 2D numpy arrays, A[i,j] and B[k,l], but the indexes are unrelated to each other (A and B won't even have the same dimensions in general). I want to be able to add/multiply these two together to get a 4D matrix: C[i,j,k,l] = A[i,j] + B[k,l] or C[i,j,k,l] = A[i,j]*B[k,l].

Obviously I can do this by looping through all four indices and running the statement "C[i,j,k,l] = A[i,j] + B[k,l]" or "C[i,j,k,l] = A[i,j]*B[k,l]" at each iteration of the loop, but is there an elegant way of doing this using a numpy function rather than loops? Some sort of addition function that adds an N-dimensional array to an M-dimensional array to give an (N+M)-dimensional array of all possible pairwise additions of elements? My loops are taking a while to run, and I'm hoping that using a "proper" numpy array manipulation function rather than loops would speed things up (and clean up my code a bit).

**Physics Forums - The Fusion of Science and Community**

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Python (Numpy) Non-Elementwise Array Operations

Have something to add?

Draft saved
Draft deleted

Loading...

Similar Threads - Python Numpy Elementwise | Date |
---|---|

Python How can I run Python by getting values from Matlab | Mar 7, 2018 |

Python Interesting article how to use Pandas with Excel sheets | Feb 19, 2018 |

Porting Python Code to Fortran for Parallel Computing | Dec 15, 2017 |

Importing a matrix into Numpy | Oct 3, 2017 |

[Python] Importing csv files and creating numpy arrays | Apr 1, 2015 |

**Physics Forums - The Fusion of Science and Community**