8
$\begingroup$

I want to build a grid to look like the top-left table shown below, but without an outside frame.

enter image description here

Here is what I've tried to do:

a = {
    {, , 2, 2, 2, 2,},
    {, , 3, , 2, 0, 3},
    {, 2, 2, , 3, , 3},
    {2, 2, 2, , , , 2},
    {2, 3, , , 1, ,},
    {2, , 1, 3, , ,},
    {3, 3, 3, 2, 3, , 3}
    };

f = Riffle[#, "\[CenterDot]", {1, -1, 2}] &;

f /@ Transpose[f /@ a] // Transpose // Grid

enter image description here

What I don't like in my result is that I have three times more dots than I need. And I don't know how to fix it. Suggestions?

$\endgroup$
  • 2
    $\begingroup$ You could replace the extra dots with spaces, by inserting this between Transpose and Grid: MapIndexed[If[OddQ@Total@#2, " ", #1] &, #, {2}] & $\endgroup$ – Rahul Dec 26 '12 at 3:04
  • $\begingroup$ Unfortunately, something like Grid[a, Frame -> All, FrameStyle -> Dashing[{0, 15.1}]] cannot be used here effectively, as it dots are too small and setup requires manual experimentation with the magic number on each platform. $\endgroup$ – István Zachar Dec 26 '12 at 15:58
8
$\begingroup$

Depending on what you are doing, this might be better solved by using Graphics commands and building the display as a graphics object rather than a textural output. This however does the trick with just inserting elements into the grid shape:

 gridDots[a_] := Module[{
   rowspacing = Riffle[#, " ", {1, 1 + Last@Dimensions[a] 2, 2}] &,
   colspacing = Riffle[#, {Flatten[ConstantArray[{".", " "}, 1 + Last@Dimensions@a]]},
                        {1, 1 + 2 First@Dimensions@a , 2}] &},
 colspacing[rowspacing /@ a]
 ]

 gridDots[a] // Grid

grid of numbers with dots intserted

$\endgroup$
6
$\begingroup$

Maybe

evenrows = Prepend[#, " "] & /@ (Join @@ Thread[{#, " "}] & /@ a);
oddrows = (Join @@ ConstantArray[{"\[CenterDot]", " "}, 8]);
Grid@Riffle[evenrows, {oddrows}, {1, -1, 2}]

enter image description here

?

$\endgroup$
1
$\begingroup$

Yet another option:

  Grid[SparseArray[
  {{i_, j_} /; OddQ[i] && OddQ[j] :> "\[CenterDot]",
   {i_, j_} /; EvenQ[i] && EvenQ[j] :> a[[i/2, j/2]]}
  , {2 Length[a] + 1, 2 Length[a] + 1}, ""]]
$\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.