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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:02:32+00:00 2026-06-15T10:02:32+00:00

I am a recent immigrant to the world of Python. I need to figure

  • 0

I am a recent immigrant to the world of Python. I need to figure out how to copy a matrix to a larger matrix in Python. Let me illustrate this with an example in Matlab:

A = randn(4,4);
B = eye(2,2);
A(1:2,1:2) = B

gives

A = 

 1.0000          0     3.5784     0.7254
      0     1.0000     2.7694   - 0.0631
-2.2588   - 0.4336   - 1.3499     0.7147
 0.8622     0.3426     3.0349   - 0.2050  

I am trying a similar thing with Python using NumPy in the following fashion.

A = np.random.randn(4,4)
B = np.eye(2,2)
A[0:1,0:1] = B

gives

ValueError: output operand requires a reduction, but reduction is not enabled.

Of course, the simplest way to avoid this is to use a loop but I would want to keep it vectorized.

Can someone please point me to a way of doing this without using for loops?

  • 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-15T10:02:33+00:00Added an answer on June 15, 2026 at 10:02 am

    Here’s the problem:

    A[0:1,0:1] = B
    

    You want:

    A[0:2,0:2] = B
    

    Why? Because Python uses half-open ranges. So the slice [0:1] is the half-open range [0, 1), meaning just the index 0; the slice [0:2] is the half-open range [0, 2), meaning the indices 0 and 1.

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

Sidebar

Related Questions

The most recent successful query I ran was this one: let $doc := doc('test')
A recent task I have to figure out, gives me some hard time thinking
In recent versions of python, one can use something like with open('abc.txt') as f:
A recent problem* left me wondering whether there is a text editor out there
Recent conversations with colleagues have produced varying points of view on this matter. What
A recent mention of PostSharp reminded me of this: Last year where I worked,
A recent crash prompted this question - I had two gui windows open. Is
The recent ATL security update updated the C++ runtimes to version 8.0.50727.4053. Unfortunately, this
A recent question got me wondering about explicit copy constructors. Here is a sample
This recent post, How to use Evolutions in Play Framework 2.0? , says that

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.