8
$\begingroup$

I want to create a PNG picture of a matrix where its entries are the following:

n,   n-1,   ..., 1,
2n,  2n-1,  ..., n+1,
3n,  3n-1,  ..., 2n+1,
... 
n^2, n^2-1, ..., (n-1)n+1

For example, I was thinking something like the picture below (including the dots)

enter image description here

I have tried to use the command mat = {{1, 2}, {3, 4}} but it does not accept {...} as the three dots.

I would really appreciate any suggestions/hints on how to do this.

UPDATE: I have created using the following code

MatrixForm[{{n, -1 + n, -2 + n, \[CenterEllipsis], 1}, {2 n, 2 n - 1, 
2 n - 2, \[CenterEllipsis], n + 1}, {3 n, 3 n - 1, 
3 n - 2, \[CenterEllipsis], 
2 n + 1}, {\[VerticalEllipsis], \[VerticalEllipsis], \\[VerticalEllipsis], \[DescendingEllipsis], \[VerticalEllipsis]}, \{n^2, -1 + n^2, -2 + n^2, \[CenterEllipsis],     HoldForm[(n - 1) n + 1]}}, TableAlignments -> Right] // TraditionalForm

this matrix

enter image description here

Thank you very much for your help.

$\endgroup$
  • 1
    $\begingroup$ Try reading the docs for Table[ ] and then for MatrixForm[ ] $\endgroup$ – Dr. belisarius Nov 5 '15 at 13:42
  • $\begingroup$ @belisariusisforth Thanks, but how one creates the dots in a matrix? $\endgroup$ – johnny09 Nov 5 '15 at 13:48
  • 1
    $\begingroup$ What's a "pgn picture"? Do you mean a PNG image? $\endgroup$ – m_goldberg Nov 5 '15 at 13:56
  • $\begingroup$ @m_goldberg Yes, sorry. Small typo, I have corrected it now. $\endgroup$ – johnny09 Nov 5 '15 at 13:58
10
$\begingroup$

You can just type it in. Like so

example

The various forms of ellipses are found on the Special Characters palette under the § tab of the Symbols tab. This palette is available from the Palettes menu.

You can get a PNG by selecting the output cell and choosing Save Selection As... from the File Menu (as I did for this post).

$\endgroup$
  • $\begingroup$ Thanks so much. I didn't know there were any special characters. :) $\endgroup$ – johnny09 Nov 5 '15 at 14:19
8
$\begingroup$

Here's a approach that uses the fact that the various directed dots are spelled SpanFromLeft, SpanFromAbove, and SpanFromBoth.

{hdots, vdots, ddots} = 
 ToString[#, StandardForm] & /@ {SpanFromLeft, SpanFromAbove, SpanFromBoth};

Here's a function that will replace everything but the upper left $ 2 \times 2 $ block and the last row and column with dots

insertDots[matrix_?MatrixQ] :=
  With[{dims = Dimensions@matrix, size = 3},
   With[{rows = dims[[1]], cols = dims[[2]]},
    With[{dropped =
       Map[Drop[#, {3, -2}] &, Drop[matrix, {3, -2}]]},
     Insert[
       Insert[dropped, hdots, {#, size} & /@ Range@size],
       Insert[ConstantArray[vdots, size], ddots, size], size] /;
      AllTrue[dims, size <= # &]]]];

We can use it like so:

array = Outer[Subscript[a, ##] &, Append[Range[3], m], Append[Range[5], n]];

MatrixForm[insertDots@array]

matrix with dots

$\endgroup$
  • 1
    $\begingroup$ Nice :). I rewrote my answer using yours :) $\endgroup$ – Dr. belisarius Nov 5 '15 at 14:40
7
$\begingroup$

Edit: Code modified to use @Pillsy's SpanFrom... trick instead of Graphics

shortm[m1_] := Module[{m, s = ConstantArray[ToString[#, StandardForm], 4] &}, 
  m = Drop[m1, {4, Length@m1 - 1}, {4, Length@m1 - 1}];
  m[[All, 3]] = s@SpanFromLeft;
  m[[3, All]] = s@SpanFromAbove;
  m[[3, 3]]   = SpanFromBoth;
  m]

Then use it as follows:

f[n_] := Table[n i - j + 1, {i, n}, {j, n}]
MatrixForm@shortm@f[5]

Mathematica graphics

MatrixForm@shortm@f[10]  

Mathematica graphics


Previous code using Graphics[ ]

shortm[m1_] := Module[{m, g},
  m = Drop[m1, {4, Length@m1 - 1}, {4, Length@m1 - 1}];
  g = Graphics[{PointSize[Small], Point[{{-1, 0}, {0, 0}, {1, 0}}]}, 
                PlotRange -> 2 {{-1, 1}, {-1, 1}}, ImageSize -> 20];
  m[[All, 3]] = ConstantArray[g, 4 ];
  m[[3, All]] = ConstantArray[Rotate[g, Pi/2], 4 ];
  m[[3, 3]]   = Rotate[g, -Pi/4];
  m]
$\endgroup$
  • $\begingroup$ the original graphics approach was a good alternative in case you want to avoid special character font issues. $\endgroup$ – george2079 Nov 5 '15 at 15:00
  • $\begingroup$ @george2079 True. I re-added at the end $\endgroup$ – Dr. belisarius Nov 5 '15 at 15:04

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.