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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:26:14+00:00 2026-06-05T18:26:14+00:00

How can I vectorize this loop, which populates two square submatrices of a larger

  • 0

How can I vectorize this loop, which populates two square submatrices of a larger matrix (also keeps larger matrix symmetric) using numpy arrays:

for x in range(n):
    assert m[x].shape == (n,)
    M[i:i+n,j+x] = m[x]
    M[j+x,i:i+n] = m[x]

This is tempting but does not agree with loop above (see example disagreement below):

assert m.shape == (n,n)
M[i:i+n,j:j+n] = m
M[j:j+n,i:i+n] = m

Here’s a little example (crashes for n>1):

from numpy import arange,empty,NAN
from numpy.testing import assert_almost_equal

for n in (1,2,3,4):
    # make the submatrix
    m = (10 * arange(1, 1 + n * n)).reshape(n, n)

    N = n # example below, submatrix is the whole thing

    # M1 using loops, M2 "vectorized"
    M1 = empty((N, N))
    M2 = empty((N, N))
    M1.fill(NAN)
    M2.fill(NAN)

    i,j = 0,0 # not really used when (n == N)

    # this results in symmetric matrix
    for x in range(n):
        assert m[x].shape == (n,)
        M1[i:i+n,j+x] = m[x]
        M1[j+x,i:i+n] = m[x]

    # this does not work as expected
    M2[i:i+n,j:j+n] = m
    M2[j:j+n,i:i+n] = m

    assert_almost_equal(M1,M1.transpose(),err_msg="M not symmetric?")
    print "M1\n",M1,"\nM2",M2
    assert_almost_equal(M1,M2,err_msg="M1 (loop) disagrees with M2 (vectorized)")

We end up with:

M1 = [10 30
      30 40] # symmetric

M2 = [10 20
      30 40] # i.e. m
  • 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-05T18:26:16+00:00Added an answer on June 5, 2026 at 6:26 pm

    Your test is incorrect:
    for i,j=0,0 your M2[]= assignments just overwrite the same matrix block.

    The reason you get the symmetric matrix when using M1 is because you assign
    the M1 values in a single loop.

    if you would split the loop into two:

    for x in range(n):
          M1[i:i+n,j+x] = m[x]
    for x in range(n): 
          M1[j+x,i:i+n] = m[x]
    

    The M1 will be obviously the same as M2.

    So Summarizing, the following code works (equivalent to your M2 calculation) but ! it will only work if there is no overlap between blocks above and below the diagonal. if there is you have to decide what to do there

    xs=np.arange(4).reshape(2,2)
    ys=np.zeros((7,7))
    ys[i:i+n,j:j+n]=xs
    ys[j:j+n,i:i+n]=xs.T
    print ys
    >> array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  1.,  0.,  0.],
           [ 0.,  0.,  0.,  2.,  3.,  0.,  0.],
           [ 0.,  0.,  2.,  0.,  0.,  0.,  0.],
           [ 0.,  1.,  3.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I vectorize the following double-loop? I have one N by A matrix
Can someone help me with this scenario? *There is a button, which when tapped,
Can you make this R code faster? Can't see how to vectorize it. I
I am currently using squeeze to remove two singleton dimensions from a matrix. The
The v4 series of the gcc compiler can automatically vectorize loops using the SIMD
Can someone please explain why this doesn't work? MyClass myClass1 = new MyClass(); object
Can anyone explain me what's going on? I use this method - (BOOL)shouldAutorotateToInterfaceOrientation:( UIInterfaceOrientation)interfaceOrientation
Can persisted column reference another persisted column and are there any rules to this
I've had this a few times, so here goes: I'm making some plots which
I have been having this problem for the last few days and I can't

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.