0
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – corey979 Sep 26 '16 at 14:12
  • $\begingroup$ 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= $\endgroup$ – george2079 Sep 26 '16 at 14:29
  • $\begingroup$ is this what you want for i ? Histogram[Mean[Flatten@#] & /@ s1] $\endgroup$ – george2079 Sep 26 '16 at 15:27
  • $\begingroup$ 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. $\endgroup$ – SAC Sep 26 '16 at 15:49
  • $\begingroup$ What are "frequency numbers 1"? How many times the number 1 occurs in each of the five rows? $\endgroup$ – corey979 Sep 26 '16 at 15:50
3
$\begingroup$

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

enter image description here

One can visualize the matrix with MatrixPlot:

MatrixPlot[mean, PlotLegends -> Automatic]

enter image description here


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"}]

enter image description here

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"}]

enter image description here

$\endgroup$

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.