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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:39:23+00:00 2026-06-17T06:39:23+00:00

I have a 1d array of values i = np.arange(0,7,1) and a function #

  • 0

I have a 1d array of values

i = np.arange(0,7,1)

and a function

# Returns a column matrix
def fn(i):
    return np.matrix([[i*2,i*3]]).T


fnv = np.vectorize(fn) 

then writing

fnv(i)

gives me an error

  File "<stdin>", line 1, in <module>
  File "c:\Python33\lib\site-packages\numpy\lib\function_base.py", 
        line 1872, in __call__
    return self._vectorize_call(func=func, args=vargs)
  File "c:\Python33\lib\site-packages\numpy\lib\function_base.py", 
        line 1942, in _vectorize_call
        copy=False, subok=True, dtype=otypes[0])
  ValueError: setting an array element with a sequence.

The result I am looking for is a matrix with two rows and as many columns as in the input array. What is the best notation in numpy to achieve this?

For example i would equal

[1,2,3,4,5,6]

and the output would equal

[[2,4,6,8,10,12],
 [3,6,9,12,15,18]]
  • 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-17T06:39:24+00:00Added an answer on June 17, 2026 at 6:39 am

    EDIT
    You should try to avoid using vectorize, because it gives the illusion of numpy efficiency, but inside it’s all python loops.

    If you really have to deal with user supplied functions that take ints and return a matrix of shape (2, 1) then there probably isn’t much you can do. But that seems like a really weird use case. If you can replace that with a list of functions that take an int and return an int, and that use ufuncs when needed, i.e. np.sin instead of math.sin, you can do the following

    def vectorize2(funcs) :
        def fnv(arr) :
            return np.vstack([f(arr) for f in funcs])
        return fnv
    
    f2 = vectorize2((lambda x : 2 * x, lambda x : 3 * x))
    
    >>> f2(np.arange(10))
    array([[ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18],
           [ 0,  3,  6,  9, 12, 15, 18, 21, 24, 27]])
    

    Just for your reference, I have timed this vectorization against your proposed one:

    f = vectorize(fn)
    
    
    >>> timeit.timeit('f(np.arange(10))', 'from __main__ import np, f', number=1000)
    0.28073329263679625
    >>> timeit.timeit('f2(np.arange(10))', 'from __main__ import np, f2', number=1000)
    0.023139129945661807
    
    
    >>> timeit.timeit('f(np.arange(10000))', 'from __main__ import np, f', number=10)
    2.3620706288432984
    >>> timeit.timeit('f2(np.arange(10000))', 'from __main__ import np, f2', number=10)
    0.002757072593169596
    

    So there is an order of magnitude in speed even for small arrays, that grows to a x1000 speed up, available almost for free, for larger arrays.

    ORIGINAL ANSWER

    Don’t use vectorize unless there is no way around it, it’s slow. See the following examples

    >>> a = np.array(range(7))
    >>> a
    array([0, 1, 2, 3, 4, 5, 6])
    >>> np.vstack((a, a+1))
    array([[0, 1, 2, 3, 4, 5, 6],
           [1, 2, 3, 4, 5, 6, 7]])
    >>> np.vstack((a, a**2))
    array([[ 0,  1,  2,  3,  4,  5,  6],
           [ 0,  1,  4,  9, 16, 25, 36]])
    

    Whatever your function is, if it can be constructed with numpy’s ufuncs, you can do something like np.vstack((a, f(a))) and get what you want

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

Sidebar

Related Questions

Suppose I have an array of values [a,b,c,d,...] and a function f(x,...) which returns
I have an array of values, I was wondering how I can parse certain
I have an array of values which is almost, but not quite sorted, with
I have an array of values all well within the range 0 - 63,
Let's say I have an array var test = new Array() the values in
I have classname stored in variable $classname; also I have an array of values
I have an array with allowed values and an array with given values. Howto
I just have the array of 15 values that all need to be inserted
Ruby 1.8.6 I have an array containing numerical values. I want to reduce it
I'm sorry about the naive question. I have the following: public Array Values {

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.