How can I use the functions decimalToBinary and padWithZeros to produce the same output as Tuples[{0, 1}, 4]?
Asked
Active
Viewed 51 times
-1
m_goldberg
- 107,779
- 16
- 103
- 257
tamir
- 1
-
Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Dec 04 '16 at 08:50
-
4What are "decimalToBinary" and "padWithZeros"? Is this a homework assignment and are those functions introduced in your course? – Szabolcs Dec 04 '16 at 10:13
1 Answers
4
It all can be done in one command:
a = Table[IntegerDigits[k, 2, 4], {k, 0, 15}]
Or:
a = IntegerDigits[Range[0, 15], 2, 4]
The 2nd argument = 2 is the base we want and the 4 pads the answer on the left for both commands.
(*{{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}}*)
a - Tuples[{0, 1}, 4]
(*{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0,
0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0,
0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0,
0, 0, 0}, {0, 0, 0, 0}}*)
Showing that we have the same output.
bobbym
- 2,628
- 2
- 15
- 20