# `table`

Create a table

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

## Syntax

```table()
table(`index1 = entry1, index2 = entry2, …`, <`default`>)
table(`<list>, <set>, <tab>, …`, <`default`>)
```

## Description

`table()` creates a new empty table.

`table(index1 = entry1, index2 = entry2, ...)` creates a new table with the given indices and entries.

In MuPAD®, tables are the most flexible objects for storing data. In contrast to arrays or lists, arbitrary MuPAD objects can be used as indices. Indexed access to table entries is fast and nearly independent of the size of the table. Thus, tables are suitable containers for large data.

For a table`T`, say, an indexed call `T[index]` returns the corresponding entry. If no such entry exists, the default value of the table is returned, if the table has one. If no default value has been set and, the indexed expression `T[index]` is returned symbolically.

An indexed assignment of the form `T[index] := entry` adds a new entry to an existing table `T` or overwrites an existing entry associated with the index.

`table` can be used to create tables from other tables, lists or sets of equations. Cf. Example 2.

`table` is used for the explicit creation of a table. There also is the following mechanism for creating a table implicitly.

If the value of an identifier `T`, say, is neither a table nor an array nor an hfarray nor a list, then an indexed assignment ```T[index] := entry``` is equivalent to `T := table(index = entry)`. I.e., implicitly, a new table with one entry is created. Cf. Example 3.

If the value of `T` was either a table or an array or an hfarray or a list, then the indexed assignment only inserts a new entry without changing the type of `T` implicitly.

Table entries can be deleted with the function `delete`. Cf. Example 4.

## Examples

### Example 1

The following call creates a table with two entries:

`T := table(a = 13, c = 42)`
` `

The data may be accessed via indexed calls. Note the symbolic result for the index `b` which does not have a corresponding entry in the table:

`T[a], T[b], T[c]`
` `

Entries of a table may be changed via indexed assignments:

`T[a] := T[a] + 10: T`
` `

Expression sequences may be used as indices or entries, respectively. Note, however, that they have to be enclosed in brackets when using them as input parameters for `table`:

`T := table((a, b) = "hello", a + b = (50, 70))`
` `
`T[a + b]`
` `

Indexed access does not require additional brackets:

`T[a, b] := T[a, b]." world": T`
` `
`delete T:`

### Example 2

A `table` can be created from other tables, lists or sets:

```table(table(a = 1, b = 2), {a = 3, c = 4}, [b = 5, e = 6])```
` `

Please note that a set has no order of operands. When a set contains several values under the same index, the table entry is chosen “randomly”:

```table({a = 3, a = 4}); table({a = 4, a = 3})```
` `
` `

### Example 3

Below, a new table is created implicitly by an indexed assignment using an identifier `T` without a value:

`delete T: T := 7: T`
` `
`delete T:`

### Example 4

Use `delete` to delete entries:

`T := table(a = 1, b = 2, (a, b) = (1, 2))`
` `
`delete T[b], T[a, b]: T`
` `
`delete T:`

### Example 5

One of the uses of tables is to count the number of occurrences of some objects. In this situation, an implementation not using default values would have to look like this:

```T := table(): L := [1,2,3,a,b,c,a,b,a]: for i in L do if contains(T, i) then T[i] := T[i] + 1; else T[i] := 1; end_if; end_for: T```
` `

Note the test whether `T[i]` has already been set. If it has not, we cannot use its previous value, because that would remain symbolic:

```T := table(): T[a] := T[a] + 1: T```
` `

By creating `T` as `table(0)` instead of `table()`, we can tell MuPAD to regard `T[i]` as `0` if it has not been told anything else and the code from above becomes substantially shorter and, much more important, much easier to read:

```T := table(0): L := [1,2,3,a,b,c,a,b,a]: for i in L do T[i] := T[i] + 1; end_for: T```
` `

A slightly more complicated version counting all identifiers in an expression:

```ex := sin(a*x+b)-cos(c+x): cnt := table(0): misc::maprec(ex, {DOM_IDENT} = (x -> (cnt[x] := cnt[x]+1; x))): cnt```
` `

## Parameters

 ```index1, index2, …``` The indices: arbitrary MuPAD objects ```entry1, entry2, …``` The corresponding entries: arbitrary MuPAD objects `list` A list of equations `set` A set of equations `tab` A table `default` The default value: A MuPAD object which is not an equation, a list, a set, nor a table

## Return Values

Object of type `DOM_TABLE`.

#### Mathematical Modeling with Symbolic Math Toolbox

Get examples and videos