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.

Are table headings for aesthetics only? If I have:

table1 = TableForm[{{1, 1, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 0, 1, 0, 0}, {0, 0, 
   1, 1, 1}, {0, 0, 1, 0, 1}, {1, 1, 0, 0, 0}, {0, 1, 1, 0, 0}}, 
 TableHeadings -> {{"1", "2", "3", "4", "5", "6", "7"}, {"a", "b", 
    "c", "d", "e"}}]

Is there any way to refer to a part of the table by the heading names? For example, if I want the position of all 1's in the table,

Position[table1, 1]

I would like this to return both numbers and letters. Is this possible?

I apologize if this question is elementary. Any help is much appreciated!

share|improve this question
6  
What wasn't really spelled out by the answers, but is important to understand, is that the purpose of TableForm is displaying tables in a pretty way. Don't use TableForm as a data structure (unless you know for certain that this is an advantage for you...). Just use it to display a table stored as a List in two-dimensional (readable) form. –  Szabolcs Nov 26 '12 at 20:33

3 Answers 3

Let's figure it out by inspecting the full form of a TableForm:

TableForm[{{1, 2}, {3, 1}}, TableHeadings -> {{"a", "b"}, {"c", "d"}}] // FullForm

TableForm[List[List[1,2],List[3,1]],Rule[TableHeadings,List[List["a","b"],List["c","d"]]]]

Evidently, then, we can obtain the list as the first element of the TableForm and we can find the headings, assuming they exist, from the TableHeadings rule. Under these assumptions, one solution is to create a parallel table of the desired addresses of all list positions and index into that:

addresses = Outer[List, Sequence @@ (TableHeadings /. Cases[table1, _Rule]), 1];
addresses[[Sequence @@ #]] & /@ Position[table1 // First, 1]

{{1,a},{1,b},{2,b},{3,c},{4,c},{4,d},{4,e},{5,c},{5,e},{6,a},{6,b},{7,b},{7,c}}

share|improve this answer
1  
Thank you @whuber! I am still very new at this and I didn't know one could use //Fullform inspect functions. –  Pancholp Nov 27 '12 at 0:00

Here is an alternative formulation that does the same thing as the answer by @whuber but uses what I think is more idiomatic syntax:

Flatten[Pick[
  Outer[List, Sequence @@ OptionValue[Options[table1], TableHeadings]
   ],
  table1[[1]],
  1],
 1]

{{"1", "a"}, {"1", "b"}, {"2", "b"}, {"3", "c"}, {"4", "c"}, {"4", 
  "d"}, {"4", "e"}, {"5", "c"}, {"5", "e"}, {"6", "a"}, {"6", 
  "b"}, {"7", "b"}, {"7", "c"}}

The table headings are options, so they (like all options) can be extracted using OptionValue[Options[table1], TableHeadings]. I then construct a matrix of tuples with the combined table headings, similar to what @whuber did (using Outer). The command Pick does the remaining work.

share|improve this answer
    
I had never seen these functions either. I will read up on Options too. Thanks @Jens, I really appreciate it! –  Pancholp Nov 27 '12 at 0:03

To emphasize Szabolcs' point, if you had data you wanted to display and work with, you might put and row and column identifiers in separate lists

rowNames = {"1", "2", "3", "4", "5", "6", "7"};
columnNames = {"a", "b", "c", "d", "e"};
data = {{1, 1, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 0, 1, 0, 0}, {0, 0, 1,1, 1}, {0, 0, 1, 0, 1}, {1, 1, 0, 0, 0}, {0, 1, 1, 0, 0}};

and use TableForm[ ] to display it in a table:

TableForm[data, TableHeadings -> {rowNames, columnNames}]  

But, to get an element from data, you would probably use some function like this:

getdata[row_, col_] := Module[{r, c}, 
 r = Position[rowNames, row][[1, 1]]; 
 c = Position[columnNames, col][[1, 1]]; 
 data[[r, c]]
]  

in the following manner:

getdata["2", "b"]  

to get the returned value of 1. So, the table display created by TableForm[ ] is just for display and not a normal way to store data.

share|improve this answer

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.