I'm a Mathematica novice here. I'm trying to execute the following code to add random 1's into a zero matrix, repeat this many times, and then bin/histogram the number of nearest neighbor 1s. Can anyone help me out with what I need to do to transform my list so that bincounts will play nicely?
ClearAll["Global`*"];
(*clear global variables*)
n = 10;
(*set number of diluent elements*)
Do[
b = SparseArray[{1, 1} -> 0, {100, 1},0];
(*generate zero matrix*)
a = RandomSample[Range[100], n];
(*select random sites to add elements *)
Do[b[[a[[i]]]] = 1, {i, n}];
(*insert elements*)
e[i] = b;
,
(*generate iterated variable to record each run*)
{i,50}
];
(*loop i times*)
f = Table[e[i], {i,1,50}];
(*generate list from iterated variable*)
Do[g[j, i] = f[[j, i]] + f[[j, i + 1]], {j, 50}, {i, 99}];
(*sum nearest neighbors and loop over data set*)
h = Table[g[j, i], {i, 1, 99}, {j, 1, 50}] ;
(*create an indexed list variable*)
BinCounts[h, {0, 3, 1}]
Where Bincounts returns the error: "BinCounts::vectmat: The first argument is expected to be a vector or matrix"
BinCount
working just do this:BinCount[Flatten[h],{0, 3, 1}]
$\endgroup$ – Stefan Jun 28 '13 at 17:33h = RandomInteger[{0, 1}, {100, 100}]
to generate a 100x100 matrix of 0's and 1's. Furthermore, I don't think you'll want to useBinCounts
to count the number of 1' that are adjacent (assuming this is what you want; your question isn't quite clear). $\endgroup$ – Teake Nutma Jun 28 '13 at 18:07