5

I have

m = {{0, -1, -2, -3, -4}, {1, 0, -1, -2, -3}, {2, 1, 0, -1, -2}, {3, 
    2, 1, 0, -1}, {4, 3, 2, 1, 0}};

And a grid Grid[m,Frame->All]

I would like to label the grid such that every column has a number above it and every row has a number to its side. This is what I've currently managed to do.

Labeled[Grid[m,Frame->All],{Y:{1,5},X:{1,5}},{Left,Top}] 

How can I label the grid appropriately?

pyler
  • 153
  • 4

2 Answers2

7

You could use TableForm, e.g.

col = CharacterRange["A", "E"];
row = CharacterRange["a", "e"];
TableForm[m, TableHeadings -> {row, col}]

A way using Grid:

Grid[{PadLeft[col, 6, ""]}~Join~
  MapThread[PadLeft[#1, 6, #2] &, {m, row}], 
 Dividers -> {{False, True, {False}}, {False, True, {False}}}]

enter image description here

enter image description here

ubpdqn
  • 60,617
  • 3
  • 59
  • 148
7

Perhaps what you want:

m = Array[Subtract, {5, 5}, 0];
r = Range @ 5;

Grid[
 ArrayFlatten[{{"", {r}}, {{r}\[Transpose], m}}],
 Frame -> All
]

enter image description here

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371