# How to build a histogram from the mean of 100 matrices

I have a matrix that looks like this:

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


I want to generate a list of 100 matrices that looks like this:

n2 = 100;
s1 = Table[SparseArray[{{i_, j_} -> f[i, j]}, {n1, n1}], {k, n2}];


Now I want to build two histograms from the average of n2-matrices:

i) the frequency numbers one per line;

ii) the frequency numbers 1 per column.

From the distribution would get the kurtosis and skewness.

Can you help me?

• I don't quite understand your points i) and ii). Could you be more specific? Maybe show an example of what you want to achieve. – corey979 Sep 26 '16 at 14:12
• a bit aside to your question but the delayed set z:= causes the two z instances in RandomChoice to have different values. Probably you want a non-delayed z= – george2079 Sep 26 '16 at 14:29
• is this what you want for i ? Histogram[Mean[Flatten@#] & /@ s1] – george2079 Sep 26 '16 at 15:27
• The command s1 = Table[SparseArray[{{i_, j_} -> f[i, j]}, {n1, n1}], {k, n2}]; generates 100 matrices 5x5 . The elements of each matrix are 0 or 1. I would like to build a histogram of the frequency numbers 1 obtained in each row and each column. – SAC Sep 26 '16 at 15:49
• What are "frequency numbers 1"? How many times the number 1 occurs in each of the five rows? – corey979 Sep 26 '16 at 15:50

## 1 Answer

I'm not really sure what is expected, so let it be for a start.

To get the mean of the matrices s1:

mean = Normal @ Mean @ s1;
MatrixForm @ mean One can visualize the matrix with MatrixPlot:

MatrixPlot[mean, PlotLegends -> Automatic] To get the row-wise histogram:

rowSum = Total /@ Total /@ Transpose@s1


{92, 82, 78, 63, 63}

Plot[Piecewise@
Table[{rowSum[[i]], i - 0.5 < x < i + 0.5}, {i, 1,
Length@rowSum}], {x, 0.5, 5.5}, Frame -> True,
PlotRange -> {All, {0, All}}, Axes -> False,
FrameLabel -> {"Row number i", "Count of 1"}] Because columns are rows of a transposed matrix, let's just transpose each of them in the list s1 and proceed like previously:

sT = Transpose /@ s1;
colSum = Total /@ Total /@ Transpose@sT


{0, 46, 74, 106, 152}

Plot[Piecewise@
Table[{colSum[[i]], i - 0.5 < x < i + 0.5}, {i, 1,
Length@colSum}], {x, 0.5, 5.5}, Frame -> True,
PlotRange -> {All, {0, All}}, Axes -> False,
FrameLabel -> {"Column number i", "Count of 1"}] 