Group Abstract Group Abstract

Message Boards Message Boards

0
|
11.4K 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

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
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

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

Hi Brandon,

No problem, I see the approach you're taking. Consider:

Begin with the table as before,

table = Table[i j, {i, 1, 12}, {j, 1, 12}];
% // TableForm

enter image description here

Then insert, using Transpose (very useful function), a column of numbers 1 to 12

(* insert a column *)
table = Transpose[Insert[Transpose[table], Range[12], 1]];
% // TableForm

enter image description here

Lastly, insert the row.

(* Insert a row *)
PrependTo[table, Range[0, 12]];
table // TableForm

enter image description here

Thanks, Ben

POSTED BY: Benjamin Goodman
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

Here is a multiplication table generator function that is a bit smarter. And, its also fun to see what happens when you place different number forms in there. For example, if you pass in the first number with a decimal point only all output shall have a decimal. This does not hold if you pass in the second number with a decimal point. Also, be careful when passing exponents. You just might end up with a crazy large table.

While a utility function probably exists somewhere in M, yet I do not know what such function is called. While I welcome enhancements and optimizations, the formatting is my own personal style so please feel free to ignore it in favor of your own conventions.

Edit; Now handles inverted ranges.

genMultiTable[low_, high_] := Module[ 
  {x , y, l, h, s, lh, hh},
  (* Later expand functionality by handling options *)
  (* 
  Needs two vars otherwise the headings do not work *)

  s = If[high < low
    , -1
    , 1
    ]; 
  l = low;
  h = high;
  x = y = low;
  lh = hh = Range[low, high, s];
  Table[x*y, {x, l, h, s}, {y, l, h, s}]
    // TableForm[# , TableAlignments -> Right, 
      TableHeadings -> {lh, hh} ] & 
    // Framed[#, FrameStyle -> {Thick, Blue} ] &
  ]
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 BY: Szabolcs Horvát
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
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard