2
$\begingroup$

I'm trying to generate square grid templates to drill holes on a plate for a fragmentation experiment. I have three different problems that I'm not finding a way to solve using the documentation, they are:

  1. I need to keep track of the order in which the cells would be drilled, but I couldn't find a way to do this using SparseArray.
  2. I which to apply a conditional formating rule where the cells that would be drilled have a dark gray background and white number, but thus far all my attempts to use conditions with Grid options failed.
  3. I need to be able to generate and print a large number of these templates, I would usually use Table or TableForm to do this, but since I don't know a way to divide the array into a matrix/grid I'm not getting an output that is printer friendly.

The basics of the program is this:

GridTemplate[msize_, ndrill_] := Module[{ms, nd},
(*creates a square grid template of size msize and number of drilling holes ndrill*)
  ms = msize;
  nd = ndrill;
  Grid[SparseArray[# -> 1 & /@ Table[RandomInteger[{1, ms}, 2], {nd}]], Frame -> All]]
$\endgroup$
2
$\begingroup$
gridTemplate[msize_, ndrill_] := Grid[SparseArray[
   MapIndexed[# -> Item[Style[#2[[1]], White], Background -> Gray] &, 
    RandomSample[Tuples[Range[msize], 2], ndrill], 1], {msize, msize}], Frame -> All]

gridTemplate[10, 5]

enter image description here

Alternatively,

gridTemplate2[msize_, ndrill_] := Grid[SparseArray[Thread[
RandomSample[Tuples[Range[msize], 2], ndrill] -> 
 Array[Item[Style[#, White], Background -> Gray] &, ndrill]], {msize, msize}], Frame->All]

gridTemplate2[10, 10]

enter image description here

$\endgroup$
  • $\begingroup$ many thanks @kglr, your answer, as always, was very enlightening. My original code has a bug since I didn't account for repeated sites being generated by the random number generated, and I'll post them here, in case anyone else needs it. $\endgroup$ – nicholas80 Apr 9 '18 at 17:15
  • $\begingroup$ Now I have another problem related to this one, which is instead of a completely random sequence of holes, I have to randomly chose the first hole and then the following holes must be chosen holes from the Von Neumann neighborhood of the holes. I have no clue on how to implement this kind of algorithm. I've tried to understand how it was implemented in this thread (mathematica.stackexchange.com/q/39793/47756), but I didn't really get it how it works. So if you have any insight for how to tackle it or at least something to point me in the right direction, I'll be very grateful. $\endgroup$ – nicholas80 Apr 9 '18 at 17:28
  • $\begingroup$ @nicholas80, I don't have a suggestion off the top of my head. You might want to post this as a new question. $\endgroup$ – kglr Apr 9 '18 at 17:57
  • $\begingroup$ @nicholas80, the updated version does not produce duplicate positions. $\endgroup$ – kglr Apr 9 '18 at 18:26
0
$\begingroup$

In the original program I didn't account for the possibility of duplicated sites, so I've modified the code provided in the answer provided by @kglr to delete duplicate site entries and continue until the desired number of occupied site. Hope it helps.

gridTemplate[msize_, ndrill_] := Module[{l},
  (* msize is the size of the grid's square matrix
    ndrill is the number of holes made in the plate *)
  l = {};
  While[Length[l] < ndrill, 
   l = Append[l, RandomInteger[{1, msize}, 2]] // DeleteDuplicates];
  Grid[SparseArray[
    MapThread[# -> Item[Style[#2, White], Background -> Gray] &, {l, 
      Range[ndrill]}], {msize, msize}], Frame -> All]]
$\endgroup$
  • $\begingroup$ Hi, @kglr I don't know if here is the right place to ask or if I should start another thread found a problem when I try to get the position of non-zero elements of this grid. I'm using list = Flatten@Normal[gridTemplate4[10, 2]]; Position[list, x_ /; x != 0] The result I get is something like this {{48, 1, 1}, {48, 1, 2, 1}, {48, 2, 2, 1}, {74, 1, 1}, {74, 1, 2, 1}, {74, 2, 2, 1}} I've tried to understand why it doesn't return just the position of non-zero entries on the list, and return every issue doubled and like it has more levels on the list than it should. $\endgroup$ – nicholas80 May 10 '18 at 16:54

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.