Main Content

Operations for Vectors and Matrices in Stateflow

Stateflow® charts in Simulink® models have an action language property that defines the syntax that you use to compute with vectors and matrices. The action language properties are:

  • MATLAB® as the action language.

  • C as the action language.

For more information, see Differences Between MATLAB and C as Action Language Syntax.

Indexing Notation

The way you access vector and matrix elements depends on your chart's action language.

Charts that use MATLAB as the action language use one-based indexing with parentheses. For multidimensional arrays, separate indices with commas inside a single pair of parentheses, M(i,j).

Charts that use C as the action language use zero-based indexing with brackets. For multidimensional arrays, enclose each dimension index in its own pair of brackets, M[i][j].

Example

MATLAB as the Action Language

C as the Action Language
The first element of a vector VV(1)V[0]
The ith element of a vector VV(i)V[i-1]
The element in row 4 and column 5 of a matrix MM(4,5)M[3][4]
The element in row i and column j of a matrix MM(i,j)M[i-1][j-1]

Binary Operations

Binary operations on vectors and matrices follow standard precedence rules (1 = highest, 3 = lowest). When operators have the same precedence, they evaluate from left to right.

All binary operators perform element-wise operations, with one exception: in charts that use MATLAB as the action language, the *, /, and \ operators perform matrix operations instead.

Multiplication and Division Operations (Precedence 1)

Operation

MATLAB as the Action Language

C as the Action Language

a * b

Matrix multiplication.

Element-wise multiplication. For matrix multiplication, use the * operation in a MATLAB function.

a .* b

Element-wise multiplication.

Not supported. Use the operation a * b.

a / b

Matrix right division.

Element-wise right division. For matrix right division, use the / operation in a MATLAB function.

a ./ b

Element-wise right division.

Not supported. Use the operation a / b.

a \ b

Matrix left division.

Not supported. Use the \ operation in a MATLAB function.

a .\ b

Element-wise left division.

Not supported. Use the .\ operation in a MATLAB function.

In charts that use C as the action language, the operations * and / perform element-wise multiplication and division. To perform standard matrix multiplication and division in a C chart, use a MATLAB function.

Suppose that you want to perform these operations on the square matrices u1 and u2:

  • Compute the standard matrix product y1 = u1 * u2.

  • Solve the equation u1 * y2 = u2.

  • Solve the equation y3 * u1 = u2.

To complete these calculations in a C chart, add a MATLAB function that runs this code:

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
Before calling the function, specify the properties for the input and output data, as described in Set Data Properties.

In charts that use MATLAB as the action language, the operations *, /, and \ perform standard matrix multiplication and division. You can use these operations directly in state and transition actions.

Addition and Subtraction Operations (Precedence 2)

Operation

MATLAB as the Action Language

C as the Action Language

a + b

Addition.

Addition.

a - b

Subtraction.

Subtraction.

Comparison Operations (Precedence 3)

Operation

MATLAB as the Action Language

C as the Action Language

a == b

Comparison, equal to.

Comparison, equal to.

a ~= b

Comparison, not equal to.

Comparison, not equal to.

a != b

Not supported. Use the operation a ~= b.

Comparison, not equal to.

a <> b

Not supported. Use the operation a ~= b.

Comparison, not equal to.

Unary Operations and Actions

Unary operations apply to a single value and behave differently from binary operations. Unary operations:

  • Have higher precedence than the binary operators.

  • Are right associative. In other words, the chart evaluates them from right to left.

  • Perform element-wise operations.

Operation

MATLAB as the Action Language

C as the Action Language

~a

Logical NOT. For bitwise NOT, use the bitcmp function.

  • Bitwise NOT (default). Enable this operation by selecting the Enable C-bit operations chart property.

  • Logical NOT. Enable this operation by clearing the Enable C-bit operations chart property.

For more information, see Bitwise Operations and Enable C-bit operations.

!a

Not supported. Use the operation ~a.

Logical NOT.

-a

Negative.

Negative.

a++

Not supported.

Increment all elements of the vector or matrix. Equivalent to a = a+1.

a--

Not supported.

Decrement all elements of the vector or matrix. Equivalent to a = a-1.

Assignment Operations

Assignment operations store values in variables. Charts that use C as the action language support compound assignment operators that combine arithmetic operations with assignment.

Operation

MATLAB as the Action Language

C as the Action Language

a = b

Simple assignment.

Simple assignment.

a += b

Not supported. Use the expression a = a+b.

Equivalent to a = a+b.

a -= b

Not supported. Use the expression a = a-b.

Equivalent to a = a-b.

a *= b

Not supported. Use the expression a = a*b.

Equivalent to a = a*b.

a /= b

Not supported. Use the expression a = a/b.

Equivalent to a = a/b.

Assign Values to Individual Elements of a Matrix

You can assign a value to an individual entry of a vector or matrix by using the indexing syntax appropriate to the action language of the chart.

Example

MATLAB as the Action Language

C as the Action Language
Assign the value 10 to the first element of the vector V.V(1) = 10;V[0] = 10;
Assign the value 77 to the element in row 2 and column 9 of the matrix M.M(2,9) = 77;M[1][8] = 77;

Assign Values to All Elements

In charts that use MATLAB as the action language, you can use a single action to specify all of the elements of a vector or matrix. For example, this action assigns each element of the 2-by-3 matrix A to a different value:

A = [1 2 3; 4 5 6];

In charts that use C as the action language, you can use scalar expansion to set all of the elements of a vector or matrix to the same value. Scalar expansion converts scalar data to match the dimensions of vector or matrix data. For example, this action sets all of the elements of the matrix A to 10:

A = 10;

Scalar expansion automatically converts scalar values to match vector or matrix dimensions. This applies to graphical, truth table, MATLAB, and Simulink functions.

For a function y = f(u) with scalar formal arguments:

Output yInput uResult
ScalarScalarNo scalar expansion.
ScalarVector or matrixSize mismatch error.
Vector or matrixScalar

Each element of y receives the scalar output y[i][j] = f(u)

Vector or matrixVector or matrix

Element-wise operation y[i][j] = f(u[i][j]).

If y and u have different sizes, a size mismatch error occurs.

For functions with multiple outputs, the same rules apply unless the outputs and inputs are all vectors or matrices. In this case, the chart generates a size mismatch error and scalar expansion does not occur.

Only fixed-size matrices support scalar expansion.

Charts that use MATLAB as the action language do not support scalar expansion.

See Also

Topics