main Content

金宝app向量和矩阵的支持操作

状态流®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®作为动作语言。

  • C作为动作语言。

For more information, seeMATLAB和C作为动作语言语法之间的差异

索引符号

在使用MATLAB作为动作语言的图表中,请参考向量或矩阵的元素通过使用括号界定的一个基于索引的索引。与逗号不同维度的单独索引。

In charts that use C as the action language, refer to elements of a vector or matrix by using zero-based indexing delimited by brackets. Enclose indices for different dimensions in their own pair of brackets.

例子

MATLABas the Action Language

C作为动作语言
向量的第一个元素v v(1) v[0]
一世Thelement of a vectorv v(i) v[i-1]
连续元素4和列5of a matrixm M(4,5) M [3] [4]
连续元素一世和列jof a matrixm m(i,j) M [I-1] [J-1]

Binary Operations

该表总结了所有二进制操作对向量和矩阵操作数的解释,并根据其优先顺序(1 =最高,3 =最低)。二进制操作是关联的,因此,在任何表达式中,从左到右评估具有相同优先级的运算符。除图表中使用MATLAB作为动作语言的矩阵乘法和除法运算符外,所有二进制运算符还执行元素操作。

Operation

优先

MATLABas the Action Language

C作为动作语言

A * b

1

matrix multiplication.

Element-wise multiplication. For matrix multiplication, use the*operation in a MATLAB function. See通过使用MATLAB职能

a。* b

1

Element-wise multiplication.

不支持。金宝app使用操作A * b

A / b

1

matrix right division.

元素右部门。For matrix right division, use the/operation in a MATLAB function. See通过使用MATLAB职能

a ./ b

1

元素右部门。

不支持。金宝app使用操作A / b

a \ b

1

矩阵左师。

不支持。金宝appUse the\ \operation in a MATLAB function. See通过使用MATLAB职能

a。\ b

1

Element-wise left division.

不支持。金宝appUse the。\ \operation in a MATLAB function. See通过使用MATLAB职能

a + b

2

一种ddition.

一种ddition.

a - b

2

Subtraction.

Subtraction.

a == b

3

比较,等于。

比较,等于。

a ~= b

3

比较,不等于。

比较,不等于。

a!= b

3

不支持。金宝app使用操作a ~= b

比较,不等于。

a <> b

3

不支持。金宝app使用操作a ~= b

比较,不等于。

一般操作和行动

This table summarizes the interpretation of all unary operations and actions on vector and matrix operands. Unary operations:

  • Have higher precedence than the binary operators.

  • 一种re right associative so that, in any expression, they are evaluated from right to left.

  • Perform element-wise operations.

例子

MATLABas the Action Language

C作为动作语言

~a

逻辑不是。对于钻头,请使用BITCMP功能。

  • Bitwise NOT (default). Enable this operation by selecting the启用C-BIT操作chart property.

  • 逻辑不是。通过清除启用C-BIT操作chart property.

For more information, seeBitwise Operationsand启用C-BIT操作

!一种

不支持。金宝app使用操作~a

逻辑不是。

-a

消极的。

消极的。

a++

不支持。金宝app

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

a--

不支持。金宝app

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

一种ssignment Operations

该表总结了对向量和矩阵操作数的分配操作的解释。

Operation

MATLABas the Action Language

C作为动作语言

a = b

简单的作业。

简单的作业。

A += B

不支持。金宝appUse the expressiona = a+b

Equivalent toa = a+b

A- = B

不支持。金宝appUse the expressiona = a-b

Equivalent toa = a-b

A *= B

不支持。金宝appUse the expressiona = a*b

Equivalent toa = a*b

A /= B

不支持。金宝appUse the expressiona = a/b

Equivalent toa = a/b

将值分配给矩阵的单个元素

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.

例子

MATLABas the Action Language

C作为动作语言
一种ssign the value10到向量的第一个元素v V(1)= 10; v[0] = 10;
一种ssign the value 77 to the element in row 2 and column 9 of the matrixm m(2,9) = 77; m[1][8] = 77;

一种ssign Values to All Elements of a Matrix

在使用MATLAB作为动作语言的图表中,您可以使用单个操作来指定向量或矩阵的所有元素。例如,此操作分配了2 by-3矩阵的每个元素一种到a different value:

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

In charts that use C as the action language, you can use标量扩展到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一种10

a = 10;

Scalar expansion applies to all graphical, truth table, MATLAB, and Simulink functions. Suppose that you define the formal arguments of a functionF作为标量。该表描述了函数调用的标量扩展规则y= f(u)

输出y Input Result
Scalar Scalar 没有标量扩展。
Scalar 向量或矩阵 该图生成大小不匹配错误。
向量或矩阵 Scalar

该图使用标量扩展来分配标量输出值f(u)到每个元素y

y[i][j] = f(u)

向量或矩阵 向量或矩阵

该图表使用标量扩展来计算每个元素的输出值并将其分配给y

y [i] [j] = f(u [i] [j])
Ifyand没有相同的大小,图表会产生大小不匹配错误。

对于具有多个输出的函数,除非输出和输入都是向量或矩阵,否则适用相同的规则。在这种情况下,图表会产生大小不匹配误差,并且不会发生标量扩展。

使用MATLAB作为动作语言的图表不支持标量扩展。金宝app

通过使用MATLAB职能

在使用C作为动作语言的图表中*and/执行元素乘法和除法。要在C图表中执行标准矩阵乘法和除法,请使用MATLAB函数。

假设您想在方形矩阵上执行这些操作U1andU2

  • 计算标准矩阵产品y1 = u1 * u2

  • 解决方程U1*y2= u2

  • 解决方程y3*U1= u2

要在C图表中完成这些计算,请添加一个运行此代码的MATLAB函数:

F你nction[y1, y2, y3] = my_matrix_ops(u1, u2)%#codegeny1 = u1 * u2;%矩阵乘法y2= u1 \ u2;右边的%矩阵部门Y3 = U1 / U2;% matrix division from the left
在调用该函数之前,请指定输入和输出数据的属性,如图所述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.

Related Topics