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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:03:21+00:00 2026-06-02T05:03:21+00:00

I am a newby in broadcasting with numpy. I define three numpy arrays as

  • 0

I am a newby in broadcasting with numpy. I define three numpy arrays as follows:

from numpy import *
a=array([10,20]).reshape(2,1)
b=array([100,200,300]).reshape(1,3)
c=arange(1,11).reshape(1,1,10)

a+b is a (2,1) vs (1,3) sum so it is supposed to be broadcastable (2vs1 in dim 1, 1vs3 in dim 2, broadcast rule is fulfiled). Indeed it is:

>>> a+b
array([[110, 210, 310],
       [120, 220, 320]])

a+c is a (2,1) vs (1,1,10) sum so it is supposed to be broadcastable (2vs1 in dim 1, 1vs1 in dim 2 and 1vs10 in dim 3, broadcast rule is fulfiled). Indeed it is:

>>> a+c
array([[[11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
        [21, 22, 23, 24, 25, 26, 27, 28, 29, 30]]])

b+c is a (1,3) vs (1,1,10) sum so it is supposed to be broadcastable (1vs1 in dim 1, 3vs1 in dim 2, 1vs10 in dim 3. But it seems it is not:

>>> b+c
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: shape mismatch: objects cannot be broadcast to a single shape

The explanation is certainely obvious … but please help me !

  • 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-02T05:03:23+00:00Added an answer on June 2, 2026 at 5:03 am
    b[:,:,None] + c
    

    returns a (1, 3, 10) array. You have to define the missing axis (the third one).

    You can also use

    b[:,:,newaxis] + c
    

    since you imported * from numpy, which is generally not a good idea.

    import numpy as np is better. This way you will always know where the methods come from (if you import more packages):

    import numpy as np
    a = np.array([10,20]).reshape(2,1)
    b = np.array([100,200,300]).reshape(1,3)
    c = np.arange(1,11).reshape(1,1,10)
    
    print a + b
    print a + c
    print b[:,:,np.newaxis] + c
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

BeautifulSoup newbe... Need help Here is the code sample... from mechanize import Browser from
Weird error. I'm a newby to rails. From a new install of rails I
Being a newby in .NET land and coming from a Delphi background, I am
I'm a grails newby. I am using grails 2.0.3 from the command line (no
I' still newby to this so I'll try to explain what I'm doing. Basically
Additional thanks extend to Daniel Newby for answering my memory usage question (and Martin
Using Ruby (newb) and Regex, I'm trying to parse the street number from the
Newby to php. When I do an include(<filename>) are the contents of <filename> literally
I'm a newby to Stack Overflow so please go easy on me! I'm reading
It is of a newby question, so sorry in advance. I'm using fluent NHibernate

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.