How to perform element-wise custom function with two matrices of identical dimension


Haven't been able to find any information on this. If I have two m x n matrices of identical dimension, is there a way to apply an element-wise function in numpty on them? To illustrate my meaning:

Custom function is F(x,y)

First Matrix:

array([[ a, b], [ c, d], [ e, f]])

Second Matrix:

array([[ g, h], [ i, j], [ k, l]])

Is there a way to use the above two matrices in numpy to get the desired output below

array([[ F(a,g), F(b,h)], [ F(c,i), F(d,j)], [ F(e,k), F(f,l)]])

I know I could just do nested for statements, but I'm thinking there may be a cleaner way


For a general function F(x,y), you can do:

out = [F(x,y) for x,y in zip(arr1.ravel(), arr2.ravel())] out = np.array(out).reshape(arr1.shape)

However, if possible, I would recommend rewriting F(x,y) in such a way that it can be vectorized:

# non vectorized F def F(x,y): return math.sin(x) + math.sin(y) # vectorized F def Fv(x,y): return np.sin(x) + np.sin(y) # this would fail - need to go the route above out = F(arr1, arr2) # this would work out = Fv(arr1, arr2)

You can use numpy.vectorize function:

<pre class="lang-py prettyprint-override">import numpy as np a = np.array([[ 'a', 'b'], [ 'c', 'd'], [ 'e', 'f']]) b = np.array([[ 'g', 'h'], [ 'i', 'j'], [ 'k', 'l']]) def F(x,y): return x+y F_vectorized = np.vectorize(F) c = F_vectorized(a, b) print(c)


array([['ag', 'bh'], ['ci', 'dj'], ['ek', 'fl']], dtype='<U2')



  • tableView.tableHeaderView being set but not drawn
  • How to create as many nested loops as you want?
  • Alert when a particular value from dropdown is selected
  • JSLint unused variable error
  • why do serviceSubscriberCellularProviders return nil? (in iOS 12)
  • How to get internal link from latest revision of a wikipedia page?
  • Chromedriver works manually but fails when ran from Jenkins Slave
  • Setting up Wordpress and Flask on Apache2
  • sending emails in python weird behaviour
  • cq5 accessing osgi servlet through Url
  • AVPlayer blank video: playing sound but no video
  • IOS - Facebook SDK fbDidLogin not called — initialize view controllers.
  • Human readable URL Scheme for ASP.NET MVC
  • How do you place a variable inside a template tag's argument?
  • How to add CKEditor RTE to typo3 Backend Module with the API?
  • Shell script to execute nohup against an inputed filename
  • How to upload an Image onto localhost server whilst path is stored in mysql database?
  • Intersection of characters in two strings
  • how to change the black color to Red with opencv python
  • Preloading webView doesn't work - trying to reduce loading time
  • How to convert SVG to jVectorMap format
  • Storing the Cursor for App Engine Pagination
  • remove date from DateTimePicker for Compact Framework
  • Identifying dates in strings using NLTK
  • connect.cookieParser and connect.session
  • How to organize this layout with overflows?
  • characters not allowed in DOM ids by spec, and by browser
  • How to display converted time zones in a 'generic week' (Sunday thru Saturday)?
  • Create an Office365 mailbox from within C# Web API method
  • Visual Studio 2017 Professional- Unable to find package at source
  • Magento-Change Attribute of All Products
  • Request Access Token in Postman for Azure Function App protected by Azure AD B2C
  • Separating definition/instantiation of template classes without 'extern'
  • Another “Cannot make static reference…” Question
  • calling IO Operations from thread in ruby c extension will cause ruby to hang
  • media foundation H264 decoder not working properly