Main Content

Direct Calculations on Tables and Timetables

Since R2023a

You can perform calculations directly on tables and timetables without extracting their data by indexing. To perform direct calculations with the same syntaxes used for arrays, your tables and timetables must meet several conditions:

  • All variables of your tables and timetables must have data types that support calculations.

  • If you perform an operation where only one operand is a table or timetable, then the other operand must be a numeric or logical array.

  • If you perform an operation where both operands are tables or timetables, then they must have compatible sizes.

This example shows how perform operations without indexing into your tables and timetables. You can also call common mathematical and statistical functions, such as sum, mean, and cumsum. This example also shows how to perform operations on tables and timetables when their rows and variables are in different orders but have matching names (or, in the case of timetables, matching row times). For a complete list of supported functions and operations, as well as related rules for their use, see Rules for Table and Timetable Mathematics.

Before R2023a, or for tables and timetables that have a mix of numeric and nonnumeric variables, see Calculations When Tables Have Both Numeric and Nonnumeric Data.

Multiply Table by Scale Factors

A simple arithmetic operation is to scale a table by a constant. If all your table variables support multiplication, then you can scale your table without extracting data from it.

For example, read data from a CSV (comma-separated values) file, testScoresNumeric.csv, into a table by using the readtable function. The sample file contains 10 test scores for each of three tests.

testScores = readtable("testScoresNumeric.csv")
testScores=10×3 table
    Test1    Test2    Test3
    _____    _____    _____

     90       87       93  
     87       85       83  
     86       85       88  
     75       80       72  
     89       86       87  
     96       92       98  
     78       75       77  
     91       94       92  
     86       83       85  
     79       76       82  

The test scores are based on a 100-point scale. To convert them to scores on a 25-point scale, multiply the table by 0.25. To multiply tables and timetables, use the times operator, .*.

scaledScores = testScores .* 0.25
scaledScores=10×3 table
    Test1    Test2    Test3
    _____    _____    _____

     22.5    21.75    23.25
    21.75    21.25    20.75
     21.5    21.25       22
    18.75       20       18
    22.25     21.5    21.75
       24       23     24.5
     19.5    18.75    19.25
    22.75     23.5       23
     21.5    20.75    21.25
    19.75       19     20.5

If different tests have different scales, then you can multiply the table by a vector. When you perform operations where one operand is a table or timetable, then the other operand must be a scalar, vector, matrix, table, or timetable that has a compatible size.

For example, use a row vector to weight each test by a different factor.

weightedScores = testScores .* [0.2 0.3 0.5]
weightedScores=10×3 table
    Test1    Test2    Test3
    _____    _____    _____

      18     26.1     46.5 
    17.4     25.5     41.5 
    17.2     25.5       44 
      15       24       36 
    17.8     25.8     43.5 
    19.2     27.6       49 
    15.6     22.5     38.5 
    18.2     28.2       46 
    17.2     24.9     42.5 
    15.8     22.8       41 

Calculate Sum and Mean of Table

Tables also support common mathematical and statistical functions. For example, calculate the sum of the weighted test scores across each row of the table. To sum across rows, specify the second dimension of the table when you call sum.

sumScores = sum(weightedScores,2)
sumScores=10×1 table
    sum 
    ____

    90.6
    84.4
    86.7
      75
    87.1
    95.8
    76.6
    92.4
    84.6
    79.6

To calculate the mean score of each test, use the mean function. By default, mean calculates along the variables, the first dimension of the table.

meanScores = mean(weightedScores)
meanScores=1×3 table
    Test1    Test2    Test3
    _____    _____    _____

    17.14    25.29    42.85

Calculate Cumulative Sum of Timetable

Timetables support the same operations and mathematical and statistical functions that tables support.

For example, load a timetable that records the main shock amplitude of an earthquake over a period of 50 seconds, sampled at 200 Hz. The three timetable variables correspond to three directional components of the shockwave as measured by an accelerometer.

load quakeData
quakeData
quakeData=10001×3 timetable
      Time       EastWest    NorthSouth    Vertical
    _________    ________    __________    ________

    0.005 sec       5            3            0    
    0.01 sec        5            3            0    
    0.015 sec       5            2            0    
    0.02 sec        5            2            0    
    0.025 sec       5            2            0    
    0.03 sec        5            2            0    
    0.035 sec       5            1            0    
    0.04 sec        5            1            0    
    0.045 sec       5            1            0    
    0.05 sec        5            0            0    
    0.055 sec       5            0            0    
    0.06 sec        5            0            0    
    0.065 sec       5            0            0    
    0.07 sec        5            0            0    
    0.075 sec       5            0            0    
    0.08 sec        5            0            0    
      ⋮

Calculate the propagation speed of the shockwave. First, multiply the timetable by the gravitational acceleration.

quakeData = 0.098 .* quakeData
quakeData=10001×3 timetable
      Time       EastWest    NorthSouth    Vertical
    _________    ________    __________    ________

    0.005 sec      0.49        0.294          0    
    0.01 sec       0.49        0.294          0    
    0.015 sec      0.49        0.196          0    
    0.02 sec       0.49        0.196          0    
    0.025 sec      0.49        0.196          0    
    0.03 sec       0.49        0.196          0    
    0.035 sec      0.49        0.098          0    
    0.04 sec       0.49        0.098          0    
    0.045 sec      0.49        0.098          0    
    0.05 sec       0.49            0          0    
    0.055 sec      0.49            0          0    
    0.06 sec       0.49            0          0    
    0.065 sec      0.49            0          0    
    0.07 sec       0.49            0          0    
    0.075 sec      0.49            0          0    
    0.08 sec       0.49            0          0    
      ⋮

Then calculate the propagation speed by integrating the acceleration data. You can approximate the integration by calculating the cumulative sum of each variable. Scale the cumulative sums by the time step of the timetable. The cumsum function returns a timetable that has the same size and the same row times as the input.

speedQuake = (1/200) .* cumsum(quakeData)
speedQuake=10001×3 timetable
      Time       EastWest    NorthSouth    Vertical
    _________    ________    __________    ________

    0.005 sec    0.00245      0.00147         0    
    0.01 sec      0.0049      0.00294         0    
    0.015 sec    0.00735      0.00392         0    
    0.02 sec      0.0098       0.0049         0    
    0.025 sec    0.01225      0.00588         0    
    0.03 sec      0.0147      0.00686         0    
    0.035 sec    0.01715      0.00735         0    
    0.04 sec      0.0196      0.00784         0    
    0.045 sec    0.02205      0.00833         0    
    0.05 sec      0.0245      0.00833         0    
    0.055 sec    0.02695      0.00833         0    
    0.06 sec      0.0294      0.00833         0    
    0.065 sec    0.03185      0.00833         0    
    0.07 sec      0.0343      0.00833         0    
    0.075 sec    0.03675      0.00833         0    
    0.08 sec      0.0392      0.00833         0    
      ⋮

Calculate the means of the scaled cumulative sums. The mean function returns the output as a one-row table.

meanQuake = mean(speedQuake)
meanQuake=1×3 table
    EastWest    NorthSouth    Vertical
    ________    __________    ________

     4.6145       -11.51      -7.2437 

Center the scaled cumulative sums by subtracting the means. The output is a timetable with the propagation speeds for each component.

speedQuake = speedQuake - meanQuake
speedQuake=10001×3 timetable
      Time       EastWest    NorthSouth    Vertical
    _________    ________    __________    ________

    0.005 sec    -4.6121       11.511       7.2437 
    0.01 sec     -4.6096       11.513       7.2437 
    0.015 sec    -4.6072       11.514       7.2437 
    0.02 sec     -4.6047       11.515       7.2437 
    0.025 sec    -4.6023       11.516       7.2437 
    0.03 sec     -4.5998       11.517       7.2437 
    0.035 sec    -4.5974       11.517       7.2437 
    0.04 sec     -4.5949       11.518       7.2437 
    0.045 sec    -4.5925       11.518       7.2437 
    0.05 sec       -4.59       11.518       7.2437 
    0.055 sec    -4.5876       11.518       7.2437 
    0.06 sec     -4.5851       11.518       7.2437 
    0.065 sec    -4.5827       11.518       7.2437 
    0.07 sec     -4.5802       11.518       7.2437 
    0.075 sec    -4.5778       11.518       7.2437 
    0.08 sec     -4.5753       11.518       7.2437 
      ⋮

Operations with Rows and Variables in Different Orders

Tables and timetables have variables, and the variables have names. Table rows can also have row names. And timetable rows always have row times. When operating on two tables or timetables, their variables and rows must meet these conditions:

  • Both operands must have the same size, or one of them must be a one-row table.

  • Both operands must have variables with the same names. However, the variables in each operand can be in a different order.

  • If both operands are tables and have row names, then their row names must be the same. However, the row names in each operand can be in a different order.

  • If both operands are timetables, then their row times must be the same. However, the row times in each operand can be in a different order.

For example, create two tables and add them. These tables have variable names but no row names. The variables are in the same order.

A = table([1;2],[3;4],VariableNames=["V1","V2"])
A=2×2 table
    V1    V2
    __    __

    1     3 
    2     4 

B = table([1;3],[2;4],VariableNames=["V1","V2"])
B=2×2 table
    V1    V2
    __    __

    1     2 
    3     4 

C = A + B
C=2×2 table
    V1    V2
    __    __

    2     5 
    5     8 

Now create two tables that have row names and variables in different orders.

A = table([1;2],[3;4],VariableNames=["V1","V2"],RowNames=["R1","R2"])
A=2×2 table
          V1    V2
          __    __

    R1    1     3 
    R2    2     4 

B = table([4;2],[3;1],VariableNames=["V2","V1"],RowNames=["R2","R1"])
B=2×2 table
          V2    V1
          __    __

    R2    4     3 
    R1    2     1 

Add the tables. The result is a table that has variables and rows in the same orders as the variables and rows of the first table in the expression.

C = A + B
C=2×2 table
          V1    V2
          __    __

    R1    2     5 
    R2    5     8 

Similarly, add two timetables. The result is a timetable with variables and row times in the same orders as in the first timetable.

A = timetable(seconds([15;30]),[1;2],[3;4],VariableNames=["V1","V2"])
A=2×2 timetable
     Time     V1    V2
    ______    __    __

    15 sec    1     3 
    30 sec    2     4 

B = timetable(seconds([30;15]),[4;2],[3;1],VariableNames=["V2","V1"])
B=2×2 timetable
     Time     V2    V1
    ______    __    __

    30 sec    4     3 
    15 sec    2     1 

C = A + B
C=2×2 timetable
     Time     V1    V2
    ______    __    __

    15 sec    2     5 
    30 sec    5     8 

See Also

| |

Related Topics