This is an extension of the following question: How to generate binary array whose elements with values 1 are randomly drawn, where the most elegant answer was posted in the comments:

ArrayReshape[SparseArray[Transpose[{RandomSample[Range[400], 300]}] -> 1, {400}], {20, 20}] 

The above snippet of code produces a 20x20 matrix whose elements contain 300 1's and 100 0's. I would like to extend this procedure by applying some specific rules. Primarily, I want to leave the diagonal empty and I want to be able to fill specific regions (blocks) on the matrix with different probabilities. For example, in the first 10 rows and 10 columns (upper left block) I would like to have 50 of 100 elements to be 1, while in the final 10 rows and 10 columns (bottom right block) I would like 30 of 100 elements to be 1. Is there a way to modify the above code to do this? My first guess is to generate each block separately and combine them, but I am unsure if that is the nicest way to go about it. Thanks!

  • 2
    $\begingroup$ Is this for generating networks? Take a look at IGStochasticBlockModel in IGraph/M $\endgroup$
    – Szabolcs
    Commented Oct 8, 2019 at 11:18
  • $\begingroup$ @Szabolcs Hey! Yeah it is, I have just taken a look and this is right up my alley. Thank you so much for your suggestions :) I also took a look at your profile and saw that you are involved with a program for interfacing Mathematica and MATLAB - I was wondering if I could get in contact with you about it to ask a question or two? I was just having a problem with the interface between these two programs. $\endgroup$
    – Cameron F.
    Commented Oct 8, 2019 at 11:37
  • $\begingroup$ Yes, you can write me. $\endgroup$
    – Szabolcs
    Commented Oct 8, 2019 at 12:01


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.