I just want to create a simple table:

Three input columns, one output column

Call the inputs {x,y,z}, each can take values {-1,0,1}

The output column should be some function f(x,y,z)

Sample table:

x  | y  | z  | f(x,y,z)

-1 | -1 | -1 | f(-1,-1,-1)

1  | -1 | -1 | f(1,-1,-1)

etc... (all possible combinations)


  • 2
    $\begingroup$ {##, f[##]} & @@@ Tuples[{-1, 0, 1}, {3}] but this is very likely a duplicate. Also, Hello :), have you tried anything? What with the documentation for "table"? Is it about Mathematica? I'm asking because your syntax does not seem to reflect it is so. $\endgroup$
    – Kuba
    Oct 25, 2013 at 19:50
  • $\begingroup$ @kuba Could be a duplicate, but that would be difficult too find. In the meantime, could you use your comment as an answer, so that our answer rate improves? ;-) $\endgroup$ Oct 25, 2013 at 20:07
  • $\begingroup$ @SjoerdC.deVries done :) Also, related - summation with constraints. $\endgroup$
    – Kuba
    Oct 25, 2013 at 20:37

2 Answers 2


Kuba has shown us how to generate the table you want, but didn't give any advice about displaying it in a way looks nice. I'm assuming you are interested in learning a little about formatting as well.

There are, of course, an unlimited number of ways to format data in Mathematica. I will discuss just two, one using TableForm and the other using Grid.

data = {##, f[##]} & @@@ Tuples[{-1, 0, 1}, {3}];

A quick and somewhat dirty method using TableForm:

  TableHeadings -> {None, {"x", "y", "z", "f(x,y,z)"}},
  TableAlignments -> Right]

This produces


The one glaring problem with TableForm is that the right-aligned last column is ugly. Unfortunately, TableForm AFAIK doesn't allow controlling the alignments on a column-by-column basis.


Thanks to Kuba for pointing out that my formatting expression using Grid could be much simplified.

A better method using Grid:

Grid and its associated functions, such as Item and others, allow for much finer control over table formatting. But Grid is not always easy to use. Indeed, it can sometimes seem difficult even to accomplish what one hopes will be a trivial bit of formatting. I have seen posts cursing Grid as even dirtier than TableForm. In this case, Grid is easy and gives good results.

labeledData = Prepend[data, {"x", "y", "z", "f(x,y,z)"}];
  Alignment -> {{Right, Right, Right, Left}},
  Dividers -> {{-2 -> True}, {2 -> True}},
  Spacings -> {2, Automatic}]


  • 1
    $\begingroup$ No need to Map with Item. Alignment -> {{Right, Right, Right, Left}} will do. +1. $\endgroup$
    – Kuba
    Oct 26, 2013 at 12:23
  • $\begingroup$ @Kuba. I missed that when reading the docs. Thanks for pointing it out. Will make edit to include this as it is a major improvement. $\endgroup$
    – m_goldberg
    Oct 26, 2013 at 12:53

Straightforward way to achieve this is, just after reading the documentation for Table:

v1 = Table[{i, j, k, f[i, j, k]}, {i, #}, {j, #}, {k, #}] &[{-1, 0, 1}] // Flatten[#, 2] &

A little improved usage of Table:

v2 = Table[Flatten[{i, f @@ i}], {i, Tuples[{-1, 0, 1}, {3}]}];

or, what I usually do, not the king of performance but clear and compact Apply:

v3 = {##, f[##]} & @@@ Tuples[{-1, 0, 1}, {3}];

variation with Array:

v4 = Array[{##, f[##]} &, {3, 3, 3}, {{-1, 1}}] // Flatten[#, 2] &

v1 == v2 == v3 == v4
  • $\begingroup$ Wonderful, thanks!! Couldn't figure out the documentation.. $\endgroup$
    – smörkex
    Oct 25, 2013 at 20:24
  • 1
    $\begingroup$ @Kurt Press F1 and use it :) or use online documentation. Take a look at Table, especially the 5th syntax. I'm glad you find it useful. Good luck :) $\endgroup$
    – Kuba
    Oct 25, 2013 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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