Rules for Table and Timetable Mathematics - MATLAB & Simulink (original) (raw)

You can perform calculations directly on tables and timetables without indexing to extract their data. Most of the common functions and operators for mathematics and statistics support tables and timetables. Operations on tables and timetables have rules about data types and sizes as well as variable names, row names, row times, and variable units. Operations on tables and timetables whose variables have units also have rules for propagating those units into the output table or timetable.

An event table is a kind of timetable. So, you can also perform calculations directly on event tables. They follow the same rules that timetables follow. However, they have additional properties that specify event labels and event durations. These properties impose additional constraints when you perform operations on event tables. (since R2023b)

Functions and Operators That Support Tables and Timetables

These MATLAB® functions and operators support direct calculations on tables and timetables. You can use these functions and operators on your tables and timetables if all their variables have data types that support calculations. Operations that have two operands must also follow the rules listed in the next section. You do not have to index into your tables and timetables to extract arrays of data.

These functions and operators also support direct calculations on event tables. (since R2023b)

For example, these statements use arithmetic, relational, and logical operators as well as mathematics and statistics functions with operands that are tables, timetables, or event tables.

T = T .* 0.5; T = T1 + T2; T2 = T > 10; T2 = ~(T < 0); T2 = abs(T); T2 = mean(T);

Rules for Operations on Tables and Timetables

When performing operations on two operands, such as addition, directly on tables and timetables, follow these rules about data types, sizes, variables, and rows.

When only one operand is a table or timetable:

When both operands are tables or timetables:

Rules for Tables and Timetables with Units

Table and timetable variables have several properties, such as names, descriptions, and units. If you define units for any variables, then the units can affect operations where both operands are tables or timetables.

In general, these rules apply to operations on tables and timetables with variables that have units:

Operations and functions do not support unit conversions. For example, if a variable in one table has units of meters (m), and the corresponding variable in the other table has units of centimeters (cm), then the operation treats those units as incompatible units. The operation does not attempt to convert centimeters to meters.

This table shows rules for arithmetic operations when table or timetable variables have units that are the same (both A), different (A and B), or undefined (one unit is undefined, and the other is eitherA or B).

Arithmetic Operation Units in First Operand Units in Second Operand Units in Output
plusminus A A A
A B Errors
A Undefined A (warns)
Undefined A A (warns)
times A A Undefined
A B Undefined
A Undefined A (warns)
Undefined A A (warns)
rdivideldivide A A Undefined
A B Undefined
A Undefined A (warns)
Undefined B Undefined
power A A Errors
A B Errors
A Undefined Undefined
Undefined B Errors
remmod A A A
A B Errors
A Undefined A (warns)
Undefined B B (warns)

In general, logical operators and exponential, logarithmic, and trigonometric functions do not propagate units. However, some functions do propagate units when it is known that the output has the same units as the input. The functions that propagate units are:

Rules for Operations and Functions on Event Tables

Since R2023b

Because an event table is a kind of timetable, operations on event tables follow the rules for operations on timetables. But because event tables have additional properties, operations on event tables have additional constraints.

When both operands are event tables:

When one operand is an event table, and the other is a timetable or a table:

When an operation has only one operand:

When a function reduces an event table along a dimension:

See Also

table | timetable | eventtable