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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:38:34+00:00 2026-06-02T03:38:34+00:00

In python, I wish to subtract line by line a 2-dim array from a

  • 0

In python, I wish to subtract line by line a 2-dim array from a 1-dim array.

I know how to do it with a ‘for’ loop and indexes but I suppose it may be quicker to use numpy functions. However I did not find a way to do it. Here is an example with a ‘for’ loop :

from numpy import *
x=array([[1,2,3,4,5],[6,7,8,9,10]])
y=array([20,10])
j=array([0, 1])
a=zeros([2,5])
for i in j :
...     a[i]=y[i]-x[i]

And here is an example of something that does not work, replacing the ‘for’ loop by this:

a=y[j]-x[j,i]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: shape mismatch: objects cannot be broadcast to a single shape

Dou you have suggestions ?

  • 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-02T03:38:35+00:00Added an answer on June 2, 2026 at 3:38 am

    The problem is that y-x have the respective shapes (2) (2,5). To do proper broadcasting, you’ll need shapes (2,1) (2,5). We can do this with .reshape as long as the number of elements are preserved:

    y.reshape(2,1) - x
    

    Gives:

    array([[19, 18, 17, 16, 15],
       [ 4,  3,  2,  1,  0]])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning C# coming from python and wish to know how the C# garbage
I know i could have used python's own functional tool-set, but I wish there's
I'm writing an application in Python where I wish to use sqlite as the
I wish to call sed from python using subprocess. The script I tried using
I wish to a run a python script which takes options and arguments from
I have written python code which involves the use of dictionaries. I now wish
I looked over Python Docs (I may have misunderstood), but I didn't see that
Python does not print traceback messages from exceptions raised in daemon threads. For example,
Python's getattr() method is useful when you don't know the name of a certain
Python is the language I know the most, and strangely I still don't know

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.