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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:22:33+00:00 2026-05-24T22:22:33+00:00

Is there a numpy function to divide an array along an axis with elements

  • 0

Is there a numpy function to divide an array along an axis with elements from another array? For example, suppose I have an array a with shape (l,m,n) and an array b with shape (m,); I’m looking for something equivalent to:

def divide_along_axis(a,b,axis=None):
    if axis is None:
        return a/b
    c = a.copy()
    for i, x in enumerate(c.swapaxes(0,axis)):
        x /= b[i]
    return c

For example, this is useful when normalizing an array of vectors:

>>> a = np.random.randn(4,3)
array([[ 1.03116167, -0.60862215, -0.29191449],
       [-1.27040355,  1.9943905 ,  1.13515384],
       [-0.47916874,  0.05495749, -0.58450632],
       [ 2.08792161, -1.35591814, -0.9900364 ]])
>>> np.apply_along_axis(np.linalg.norm,1,a)
array([ 1.23244853,  2.62299312,  0.75780647,  2.67919815])
>>> c = divide_along_axis(a,np.apply_along_axis(np.linalg.norm,1,a),0)
>>> np.apply_along_axis(np.linalg.norm,1,c)
array([ 1.,  1.,  1.,  1.])
  • 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-24T22:22:33+00:00Added an answer on May 24, 2026 at 10:22 pm

    For the specific example you’ve given: dividing an (l,m,n) array by (m,) you can use np.newaxis:

    a = np.arange(1,61, dtype=float).reshape((3,4,5)) # Create a 3d array 
    a.shape                                           # (3,4,5)
    
    b = np.array([1.0, 2.0, 3.0, 4.0])                # Create a 1-d array
    b.shape                                           # (4,)
    
    a / b                                             # Gives a ValueError
    
    a / b[:, np.newaxis]                              # The result you want 
    

    You can read all about the broadcasting rules here. You can also use newaxis more than once if required. (e.g. to divide a shape (3,4,5,6) array by a shape (3,5) array).

    From my understanding of the docs, using newaxis + broadcasting avoids also any unecessary array copying.

    Indexing, newaxis etc are described more fully here now. (Documentation reorganised since this answer first posted).

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

Sidebar

Related Questions

I have a function foo that takes a NxM numpy array as an argument
Is there way to initialize a numpy array of a shape and add to
Say I have an array in NumPy containing evaluations of a continuous differentiable function,
I have a function gen() which returns a numpy array of nElements number of
Folks, is there a collection of gotchas where Numpy differs from python, points that
Suppose that I have two numpy arrays of the form x = [[1,2] [2,4]
In numpy there is a function that makes arrays print prettier. set_printoptions(suppress = True)
Is there a function in numpy that determines whether strings should be integers or
I made a function similar to numpy's array . It converts lists to arrays,
Is there a FFT-based 2D cross-correlation or convolution function built into scipy (or another

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.