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

  • Home
  • SEARCH
  • 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 8303833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:38:08+00:00 2026-06-08T17:38:08+00:00

I want to broadcast an array b to the shape it would take if

  • 0

I want to broadcast an array b to the shape it would take if it were in an arithmetic operation with another array a.

For example, if a.shape = (3,3) and b was a scalar, I want to get an array whose shape is (3,3) and is filled with the scalar.

One way to do this is like this:

>>> import numpy as np
>>> a = np.arange(9).reshape((3,3))
>>> b = 1 + a*0
>>> b
array([[1, 1, 1],
       [1, 1, 1],
       [1, 1, 1]])

Although this works practically, I can’t help but feel it looks a bit weird, and wouldn’t be obvious to someone else looking at the code what I was trying to do.

Is there any more elegant way to do this? I’ve looked at the documentation for np.broadcast, but it’s orders of magnitude slower.

In [1]: a = np.arange(10000).reshape((100,100))

In [2]: %timeit 1 + a*0
10000 loops, best of 3: 31.9 us per loop

In [3]: %timeit bc = np.broadcast(a,1);np.fromiter((v for u, v in bc),float).reshape(bc.shape)
100 loops, best of 3: 5.2 ms per loop

In [4]: 5.2e-3/32e-6
Out[4]: 162.5
  • 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-08T17:38:09+00:00Added an answer on June 8, 2026 at 5:38 pm

    If you just want to fill an array with a scalar, fill is probably the best choice. But it sounds like you want something more generalized. Rather than using broadcast you can use broadcast_arrays to get the result that (I think) you want.

    >>> a = numpy.arange(9).reshape(3, 3)
    >>> numpy.broadcast_arrays(a, 1)[1]
    array([[1, 1, 1],
           [1, 1, 1],
           [1, 1, 1]])
    

    This generalizes to any two broadcastable shapes:

    >>> numpy.broadcast_arrays(a, [1, 2, 3])[1]
    array([[1, 2, 3],
           [1, 2, 3],
           [1, 2, 3]])
    

    It’s not quite as fast as your ufunc-based method, but it’s still on the same order of magnitude:

    >>> %timeit 1 + a * 0
    10000 loops, best of 3: 23.2 us per loop
    >>> %timeit numpy.broadcast_arrays(a, 1)[1]
    10000 loops, best of 3: 52.3 us per loop
    

    But scalars, fill is still the clear front-runner:

    >>> %timeit b = numpy.empty_like(a, dtype='i8'); b.fill(1)
    100000 loops, best of 3: 6.59 us per loop
    

    Finally, further testing shows that the fastest approach — in at least some cases — is to multiply by ones:

    >>> %timeit numpy.broadcast_arrays(a, numpy.arange(100))[1]
    10000 loops, best of 3: 53.4 us per loop
    >>> %timeit (1 + a * 0) * numpy.arange(100)
    10000 loops, best of 3: 45.9 us per loop
    >>> %timeit b = numpy.ones_like(a, dtype='i8'); b * numpy.arange(100)
    10000 loops, best of 3: 28.9 us per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get broadcast(or any way) when any application launch from home screen.
Can I access any database in the broadcast receiver? I want to get contact
I want to broadcast messages locally to many application. For that I thought UDP
What I want is to broadcast an android camera video to remote locations, for
I want to calculate the broadcast of an IP address, I calculated the network
I want to make an app that can receive broadcast when other apps on
I have a broadcast receiver which receive an internet state changes. I want to
Want to run javascript function from parent window in child window Example I have
I want to broadcast a UDP message in my LAN with fake source IP
I want to send broadcast message to all peers in my local network. Message

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.