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.


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:24Out[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