I want to create an $n \times n$ matrix (eg. $n=5$) using the following steps:

1) The entries aii (i = 1, ..., n) that form the main diagonal of a square matrix are 0 (aii=0); In C++, the element of matrix is generate if(i==j){M[i][j]=0}. In Mathematica:

s = SparseArray[{{i_, i_} -> 0},{n,n}]

2) Generates a random integer number in the interval [1,i-1] with uniform distribution. Using Mathematica:

z = RamdomInteger[{1,i-1}]

3) The off-diagonal elements are zero or 1 as follows:

a) {i_,j_}->{z,j} with probability 0.9 or {i_,j_}->1-{z,j} with probability 0.1

b) {j_,i_}->{j,z} with probability 0.9 or {j_,i_}->{j,z} with probability 0.1

In Mathematica:




    s = SparseArray[{z=RamdomInteger[{1,i-1}],{i_, i_} ->  0, 

There is error in language. How to solve?

  • $\begingroup$ You're not supposed to have underscores on the right hand side of a rule, for one. $\endgroup$ Aug 31, 2016 at 18:14
  • $\begingroup$ You have spelled RandomInteger as RamdomInteger with a 'm' instead of 'n' $\endgroup$ Aug 31, 2016 at 19:28
  • $\begingroup$ a SparseArray can not hold lists as elements. RandomChoice can not take lists as weights. It is really unclear what you want. $\endgroup$
    – george2079
    Sep 1, 2016 at 12:33

1 Answer 1


I cannot understand what you are trying to do (e.g., how is {z,j} supposed to stand for either 0 or 1, as you suggest?), but I believe a general answer to your general question is possible. The key is to separate out what you are trying to produce as values into a function of the indexes. Here is an example, which is intended to echo your problem in key ways (without matching it):

f[i_, j_] := With[{z := RandomInteger[i - 1]},
  If[i > j, Boole[z > j], 0]
s=SparseArray[{{i_, j_} /; i != j -> f[i, j]}, {n, n}]

Note that the default value is 0, so you don't need to place the zeros on the diagonal.

  • $\begingroup$ For example, I will build point-to-point until you reach a 3x3 matrix: $\endgroup$
    – SAC
    Sep 1, 2016 at 16:59
  • $\begingroup$ For i=1 (associate j=1) f[1][1]=0 $\endgroup$
    – SAC
    Sep 1, 2016 at 18:12
  • $\begingroup$ 2) For i=2 (2 values associated with j). Generates a random integer number in the interval [1,i-1]; z=RandomInteger[{1,i-1}]=1 f[i=2][j=1]=RandomChoice[{0.9,0.1}->{f[z=1][1],1-f[z=1][1]}] f[i=2][j=2]=0 f[j=1][i=2]=RandomChoice[{0.9,0.1}->{f[j=1][z=1],1-f[j=1][z=1]}] f[j=2][i=2]=0 $\endgroup$
    – SAC
    Sep 1, 2016 at 18:12
  • $\begingroup$ 3) For i=3, generates a random integer number in the interval z=RandomInteger[{1,i-1}]=(1 or 2). Suppose the draw is z=2 f[i=3][j=1]=RandomChoice[{0.9,0.1}->{f[z=2][j=1],1-f[z=2][j=1]}] f[i=3][j=2]=RandomChoice[{0.9,0.1}->{f[z=2][j=2],1-f[z=2][j=2]}] f[i=3][j=3]=0 f[j=1][i=3]=RandomChoice[{0.9,0.1}->{f[j=1][z=2],1-f[j=1][z=2]}] f[j=2][i=3]=RandomChoice[{0.9,0.1}->{f[j=2][z=2],1-f[j=2][z=2]}] f[j=3][i=3]=0 $\endgroup$
    – SAC
    Sep 1, 2016 at 18:13
  • 1
    $\begingroup$ you should edit the question with clarifying information. An example expected output might be useful as well (I'm still confused by the "off-diagonal elements are zero or 1" part ) $\endgroup$
    – george2079
    Sep 1, 2016 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.