Main Content

ones

用定点属性创建全为 1 的数组

说明

示例

X = ones('like',p) 返回标量 1,其 numerictype、复/实性(实数或复数)和 fimathp 相同。

示例

X = ones(n,'like',p) 返回类似 p 的由 1 组成的 n×n 数组。

示例

X = ones(sz1,...,szN,'like',p) 返回类似 p 的由 1 组成的 sz1×...×szN 数组。

示例

X = ones(sz,'like',p) 返回类似 p 的由 1 组成的数组。大小向量 sz 定义了 size(X)

示例

全部折叠

创建一个具有指定的 numerictype 和 fimath 属性的由 1 组成的 2×3 数组。

创建一个有符号 fi 对象,其字长为 24,小数长度为 12

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

创建一个由 1 组成的 2×3 数组,它与 p 具有相同的 numerictype 属性。

X = ones(2,3,'like',p)
X = 
     1     1     1
     1     1     1

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 24
        FractionLength: 12

定义一个 3×2 数组 A

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

sz = size(A)
sz = 1×2

     3     2

创建一个有符号 fi 对象,其字长为 24,小数长度为 12

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

创建一个由 1 组成的数组,其大小与 A 相同,具有与 p 相同的 numerictype 属性。

X = ones(sz,'like',p)
X = 
     1     1
     1     1
     1     1

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 24
        FractionLength: 12

创建一个具有指定的 numerictype 和 fimath 属性的由 1 组成的 4×4 数组。

创建一个有符号 fi 对象,其字长为 24,小数长度为 12

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

创建一个由 1 组成的 4×4 数组,与 p 具有相同的 numerictype 属性。

X = ones(4, 'like', p)
X = 
     1     1     1     1
     1     1     1     1
     1     1     1     1
     1     1     1     1

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 24
        FractionLength: 12

创建一个有符号 fi 对象,其字长为 16,小数长度为 15,并且 OverflowAction 设置为 Wrap

format long
p = fi([],1,16,15,'OverflowAction','Wrap');

创建一个与 p 具有相同 numerictype 属性的由 1 组成的 2×2 数组。

X = ones(2,'like',p)
X = 
   0.999969482421875   0.999969482421875
   0.999969482421875   0.999969482421875

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 15

        RoundingMethod: Nearest
        OverflowAction: Wrap
           ProductMode: FullPrecision
               SumMode: FullPrecision

1 无法用数据类型 p 表示,因此值会进行饱和处理。输出 fi 对象 Xp 具有相同的 numerictypefimath 属性。

创建一个标量定点 1,它不是实数值,而是类似于现有数组的复数。

定义一个复数 fi 对象。

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

创建一个标量 1,它是类似于 p 的复数。

X = ones('like',p)
X = 
   1.0000 + 0.0000i

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 24
        FractionLength: 12

编写一个 MATLAB® 算法,该算法支持使用不同数据类型运行,而无需更改算法本身。要重用算法,请将数据类型与该算法分开定义。

通过这种方法,您可以在使用浮点数据类型的情况下运行算法来定义基线。然后,您可以在使用不同定点数据类型的情况下测试算法,并将定点行为与基线进行比较,而无需对原始 MATLAB 代码进行任何修改。

编写一个 MATLAB 函数 my_filter,其输入参数为 T,它是用来定义系数和输入和输出数据的数据类型的一个结构体。

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

编写一个 MATLAB 函数 zeros_ones_cast_example,用浮点步长输入和定点阶跃输入调用 my_filter,然后比较结果。

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×n 的全 1 矩阵)。

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

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

数据类型: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

每个维度的大小(指定为两个或多个整数值)将 X 定义为一个 sz1×...×szN 数组。

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

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

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

数据类型: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

输出大小,指定为由整数值组成的行向量。此向量的每个元素都表示对应维度的大小。

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

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

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

示例: sz = [2,3,4]X 定义为 2×3×4 数组。

数据类型: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

原型,指定为 fi 对象或数值变量。要使用原型指定复数对象,必须为原型指定值。否则,您不需要指定值。

如果值 1 溢出 p 的数值类型,则无论关联的 fimath 的指定 OverflowAction 属性如何,输出都会进行饱和处理。对输出执行的所有后续操作都遵守关联的 fimath 的规则。

复数支持:是

提示

通过使用 b = cast(a,'like',p) 语法指定独立于算法代码的数据类型,您可以:

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

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

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

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

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

版本历史记录

在 R2013a 中推出