Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8890977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:37:23+00:00 2026-06-14T22:37:23+00:00

I have two input arrays x and y of the same shape. I need

  • 0

I have two input arrays x and y of the same shape. I need to run each of their elements with matching indices through a function, then store the result at those indices in a third array z. What is the most pythonic way to accomplish this? Right now I have four four loops – I’m sure there is an easier way.

x = [[2, 2, 2],
     [2, 2, 2],
     [2, 2, 2]]

y = [[3, 3, 3],
     [3, 3, 3],
     [3, 3, 1]]

def elementwise_function(element_1,element_2):
    return (element_1 + element_2)

z = [[5, 5, 5],
     [5, 5, 5],
     [5, 5, 3]]

I am getting confused since my function will only work on individual data pairs. I can’t simply pass the x and y arrays to the function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:37:24+00:00Added an answer on June 14, 2026 at 10:37 pm

    One “easier way” is to create a NumPy-aware function using numpy.vectorize. A “ufunc” is NumPy terminology for an elementwise function (see documentation here). Using numpy.vectorize lets you use your element-by-element function to create your own ufunc, which works the same way as other NumPy ufuncs (like standard addition, etc.): the ufunc will accept arrays and it will apply your function to each pair of elements, it will do array shape broadcasting just like standard NumPy functions, etc. The documentation page has some usage examples that might be helpful.

    In [1]: import numpy as np
       ...: def myfunc(a, b):
       ...:     "Return 1 if a>b, otherwise return 0"
       ...:     if a > b:
       ...:         return 1
       ...:     else:
       ...:         return 0
       ...: vfunc = np.vectorize(myfunc)
       ...: 
    
    In [2]: vfunc([1, 2, 3, 4], [4, 3, 2, 1])
       ...: 
    Out[2]: array([0, 0, 1, 1])
    In [3]: vfunc([1, 2, 3, 4], 2)
       ...: 
    Out[3]: array([0, 0, 1, 1])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays and I post the arrays in one input; <input name='sistem[]'
I have two input fields, and without changing their names (i.e., I have to
I want to have two input with one button for user to enter their
I have a function with two input variables min.depth<-2 max.depth<-5 the function produces a
I have two textboxes <input id='random_value' name='random_name' value='First' <input id='random_value' name='random_name' value='' I need
I have input as two arrays shown below NSArray *array1=[[NSArray alloc]initWithObjects:@1,@2,@3, nil]; NSArray *array2=[[NSArray
I have multiple function objects declared from the same function that deal with their
I have two integer arrays created at runtime (size depends on the program input).
I have this function which i am using to compare two input fields. If
I have two input text boxes which are decimal. The sum of the two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.