# Creating simple table

How do I create a table like this? I find it very hard to create the zero-row and zero-column.

Edit: I am looking for a solution where a formula eg. Table or something else is being used. I can already create the table manually.

• @Nasser Nothing.. There can be 0 if makes it easier of course. Aug 2, 2013 at 23:34
• Try with a Grid[] with element (1,1) as "" or Null Aug 2, 2013 at 23:37

If it is only for display purposes, you could wrap the unwanted elements in Invisible which works pretty well, because the invisible part will have the same size as the element you wrapped.

The rules to create the numbers is pretty easy, but the empty elements are not consistent. I see it has something to do with the distance to the {0,0} element, but only when both x and y are greater 0.

Table[With[{value = Abs[x]*Abs[y]},
If[x < 0 && y < 0 || Sign[x y] > 0 && Norm[{x, y}] > 4,
Invisible[0], value]
], {y, -1, 4}, {x, -1, 4}] // Grid


• Thank you for the respond. However, it is not only for the display purpose. I also need to know how to generate such a matrix exactly. Aug 2, 2013 at 23:51
• @JensJensen It wasn't clear that you try to find the creation rule too. Please see my edit. Aug 3, 2013 at 0:22
• This is the best solution because you create a function. Thank you very much! Aug 3, 2013 at 12:04

It's not clear why the sixes are included and without additional samples I see no point in guessing. Omitting that detail your table can be had with this:

Array[If[-2 < +## < 5, Abs[1 ##]] &, {6, 6}, -1] // Grid


$\begin{array}{cccccc} & 0 & 1 & 2 & 3 & 4 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 1 & 0 & 1 & 2 & 3 & \\ 2 & 0 & 2 & 4 & & \\ 3 & 0 & 3 & & & \\ 4 & 0 & & & & \end{array}$

I used terse coding tricks because that's how I enjoy these questions. +## is short for Plus[##] and 1 ## is equivalent to Times[1, ##]. Null is the default return of If, absent a third argument.

If this is an actual example and not a toy version of something you require, then hard to imagine why you need a creation rule and not just type the list explicitly. But having said that:

SparseArray[{{i_, j_} /; (2 < i + j < 9 || {i, j} == {4, 5} || {i, j} == {5, 4}) :>
Abs[(i - 2) (j - 2)]}, {6, 6}, ""];
Grid[%]


• Very neat...now I understand that you can set default "0" element. Agree with your general comment as in all codes need to add specific conditions to deal with adding the two 6's or the {1,1}. @halirutan removes lower elements using Norm and signs to deal with {1,1} element. Aug 3, 2013 at 4:32

This is a truncated multiplication table. You could use:

tab = SparseArray[{{i_, j_} /; i + j <= 7 -> (i - 1) (j - 1), {i_, j_} /;
i + j > 7 -> ""}, {5, 5}];
TableForm[Normal[tab], TableHeadings -> Table[Range[5] - 1, {2}]]


This yields:

EDIT

In order to provide a legitimate answer, I use the criterion from @halirutan code which knocks out the lower matrix elements. The correct result is achieved:

tab = SparseArray[{{1, 1} -> Invisible[0], {1, j_} /; j > 1 ->
j - 2, {i_, 1} /; i > 1 ->
i - 2, {i_,
j_} /; (i > 2 && 2 > 1 && Norm[{i - 2, j - 2}] <= 4) -> (i -
2) (j - 2), {i_, j_} /; Norm[{i - 2, j - 2}] > 4 ->
Invisible[0]}, {6, 6}];
Normal[tab] // TableForm


yielding:

• Apologies: my answer does not exactly match request and as hhas been commented the rules require modification. Just adding {2,5}->"",{5,2}->"" to the list of rule corrects this but is not within the spirit of the question. Aug 3, 2013 at 1:08
• Thank you for the solution. This was what I was looking for. However i personally prefere @halirutan code. Aug 3, 2013 at 12:03
• @JenJensen it was your question and you are the judge. I learned a lot from the solutions of halirutan and Mike Honeychurch. Aug 3, 2013 at 12:43