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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:58:51+00:00 2026-06-04T23:58:51+00:00

I am trying to generate symmetric matrices in numpy. Specifically, these matrices are to

  • 0

I am trying to generate symmetric matrices in numpy. Specifically, these matrices are to have random places entries, and in each entry the contents can be random. Along the main diagonal we are not concerned with what entries are in there, so I have randomized those as well.

The approach I have taken is to first generate a nxn all zero matrix and simply loop over the indices of the matrices.
How can I do this more efficiently using numpy?

import numpy as np
import random

def empty(x, y):
    return x*0

b = np.fromfunction(empty, (n, n), dtype = int)

for i in range(0, n):
    for j in range(0, n):
        if i == j:
            b[i][j] = random.randrange(-2000, 2000)
        else:
            switch = random.random()
            random.seed()
            if switch > random.random():
                a = random.randrange(-2000, 2000)
                b[i][j] = a
                b[j][i] = a
            else:
                b[i][j] = 0
                b[j][i] = 0
  • 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-04T23:58:52+00:00Added an answer on June 4, 2026 at 11:58 pm

    You could just do something like:

    import numpy as np
    
    N = 100
    b = np.random.random_integers(-2000,2000,size=(N,N))
    b_symm = (b + b.T)/2
    

    Where you can choose from whatever distribution you want in the np.random or equivalent scipy module.

    Update: If you are trying to build graph-like structures, definitely check out the networkx package:

    http://networkx.lanl.gov

    which has a number of built-in routines to build graphs:

    http://networkx.lanl.gov/reference/generators.html

    Also if you want to add some number of randomly placed zeros, you can always generate a random set of indices and replace the values with zero.

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

Sidebar

Related Questions

I'm trying to generate a SQL query that can join two tables together and
I'm trying to generate a report base on pre defined dates(cutoff dates). I have
I'm trying to generate a sequence table in MySQL, so that I can get
Im trying to generate n unique random numbers between 1 and max I tried
I am trying to generate a different random number every time my RandomNumber method
I'm trying to generate a random Gaussian double in Objective-C (the same as random.nextGaussian
I am trying to generate a random password in php. However I am getting
I'm trying to generate a linear regression on a scatter plot I have generated,
I am trying to generate random numbers to be printed on the console. I
I'm trying to generate semi-random subsets with some restrictions. Here are the variable descriptions

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.