Using Table in Mathematica
约 213 字小于 1 分钟
Mathematica
2025-09-15
Table is one of the core tools in Mathematica for generating data and matrices. It can create both numeric matrices and symbolic matrices, which makes it very flexible.
1. Basic syntax
Table[expr, {i, imax}]expr: an expressioni: the loop variableimax: the upper limit of the loop
Example:
Table[i^2, {i, 5}]Output:
{1, 4, 9, 16, 25}2. Specify a starting value and step size
Table[expr, {i, imin, imax, step}]Example:
Table[2 i, {i, 1, 5, 2}]Output:
{2, 6, 10}3. Generate a 2D matrix
To generate an n × m matrix, nest two loops:
Table[a[i, j], {i, n}, {j, m}]Example:
Table[a[i, j], {i, 3}, {j, 3}] // MatrixForm4. Symbolic matrix example
n = 4;
A = Table[a[i, j], {i, n}, {j, n}]
A // MatrixFormWith Table, you can build symbolic matrices of almost any structure, including upper triangular, lower triangular, and diagonal matrices.
5. Conditional matrix generation
Table can be combined with If or other conditions:
Table[If[i == j, 1, 0], {i, 4}, {j, 4}] // MatrixFormThis generates a 4×4 identity matrix.
