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 6571615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:54:19+00:00 2026-05-25T14:54:19+00:00

[EDIT:I sort of brush this example up so I didn’t clean up my code

  • 0

[EDIT:I sort of brush this example up so I didn’t clean up my code very well. My question is more on, how do I pass a subarray into a numpy.vectorize-d function, not specifically about this example.]

I can’t figure out how to use numpy.vectorize or numpy.frompyfunc to vectorize commands that takes an array as an argument.

Let’s think of this easy example (I understand this is a very basic example and I don’t have to use numpy.vectorize at all. I am just asking for an example):

aa = [[1,2,3,4], [2,3,4,5], [5,6,7,8], [9,10,11,12]]
bb = [[100,200,300,400], [100,200,300,400], [100,200,300,400], [100,200,300,400]]

And I want to vectorize a function that adds up the second element of each subarray of aa and bb. In this example I want to return an array of [202 203 206 210]

But a code like this doesnt work:

def vec2(bsub, asub):
    return bsub[1] + asub[1]

func2 = np.vectorize(vec2)
func2( bb, aa )

Similar thing with numpy.frompyfunc has no luck.

My question is, how do I past a list of subarrays into a numpy.vectorize-d function and let each subarray be the argument of 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-05-25T14:54:20+00:00Added an answer on May 25, 2026 at 2:54 pm

    One of your problems is that aa and bb are lists, not numpy.array(). You should be doing:

    aa = np.array([[1,2,3,4], [2,3,4,5], [5,6,7,8], [9,10,11,12]])
    bb = np.array([[100,200,300,400], [100,200,300,400], [100,200,300,400], [100,200,300,400]])
    

    The second thing I notice is that to get the second element of each subarray, you need aa[:,1], not aa[2].

    Third, your vec2 function should return something, not just print.

    The final issue is that your vec2 function should operate on integers, not arrays, and you should pass the slices to the function, not the complete arrays. The corrected version (which returns the expected output) is then:

    import numpy as np
    aa = np.array([[1,2,3,4], [2,3,4,5], [5,6,7,8], [9,10,11,12]])
    bb = np.array([[100,200,300,400], [100,200,300,400], [100,200,300,400], [100,200,300,400]])
    
    def vec2(a, b):
        return a + b
    
    func2 = np.vectorize(vec2)
    print func2(bb[:,1], aa[:,1])
    

    Note EDITS on OP’s post which make this answer seem a bit odd.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: This question is more about language engineering than C++ itself. I used C++
EDIT:Well I guess I should asked then before this question, would it be better
Edit: This question was written in 2008, which was like 3 internet ages ago.
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
Edit: This was accidentally posted twice. Original: VB.NET Importing Classes I've seen some code
Total F# n00b question. How do I sort a LIST data structure? Edit: Sorry,
[EDIT] original title of this question was Getting the last element of a Postgres
EDIT: Solved this myself - obviously won't work as sorting the dataTable doesn't sort
EDIT The dictionary is the offender here, the answer marked on this question works,
How can I sort an IEnumerable<string> alphabetically. Is this possible? Edit: How would I

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.