1

I'm trying to generate a list of random NxN matrics with all diagonal elements zero. I use the command

RM[n_, p_] := Table[RandomChoice[{p, 1 - p} -> {RandomReal[{-1, 1}], 0}], {j, 1, n}, {k, 1, n}]
N0 = Table[RM[50, 0.1], 100];

to generate the matrics and then write

For[i = 1, i <= 2, i++, N2 = N0[[i]] - Diagonal[N0[[i]]] // MatrixForm]

to make all parts in N0 have zeros on their diagonal. However, it doesn't work since Mathematica said the parts in N2 (except part1) do not exist.

mikado
  • 16,741
  • 2
  • 20
  • 54
  • It depends very much from which distribution you're pulling the matrices. – Roman Nov 18 '22 at 18:41
  • Hi there, the probability density is uniform. But I am still confused about how to generate it. I can find a single matrix with all zeros on the diagonal but cannot do it with a list, say a sample that contains 100 matrics. – Carolyn Lan Nov 18 '22 at 18:57
  • How about N0 = Table[# - DiagonalMatrix@Diagonal@# &@RM[50, 0.1], 100];? This will subtract off the diagonal elements on the fly. – march Nov 18 '22 at 19:24
  • reply to March. Thanks for help. I tried the command and use diagonal[] to verify if it worked, but several numbers are remaining on the diagonal. – Carolyn Lan Nov 18 '22 at 20:21
  • Just zero them after the fact. `In[45]:= RM[n_, p_] := Table[RandomChoice[{p, 1 - p} -> {RandomReal[{-1, 1}], 0.}], {j, 1, n}, {k, 1, n}] nn = 3; N0 = Table[RM[nn, 0.8], 10]; Do[N0[[All, j, j]] = 0., {j, nn}]; N0[[1 ;; 2]]

    Out[49]= {{{0., 0.863311, 0.768276}, {0.579728, 0., 0.}, {0.128724, 0.711393, 0.}}, {{0., -0.0571237, 0.658995}, {0., 0., -0.896609}, {0.437745, -0.626474, 0.}}}` Note I use 0. to allow these to be packed arrays (improves size/speed in further usage).

    – Daniel Lichtblau Nov 19 '22 at 17:42

3 Answers3

3

Using SparseArray

$Version

(* "13.1.0 for Mac OS X x86 (64-bit) (June 16, 2022)" *)

Clear["Global`*"]

dim = 4; nmat = 5;

SeedRandom[1234];

EDIT:

RM[n_Integer?(# > 1&), p_?(0 <= # <= 1 &)] :=
 SparseArray[{{i_, j_} /; j != i :>
    RandomChoice[{p, 1 - p} -> {RandomReal[{-1, 1}], 0}]},
  {n, n}]

MatrixForm /@ (N0 = Table[RM[dim, 0.7], nmat])

enter image description here

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
  • Thanks for your help! But there is another restriction that matrix-elements are 0 with probability 1-p, and with probability p are randomly drawn real numbers between 1 and -1. So only using RandomReal might not be enough? – Carolyn Lan Nov 18 '22 at 19:56
  • Corrected randomizer – Bob Hanlon Nov 18 '22 at 20:34
2

If dim is the dimension and nmat the number of matrices in the list:

dim = 2;
nmat = 3
res = Table[t = RandomReal[{-1, 1}, {dim, dim}]; t - Diagonal[t], {nmat}] ;
MatrixForm /@ res

enter image description here

Daniel Huber
  • 51,463
  • 1
  • 23
  • 57
1

Here is a way to generate an n x n matrix with uniform random elements and a zero diagonal:

neb[z_] := ReplacePart[RandomVariate[UniformDistribution[1], {z, z}], {i_, i_} :> 0]

MatrixForm[neb[5]]

0 {0.927086} {0.632727} {0.794216} {0.111677}

{0.187628} 0 {0.859068} {0.496428} {0.901312}

{0.372348} {0.586887} 0 {0.686587} {0.83178}

{0.636274} {0.286791} {0.406237} 0 {0.167102}

{0.00708581} {0.89337} {0.126658} {0.108218} 0

Lexington1776
  • 619
  • 1
  • 6
  • Unfortunately, you have a slight error. UniformDistribution[1] doesn't do what you guessed it does . UniformDistribution[] does what you need. (UniformDistribution[1] generates a 1D list, rather than a single value). – mikado Nov 18 '22 at 22:25