Group Abstract Group Abstract

Message Boards Message Boards

0
|
12.1K Views
|
12 Replies
|
8 Total Likes
View groups...
Share
Share this post:

Create a multiplication table using AppendTo/Drop?

Posted 9 years ago
clear[Num4];
        Num4 = Table[{i*j}, {i, 1, 12, 1}, {j, 1, 12, 1}];
        Num4 // TableForm

I am trying to produce a proper multiplication table; however, the current code only produces the output for the table and does not show the actual rows/columns listed as 1,2..12.

I know I need to use AppendTo but cannot figure out exactly what to do. Any help is appreciated.

POSTED BY: Brandon Davis
12 Replies
Posted 9 years ago

Thanks much. I'm pretty new to Mathematica. So this is the best way to learn!

POSTED BY: David Fuller
Posted 9 years ago

David,

This is your function, slimmed down a bit:

genMultiTable2[low_, high_] := Module[
  {s, r},
  s = If[high < low, -1, 1];
  r = Range[low, high, s];
  Table[x*y, {x, low, high, s}, {y, low, high, s}]
  // TableForm[#, TableAlignments -> Right, TableHeadings -> {r, r}] &
  // Framed[#, FrameStyle -> {Thick, Blue}] &
]
POSTED BY: Hans Milton
Posted 9 years ago
POSTED BY: David Fuller
Posted 9 years ago

I like the output of this one better.

POSTED BY: David Fuller
Posted 9 years ago

Thank you everyone!

The best part about coding is that there is SO many ways to get the same process done.

I really appreciate you all!

Brandon

POSTED BY: Brandon Davis
Posted 9 years ago

Yet another version:

enter image description here

Table[i j, {i, 12}, {j, 12}] //
TableForm[#, TableAlignments -> Right, TableHeadings -> Automatic] & //
Framed[#, FrameStyle -> {Thick, Blue}] &
POSTED BY: Hans Milton
Posted 9 years ago

Another method :

Grid[Table[  a*b, {a, 1, 12}, {b, 1, 12}], {Frame -> {{Left}, {Top}}}]

OR

Grid[Table[  a*b, {a, 1, 12}, {b, 1, 12}], {Dividers -> {{None, 1}, {None, 1}, }}]
POSTED BY: David Fuller

If you want to insert those elements explicitly, consider ArrayFlatten.

tab = Table[i j, {i, 12}, {j, 12}];

ArrayFlatten[
 {{0, {Range[12]}},
  {List /@ Range[12], tab}}
 ]

If you are new to Mathematica, think twice before using AppendTo/PrependTo as they modify the value of a variable. The first argument must be a variable. Such repeated modification is often not the best approach in Mathematica. The usual way to do things is with functions that return results instead of modify their arguments in place. In other words, prefer functions that have no side effects as these can be composed together easily.

Of course AppendTo does have a very useful role. Just make sure it's what you really need.

POSTED BY: Szabolcs Horvát

You can simplify that code a bit:

TableForm[
 Table[i j, {i, 12}, {j, 12}],
 TableHeadings -> Automatic
 ]

enter image description here

POSTED BY: Szabolcs Horvát
POSTED BY: Benjamin Goodman
Posted 9 years ago

Benjamin,

This is exactly what I meant. I tried to use the command AppendTo, but I was having some trouble and ended up creating more code than I needed. It produced the same table but was very tedious...I am a novice in Mathematica but picking up on things quickly..

This is how I went about it, like I said very tedious. If you can use one line of code vs ten lines of code then I would always choose one line of course..

clear[Num4];

Num4 = Table[{i*j}, {i, 1, 12, 1}, {j, 1, 12, 1}]; PrependTo[Num4, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}]; PrependTo[Num4[[1]], 0]; PrependTo[Num4[[2]], 1]; PrependTo[Num4[[3]], 2]; PrependTo[Num4[[4]], 3]; PrependTo[Num4[[5]], 4]; PrependTo[Num4[[6]], 5]; PrependTo[Num4[[7]], 6]; PrependTo[Num4[[8]], 7]; PrependTo[Num4[[9]], 8]; PrependTo[Num4[[10]], 9]; PrependTo[Num4[[11]], 10]; PrependTo[Num4[[12]], 11]; PrependTo[Num4[[13]], 12];

Like I said, very tedious and almost pointless, although it DID give me the result I wanted, but time is of essence for things like this. I don't even know how to properly include my code in this post so it looks nice and not messy like what it looks like now.

Thank you for your help. I appreciate it.

Brandon

POSTED BY: Brandon Davis

Is this what you mean?

TableForm[
 Table[{i*j}, {i, 1, 12, 1}, {j, 1, 12, 1}],
 TableHeadings -> {Range[12], Range[12]}
 ]

enter image description here

POSTED BY: Benjamin Goodman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard