7
$\begingroup$

This is my first time using Mathematica and for a homework assignment we were asked to print this table:

enter image description here

By using this code TableForm@Table[j , {i, 1, 9}, {j, 1, i}] to print the table but with the 9 ones on the right and the one nine on the left. Is there some way to use table form manipulations to flip the table around horizontally, or use different logic to get the same result?

$\endgroup$
  • 2
    $\begingroup$ change {j, 1,i} to {j, i,1,-1}? See also Reverse and PadLeft in the docs. $\endgroup$ – kglr Feb 2 at 2:09
9
$\begingroup$

Another possibilities

n = 9;
tbl = Table[If[n - i < j, n - j + 1, ""], {i, 1, 9}, {j, 1, 9}];

MatrixForm[tbl]

Mathematica graphics

Grid[tbl]

Mathematica graphics

Grid[tbl, Frame -> All]

Mathematica graphics

|improve this answer|||||
$\endgroup$
  • $\begingroup$ A SparseArray[] equivalent for your first snippet: With[{n = 9}, SparseArray[{{i_, j_} /; n - i < j :> n - j + 1}, {n, n}, ""] // MatrixForm] $\endgroup$ – J. M.'s technical difficulties Feb 4 at 14:00
8
$\begingroup$

You can also play with alternative ways to generate the same table:

table = PadLeft[Reverse /@ Range @ Range @ 9, Automatic, ""];

TeXForm @ TableForm @ table

$\begin{array}{ccccccccc} \text{} & \text{} & \text{} & \text{} & \text{} & \text{} & \text{} & \text{} & 1 \\ \text{} & \text{} & \text{} & \text{} & \text{} & \text{} & \text{} & 2 & 1 \\ \text{} & \text{} & \text{} & \text{} & \text{} & \text{} & 3 & 2 & 1 \\ \text{} & \text{} & \text{} & \text{} & \text{} & 4 & 3 & 2 & 1 \\ \text{} & \text{} & \text{} & \text{} & 5 & 4 & 3 & 2 & 1 \\ \text{} & \text{} & \text{} & 6 & 5 & 4 & 3 & 2 & 1 \\ \text{} & \text{} & 7 & 6 & 5 & 4 & 3 & 2 & 1 \\ \text{} & 8 & 7 & 6 & 5 & 4 & 3 & 2 & 1 \\ 9 & 8 & 7 & 6 & 5 & 4 & 3 & 2 & 1 \\ \end{array}$

Also

table2 = Reverse /@ LowerTriangularize @ Array[#2 &, {9, 9}] /. 0 -> ""

TeXForm @ TableForm @ table2

same picture

|improve this answer|||||
$\endgroup$
1
$\begingroup$

Just for fun, an additional method (I wouldn't suggest to use this though :P)

ConstantArray[Rotate[10 - #, -Pi/2], #] & /@ Range[9];
Rotate[Grid[%], Pi/2]

enter image description here

|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.