0
$\begingroup$

How I can go to a new line in the following code:

M = 2; 
L = 2; 
For[n = 1, n <= M, n++, For[k = 0, k <= L, k++, Print[u[n, k] = n*k]]]

I want to go to a new line after running the inner loop. Can I use \n?

I want to have:

PIC

Any suggestions?

$\endgroup$
7
  • $\begingroup$ @Karsten 7 I want to have new line, for this code I want to have 0 1 2 and the new line 0 2 4. $\endgroup$ – user45459 May 31 '17 at 19:25
  • 3
    $\begingroup$ What are your actual goals here? The approach you've chosen in unlikely to match your goals. In a notebook, print adds a cell to the notebook. $\endgroup$ – Alan May 31 '17 at 19:43
  • 2
    $\begingroup$ probably better to make a Table and use MatrixForm or TableForm $\endgroup$ – george2079 May 31 '17 at 19:48
  • $\begingroup$ Scan[Print[# Range[0, 2]] &, {1, 2}] $\endgroup$ – Ali Hashmi May 31 '17 at 19:54
  • 3
    $\begingroup$ If you are really intent on "printing" individual items and somewhat controlling the format you can use WriteString[$Output,expr] with "\n" as needed for line breaks. I'd really encourage you not to try to write c in mathematica though. $\endgroup$ – george2079 May 31 '17 at 21:22
3
$\begingroup$

Avoiding For, ApendTo(1)(2), and GraphicsGrid:

Grid[
  Array[Times, {2, 3}, {1, 0}],
  Spacings -> {4, 1}
]

$\begin{array}{ccccc} 0 & \text{ } & 1 & \text{ } & 2 \\ 0 & \text{ } & 2 & \text{ } & 4 \\ \end{array}$

$\endgroup$
3
$\begingroup$

Perversion of "\n" (StringRiffle):

StringRiffle[Table[i j, {i, 1, 2}, {j, 0, 2}], "\n", "\t"]

enter image description here

$\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.