Matlab-numbers
MATLAB-数値
MATLABは、符号付きおよび符号なし整数、単精度および倍精度の浮動小数点数を含むさまざまな数値クラスをサポートしています。 デフォルトでは、MATLABはすべての数値を倍精度浮動小数点数として保存します。
任意の数または数の配列を整数または単精度数として保存することを選択できます。
すべての数値型は、基本的な配列演算と数学演算をサポートしています。
さまざまな数値データ型への変換
MATLABは、さまざまな数値データ型に変換するために次の関数を提供します-
Function | Purpose |
---|---|
double | Converts to double precision number |
single | Converts to single precision number |
int8 | Converts to 8-bit signed integer |
int16 | Converts to 16-bit signed integer |
int32 | Converts to 32-bit signed integer |
int64 | Converts to 64-bit signed integer |
uint8 | Converts to 8-bit unsigned integer |
uint16 | Converts to 16-bit unsigned integer |
uint32 | Converts to 32-bit unsigned integer |
uint64 | Converts to 64-bit unsigned integer |
例
スクリプトファイルを作成し、次のコードを入力します-
x = single([5.32 3.47 6.28]) .* 7.5
x = double([5.32 3.47 6.28]) .* 7.5
x = int8([5.32 3.47 6.28]) .* 7.5
x = int16([5.32 3.47 6.28]) .* 7.5
x = int32([5.32 3.47 6.28]) .* 7.5
x = int64([5.32 3.47 6.28]) .* 7.5
ファイルを実行すると、次の結果が表示されます-
x =
39.900 26.025 47.100
x =
39.900 26.025 47.100
x =
38 23 45
x =
38 23 45
x =
38 23 45
x =
38 23 45
例
前の例をもう少し拡張しましょう。 スクリプトファイルを作成し、次のコードを入力します-
x = int32([5.32 3.47 6.28]) .* 7.5
x = int64([5.32 3.47 6.28]) .* 7.5
x = num2cell(x)
ファイルを実行すると、次の結果が表示されます-
x =
38 23 45
x =
38 23 45
x =
{
[1,1] = 38
[1,2] = 23
[1,3] = 45
}
最小および最大の整数
関数* intmax()および intmin()*は、すべてのタイプの整数で表現できる最大値と最小値を返します。
どちらの関数も、intmax(int8)やintmin(int64)などの整数データ型を引数として受け取り、整数データ型で表すことができる最大値と最小値を返します。
例
次の例は、整数の最小値と最大値を取得する方法を示しています。 スクリプトファイルを作成し、その中に次のコードを書きます-
% displaying the smallest and largest signed integer data
str = 'The range for int8 is:\n\t%d to %d ';
sprintf(str, intmin('int8'), intmax('int8'))
str = 'The range for int16 is:\n\t%d to %d ';
sprintf(str, intmin('int16'), intmax('int16'))
str = 'The range for int32 is:\n\t%d to %d ';
sprintf(str, intmin('int32'), intmax('int32'))
str = 'The range for int64 is:\n\t%d to %d ';
sprintf(str, intmin('int64'), intmax('int64'))
% displaying the smallest and largest unsigned integer data
str = 'The range for uint8 is:\n\t%d to %d ';
sprintf(str, intmin('uint8'), intmax('uint8'))
str = 'The range for uint16 is:\n\t%d to %d ';
sprintf(str, intmin('uint16'), intmax('uint16'))
str = 'The range for uint32 is:\n\t%d to %d ';
sprintf(str, intmin('uint32'), intmax('uint32'))
str = 'The range for uint64 is:\n\t%d to %d ';
sprintf(str, intmin('uint64'), intmax('uint64'))
ファイルを実行すると、次の結果が表示されます-
ans = The range for int8 is:
-128 to 127
ans = The range for int16 is:
-32768 to 32767
ans = The range for int32 is:
-2147483648 to 2147483647
ans = The range for int64 is:
0 to 0
ans = The range for uint8 is:
0 to 255
ans = The range for uint16 is:
0 to 65535
ans = The range for uint32 is:
0 to -1
ans = The range for uint64 is:
0 to 18446744073709551616
最小および最大の浮動小数点数
関数* realmax()および realmin()*は、浮動小数点数で表現できる最大値と最小値を返します。
両方の関数は、引数 'single’で呼び出されると、単精度データ型で表現できる最大値と最小値を返し、引数 'double’で呼び出されると、表現できる最大値と最小値を返します。倍精度データ型。
例
次の例は、最小および最大の浮動小数点数を取得する方法を示しています。 スクリプトファイルを作成し、その中に次のコードを書きます-
% displaying the smallest and largest single-precision
% floating point number
str = 'The range for single is:\n\t%g to %g and\n\t %g to %g';
sprintf(str, -realmax('single'), -realmin('single'), ...
realmin('single'), realmax('single'))
% displaying the smallest and largest double-precision
% floating point number
str = 'The range for double is:\n\t%g to %g and\n\t %g to %g';
sprintf(str, -realmax('double'), -realmin('double'), ...
realmin('double'), realmax('double'))
あなたがファイルを実行すると、次の結果が表示されます-
ans = The range for single is:
-3.40282e+38 to -1.17549e-38 and
1.17549e-38 to 3.40282e+38
ans = The range for double is:
-1.79769e+308 to -2.22507e-308 and
2.22507e-308 to 1.79769e+308