Main Content

# zeros

## 语法

``X = zeros('like',p)``
``X = zeros(n,'like',p)``
``X = zeros(sz1,...,szN,'like',p)``
``X = zeros(sz,'like',p)``

## 说明

``X = zeros('like',p)` 返回标量 `0`，其 `numerictype`、复/实性（实数或复数）和 `fimath` 与 `p` 相同。`

``X = zeros(n,'like',p)` 返回一个 `n`×`n` 全零数组，类似于 `p`。`

``X = zeros(sz1,...,szN,'like',p)` 返回一个 `sz1`×....×`szN` 全零数组，类似于 `p`。`

``X = zeros(sz,'like',p)` 返回一个全零数组，类似于 `p`。大小向量 `sz` 定义了 `size(X)`。`

## 示例

`p = fi([],1,24,12);`

`X = zeros(2,3,'like',p)`
```X = 0 0 0 0 0 0 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 24 FractionLength: 12 ```

```A = [1 4 ; 2 5 ; 3 6]; sz = size(A)```
```sz = 1×2 3 2 ```

`p = fi([],1,24,12);`

`X = zeros(sz,'like',p)`
```X = 0 0 0 0 0 0 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 24 FractionLength: 12 ```

`p = fi([],1,24,12);`

`X = zeros(4, 'like', p)`
```X = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 24 FractionLength: 12 ```

`p = fi( [1+2i 3i],1,24,12);`

`X = zeros('like',p)`
```X = 0.0000 + 0.0000i DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 24 FractionLength: 12 ```

```function [y,z] = my_filter(b,a,x,z,T) % Cast the coefficients to the coefficient type b = cast(b,'like',T.coeffs); a = cast(a,'like',T.coeffs); % Create the output using zeros with the data type y = zeros(size(x),'like',T.data); for i = 1:length(x) y(i) = b(1)*x(i) + z(1); z(1) = b(2)*x(i) + z(2) - a(2) * y(i); z(2) = b(3)*x(i) - a(3) * y(i); end end ```

```function zeros_ones_cast_example % Define coefficients for a filter with specification % [b,a] = butter(2,0.25) b = [0.097631072937818 0.195262145875635 0.097631072937818]; a = [1.000000000000000 -0.942809041582063 0.333333333333333]; % Define floating-point types T_float.coeffs = double([]); T_float.data = double([]); % Create a step input using ones with the % floating-point data type t = 0:20; x_float = ones(size(t),'like',T_float.data); % Initialize the states using zeros with the % floating-point data type z_float = zeros(1,2,'like',T_float.data); % Run the floating-point algorithm y_float = my_filter(b,a,x_float,z_float,T_float); % Define fixed-point types T_fixed.coeffs = fi([],true,8,6); T_fixed.data = fi([],true,8,6); % Create a step input using ones with the % fixed-point data type x_fixed = ones(size(t),'like',T_fixed.data); % Initialize the states using zeros with the % fixed-point data type z_fixed = zeros(1,2,'like',T_fixed.data); % Run the fixed-point algorithm y_fixed = my_filter(b,a,x_fixed,z_fixed,T_fixed); % Compare the results coder.extrinsic('clf','subplot','plot','legend') clf subplot(211) plot(t,y_float,'co-',t,y_fixed,'kx-') legend('Floating-point output','Fixed-point output') title('Step response') subplot(212) plot(t,y_float - double(y_fixed),'rs-') legend('Error') figure(gcf) end```

## 输入参数

• 如果 `n` 为零，则 `X` 为空矩阵。

• 如果 `n` 为负，则它被视为零。

• 如果任一维度的大小为零，则 `X` 为空数组。

• 如果任一维度的大小为负，则它被视为零。

• 如果大于 2 的任何尾部维度的大小为 1，则输出 `X` 将不包括这些维度。

• 如果任一维度的大小为零，则 `X` 为空数组。

• 如果任一维度的大小为负，则它被视为零。

• 如果大于 2 的任何尾部维度的大小为 1，则输出 `X` 将不包括这些维度。

## 提示

• 重用具有不同数据类型的算法代码。

• 使用数据类型设定和针对不同数据类型的 switch 语句来保持算法的简洁性。

• 提高算法代码的可读性。

• 在定点和浮点数据类型之间切换以比较基线。

• 在不更改算法代码的情况下，在不同定点设置之间切换。