Main Content

ones

固定小数点プロパティをもつ、すべて 1 の配列を作成

説明

X = ones('like',p) は、p と同じ numerictype、実数/複素数および fimath をもつスカラー 1 を返します。

X = ones(n,'like',p) は、p と同様の nn 列の 1 の配列を返します。

X = ones(sz1,...,szN,'like',p) は、p と同様の sz1 x ... x szN の 1 の配列を返します。

X = ones(sz,'like',p) は、p と同様の 1 の配列を返します。サイズ ベクトル szsize(X) を定義します。

すべて折りたたむ

指定された numerictype および fimath プロパティをもつ、2 行 3 列の 1 の配列を作成します。

語長が 24 で小数部の長さが 12 である符号付き fi オブジェクトを作成します。

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

p と同じ numerictype プロパティをもつ、2 行 3 列の 1 の配列を作成します。

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

語長が 24 で小数部の長さが 12 である符号付き fi オブジェクトを作成します。

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

A と同じサイズで p と同じ numerictype プロパティをもつ、1 の配列を作成します。

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 プロパティをもつ、4 行 4 列の 1 の配列を作成します。

語長が 24 で小数部の長さが 12 である符号付き fi オブジェクトを作成します。

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

p と同じ numerictype プロパティをもつ、4 行 4 列の 1 の配列を作成します。

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

語長が 16、小数部の長さが 15 で OverflowActionWrap に設定された符号付き fi オブジェクトを作成します。

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

p と同じ numerictype プロパティをもつ 2 行 2 列の 1 の配列を作成します。

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

p のデータ型で 1 を表現することはできないため、値は飽和します。出力 fi オブジェクト X は、p と同じ numerictype および fimath プロパティをもちます。

実数値ではなく、既存の配列のような複素数のスカラー固定小数点の 1 を作成します。

複素数 fi オブジェクトを定義します。

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

p のような複素数の 1 のスカラーを作成します。

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 が 0 の場合、X は空の行列です。

  • n が負の場合は 0 として扱われます。

データ型: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

各次元のサイズを 2 つ以上の整数値として指定し、X を sz1 x ... x szN の配列として定義します。

  • 次元のサイズが 0 の場合、X は空の配列になります。

  • 次元のサイズが負の場合は 0 として扱われます。

  • 2 次元より大きい後続次元のサイズが 1 の場合、出力 X にそれらの次元は含まれません。

データ型: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

整数値の行ベクトルとして指定される出力サイズ。このベクトルの各要素は対応する次元のサイズを示します。

  • 次元のサイズが 0 の場合、X は空の配列になります。

  • 次元のサイズが負の場合は 0 として扱われます。

  • 2 次元より大きい後続次元のサイズが 1 の場合、出力 X にそれらの次元は含まれません。

例: sz = [2,3,4] は、X を 2 x 3 x 4 の配列として定義します。

データ型: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

fi オブジェクトまたは数値変数として指定されるプロトタイプ。プロトタイプを使用して複素数オブジェクトを指定するには、プロトタイプの値を指定しなければなりません。それ以外の場合は、値を指定する必要はありません。

値 1 によって p の数値型がオーバーフローする場合、付加された fimathOverflowAction プロパティの指定にかかわらず出力は飽和します。出力に対して実行される以降のすべての演算は、付加された fimath のルールに従います。

複素数のサポート: あり

ヒント

b = cast(a,'like',p) 構文を使用してアルゴリズム コードとは別にデータ型を指定すると、以下のことができます。

  • 異なるデータ型でアルゴリズム コードを再利用

  • データ型指定を切り離してアルゴリズムを整理し、データ型ごとにステートメントを切り替え

  • アルゴリズム コードの可読性を向上

  • 固定小数点データ型と浮動小数点データ型を切り替えてベースラインを比較

  • アルゴリズム コードを変更しないで固定小数点設定のバリエーションを切り替え

バージョン履歴

R2013a で導入