0
$\begingroup$

I have two lists shown below:

list1 = {(0,0,0,0),(0,0,0,1),(0,0,1,0),...,(1,1,1,1)} list2 = {1,5,5,5,...,5}

How can I construct a function that outputs a table similar to this:

$$\begin{array}{c|c} \text{Initial vector} & \text{Period} \\ \hline (0,0,0,0) & 1 \\ (0,0,0,1) & 5 \\ (0,1,0,1) & 5 \\ (0,1,1,1) & 5 \\ \end{array}$$

I have experimented using Table, but it just outputs a list.

EDIT:

What is wrong with my function taken from one of the answers:

printPeriodTbl[vectors_, periods_] := 
  Grid[Prepend[
    Transpose[{Row[{"(", Row[#, ","], ")"}] & /@ vectors, 
      periods}], {"Initial Vector", "Period"}], 
   Dividers -> {2 -> True, 2 -> True}];

what I am passing into this function looks like:

vectors =  {{0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 0}, {0, 0, 1, 1}, {0, 1, 0, 0}, {0, 1, 0, 1}, {0, 1, 1, 0}, {0, 1, 1, 1}, {1, 0, 0, 0}, {1, 0, 0, 1}, {1, 0, 1, 0}, {1, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 0, 1}, {1, 1, 1, 0}, {1, 1, 1, 1}}
period = {1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}

Errors from this are:

Transpose::nmtx: The first two levels of {Pattern[((Row[#1,,])&)[listOfInitVectors],(Row[_,,])],listOfPeriods_} cannot be transposed. >>
Transpose::tperm: Permutation {Pattern[((Row[#1,,])&)[listOfInitVectors],(Row[_,,])],listOfPeriods_} is longer than the dimensions {2} of the expression. >>
$\endgroup$
2
  • $\begingroup$ Transpose[{list1,list2}] then use TableForm and add your table headers $\endgroup$ Feb 22, 2015 at 21:42
  • $\begingroup$ You have written list1 as {(0,0,0,0),(0,0,0,1),(0,0,1,0),...,(1,1,1,1)}. Since you clearly know that lists are {} the only way to interpret this is that (0,0,0,0) must be a string. In other words list1 and list 2 are both vectors (hence Transpose). Is that actually what you mean or is it a typo? $\endgroup$ Feb 22, 2015 at 22:21

2 Answers 2

2
$\begingroup$
list1 = {{0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 0}, {1, 1, 1, 1}};
list2 = {1, 5, 5, 5};

Grid[Prepend[Transpose[{Row[{"(", Row[#, ","], ")"}] & /@ list1, list2}],
  {"Initial Vector", "Period"}], Dividers -> {2 -> True, 2 -> True}]

enter image description here

or

 Grid[Join[{{"Initial Vector", "Period"}}, 
            Transpose[{Row[{"(", Row[#, ","], ")"}] & /@ list1, list2}]] ,
      Dividers -> {2 -> True, 2 -> True}]
 (* same output *)

or

tbl = Table[{Row[{"(", Row[list1[[i]], ","], ")"}], list2[[i]]}, {i, 1, Length@list1}];
Grid[Prepend[tbl, {"Initial Vector", "Period"}], Dividers -> {2 -> True, 2 -> True}]
(* same  output *)

Update: If your list1 is a list of strings like

 list1b = {"(0,0,0,0)", "(0,0,0,1)", "(0,0,1,0)", "(1,1,1,1)"} 

then you can simply use list1b instead of Row[{"(", Row[...],")"}].

$\endgroup$
10
  • $\begingroup$ Based on how the question was written I thought the only way to interpret list1 was {"(0,0,0,0})",...} unless he is completely unfamiliar with Mma syntax. $\endgroup$ Feb 22, 2015 at 22:19
  • $\begingroup$ @MikeHoneychurch, that makes more sense; makes it easier too. $\endgroup$
    – kglr
    Feb 22, 2015 at 22:42
  • $\begingroup$ Could you look at my edit please. I am confused on the issue $\endgroup$
    – connor
    Feb 22, 2015 at 22:42
  • $\begingroup$ @connor, if your list1 is a list of strings, e.g., {"(0,0,0,0)", "(0,0,0,1)", "(0,0,1,0)", "(1,1,1,1)"} then you don't need the Row[....] piece; you can just use list1 instead of Row[{"(", Row[#, ","], ")"}] & /@ list1. $\endgroup$
    – kglr
    Feb 22, 2015 at 22:49
  • $\begingroup$ I actually turned them into lists, so it is currently a list of lists like {{0,0,0,1},{0,0,0,1}} $\endgroup$
    – connor
    Feb 22, 2015 at 22:51
1
$\begingroup$
Grid[Prepend[Transpose@{list1, list2}, {"Initial Vector", "Period"}], Dividers -> {2 -> True, 2 -> True}]
$\endgroup$

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.