# Documentation

## Best Practices for Vectors and Matrices in C Charts

### Perform Matrix Multiplication and Division Using MATLAB Functions

In a C chart, the multiplication and division operators perform element-wise multiplication and division. Use a MATLAB® function to perform standard matrix multiplication and division.

For example, suppose that you want to perform standard matrix operations on two square matrices during simulation. Follow these steps:

1. In your chart, add a MATLAB function with the following signature:

`[y1, y2, y3] = my_matrix_ops(u1, u2)`

2. Double-click the function box to open the editor.

3. In the editor, enter the code below.

```function [y1, y2, y3] = my_matrix_ops(u1, u2) %#codegen y1 = u1 * u2; % matrix multiplication y2 = u1 \ u2; % matrix division from the right y3 = u1 / u2; % matrix division from the left```

This function computes three values:

• `y1` is the product of two input matrices `u1` and `u2`.

• `y2` is the matrix that solves the equation `u1 * y2 = u2`.

• `y3` is the matrix that solves the equation `y3 * u1 = u2`.

4. Set properties for the input and output data.

1. Open the Model Explorer.

2. In the Model Hierarchy pane, navigate to the level of the MATLAB function.

3. In the Contents pane, set properties for each data object.

### Note

To initialize a matrix, see Define a Matrix.

### Index a Vector Using the temporalCount Operator

When you index a vector, you can use the `temporalCount` operator to avoid using an extra variable for the index counter. This indexing method works for vectors that contain real or complex data.

For example, suppose that you want to collect input data in a buffer during simulation. Follow these steps:

The state `Collect_Data` stores data in the vector `y`, which is of size `10`. The `entry` action assigns the value of input data `u` to the first element of `y`. The `during` action assigns the next nine values of input data to successive elements of the vector `y` until you store ten elements.

2. Add the input data `u` to the chart.

1. In the Stateflow® Editor, select Chart > Add Inputs & Outputs > Data Input From Simulink.

2. In the Data properties dialog box, enter `u` in the Name field.

3. Click .

3. Add the output data `y` to the chart.

1. In the Stateflow Editor, select Chart > Add Inputs & Outputs > Data Output To Simulink.

2. In the Data properties dialog box, enter `y` in the Name field.

3. Enter `10` in the Size field.

4. Click .

### Note

You do not need to set initial values for this output vector. By default, all elements initialize to 0.

For information about the `temporalCount` operator, see Control Chart Execution Using Temporal Logic.