cast
変数を別のデータ型にキャスト
説明
例
int8 値を固定小数点に変換
8 ビット整数のスカラーを定義します。
a = int8(5);
語長が 24
で小数部の長さが 12
である符号付き fi
オブジェクトを作成します。
p = fi([],1,24,12);
指定された fi
オブジェクト p
の numerictype
、実数/複素数および fimath
をもつ固定小数点に a
を変換します。
b = cast(a, 'like', p)
b = 5 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 24 FractionLength: 12
固定小数点への配列の変換
1 の 2 行 3 列の行列を定義します。
A = ones(2,3);
語長が 16
で小数部の長さが 8
である符号付き fi
オブジェクトを作成します。
p = fi([],1,16,8);
A
を p
と同じデータ型および実数/複素数に変換します。
B = cast(A,'like',p)
B = 1 1 1 1 1 1 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 8
データ型に依存しない MATLAB コードの作成
アルゴリズムそのものを変更しなくてもさまざまなデータ型で実行できるような 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
入力引数
a
— 別のデータ型にキャストする変数
fi
オブジェクト | 数値変数
fi
オブジェクトまたは数値変数として指定される変数。
複素数のサポート: あり
p
— プロトタイプ
fi
オブジェクト | 数値変数
fi
オブジェクトまたは数値変数として指定されるプロトタイプ。プロトタイプを使用して複素数オブジェクトを指定するには、プロトタイプの値を指定しなければなりません。それ以外の場合は、値を指定する必要はありません。
複素数のサポート: あり
ヒント
b = cast(a,'like',p)
構文を使用してアルゴリズム コードとは別にデータ型を指定すると、以下のことができます。
異なるデータ型でアルゴリズム コードを再利用
データ型指定を切り離してアルゴリズムを整理し、データ型ごとにステートメントを切り替え
アルゴリズム コードの可読性を向上
固定小数点データ型と浮動小数点データ型を切り替えてベースラインを比較
アルゴリズム コードを変更しないで固定小数点設定のバリエーションを切り替え
バージョン履歴
R2013a で導入
MATLAB コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは MATLAB コマンドをサポートしていません。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)