Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

The table

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.

share|improve this question
    
@Nasser Nothing.. There can be 0 if makes it easier of course. –  Jens Jensen Aug 2 '13 at 23:34
    
Try with a Grid[] with element (1,1) as "" or Null –  Gustavo Delfino Aug 2 '13 at 23:37
add comment

4 Answers

up vote 7 down vote accepted

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

Mathematica graphics

share|improve this answer
    
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. –  Jens Jensen Aug 2 '13 at 23:51
    
@JensJensen It wasn't clear that you try to find the creation rule too. Please see my edit. –  halirutan Aug 3 '13 at 0:22
    
This is the best solution because you create a function. Thank you very much! –  Jens Jensen Aug 3 '13 at 12:04
add comment

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.

share|improve this answer
add comment

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[%]

enter image description here

share|improve this answer
    
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. –  ubpdqn Aug 3 '13 at 4:32
add comment

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:

enter image description here

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: enter image description here

share|improve this answer
    
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. –  ubpdqn Aug 3 '13 at 1:08
    
Thank you for the solution. This was what I was looking for. However i personally prefere @halirutan code. –  Jens Jensen Aug 3 '13 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. –  ubpdqn Aug 3 '13 at 12:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.