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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:25:56+00:00 2026-06-09T08:25:56+00:00

I have written a code in MATLAB that allows me to generate a random

  • 0

I have written a code in MATLAB that allows me to generate a random graph of n vertices, each with c fixed neighbours without loops (note the edges are directed, thus “a connected to b” does not imply “b connected to a”).

However, it is terribly inefficient, especially when I need to it work on magnitudes such as n = 10000 and c = 1000. I was wondering if anyone could optimize it big time, or suggest anything constructive?

function [M]=matsrand(n,c)


MM=0;   %arbitrary starting value
while MM ~=n*c

    M = sparse(zeros(n));       
    ctin = zeros(1,n);  


    for i=1:n
        rp = randperm(n);   %generate vector of the randomly permuted order of n vertices
        rp(rp==i)=[];       %get rid of itself to avoid self connection

        noconnect=find(ctin(:)>=c); %generate list that i is not allowed to connect to
        where=ismember(rp,noconnect);   %returns 1 to the subset noconnect in rp
        noconnectind=find(where);

        rp(noconnectind(:))=[];         %remove the neurons i is not allowed to connect to

        if length(rp)<c
            break
        else
            r=rp(1:c);
        end
        M(i,r)=1;
        ctin(r)=ctin(r)+1;

    end
    MM=sum(ctin);
end
  • 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-09T08:25:58+00:00Added an answer on June 9, 2026 at 8:25 am

    This’ll speed up things a bit:

    function [M]=matsrand(n,c)
    
        MM=0;   %arbitrary starting value
        all_nums=1:n;
    
        while MM ~=n*c
    
            M = sparse([],[],[],n,n,n*c);
            ctin = zeros(1,n);
    
            for ii=1:n
                noconnect=ctin>=c;
                noconnect(ii)=true;
    
                rem_nums = all_nums(~noconnect); % remaining numbers
                rp=randperm(n-sum(noconnect));
                rp = rem_nums(rp); % remaining numbers, hussled
    
                if numel(rp)<c
                    break
                else
                    r=rp(1:c);
                end
                M(ii,r)=1;
                ctin(r)=ctin(r)+1;
            end
            MM=sum(ctin);
        end
    end
    

    If memory isn’t an issue, I think you can replace the sparse matrix with an ordinary zeros(n,n).

    Main problem still is you have to hit that lucky combination.

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

Sidebar

Related Questions

I have written some matlab code for image analysis that searches for clusters in
I'm trying to rewrite code I've written in matlab in C++ instead. I have
I have written code that opens 16 figures at once. Currently, they all open
I have written code to perform a function that could take a while to
I have written some code that loads an XML document using an XmlDocument object
I have written code for an animation that shakes a UIImageView on the screen,
I have written code to perform click event on JMenuItem but that is not
I have written code as follows. Problem is that I can't remove Event.COMPLETE event
I have this line of code in MATLAB, written by someone else: c=a.'/b I
I have written the following code in MATLAB to process large images of the

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.