TransWikia.com

Creating a matrix

Mathematica Asked by RF_1 on July 24, 2021

I have a function as follows:

f[x_, t_] := x + t;
Table[{x, t, f[x, t]}, {x, 0, 1}, {t, 0, 2}]

When I write MatrixForm[%], I get

enter image description here

How to get a matrix as follows:

enter image description here

2 Answers

f[x_, t_] := x + t;
z = Table[{x, t, f[x, t]}, {x, 0, 1}, {t, 0, 2}];

There are several different formatting options, including the following. Note that the first two are presentation forms only; you can't do further operations on them. You can see this for yourself by applying Transpose to each of the three.

z1 = Flatten[z, 1];
TableForm[z1, TableHeadings -> {None, {"x", "t", "f(x,t)"}}]

enter image description here

z2 = Join[{{"x", "t", "f(x,t)"}}, Flatten[z, 1]];
Grid[z2, Frame -> All, ItemSize -> All]

enter image description here

Dataset[AssociationThread[First@z2, #] & /@ Rest@z2]

(credit to @creidhne from Better way to create a Dataset?)

enter image description here

Correct answer by theorist on July 24, 2021

You can use the command ArrayReshape

ArrayReshape[
  Table[{x, t, f[x, t]}, {x, 0, 1}, {t, 0, 2}], {6, 3}] // MatrixForm

which gives

Blockquote

Answered by DiSp0sablE_H3r0 on July 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP