MATLAB 教程
- 第1节:MATLAB 介绍
- 第2节:MATLAB 安装
- 第3节:MATLAB 基本语法
- 第4节:MATLAB 变量
- 第5节:MATLAB 命令
- 第6节:MATLAB M-Files
- 第7节:MATLAB 数据类型
- 第8节:MATLAB 运算符
- 第9节:MATLAB 算术运算
- 第10节:MATLAB 逻辑运算
- 第11节:MATLAB 关系运算
- 第12节:MATLAB 位运算
- 第13节:MATLAB 集合操作
- 第14节:MATLAB 决策制定
- 第15节:MATLAB if...end 语句
- 第16节:MATLAB if...else...end 语句
- 第17节:MATLAB if...elseif...elseif...else...end 语句
- 第18节:MATLAB 嵌套if语句
- 第19节:MATLAB switch语句
- 第20节:MATLAB 嵌套switch语句
- 第21节:MATLAB 循环类型
- 第22节:MATLAB while循环
- 第23节:MATLAB for循环
- 第24节:MATLAB 嵌套循环
- 第25节:MATLAB break语句
- 第26节:MATLAB continue语句
- 第27节:MATLAB 向量
- 第28节:MATLAB 向量的加法和减法
- 第29节:MATLAB 标量向量乘法
- 第30节:MATLAB 转置向量
- 第31节:MATLAB 追加向量
- 第32节:MATLAB 向量的模
- 第33节:MATLAB 向量点积
- 第34节:MATLAB 等差元素向量
- 第35节:MATLAB 矩阵
- 第36节:MATLAB 矩阵的加法和减法
- 第37节:MATLAB 除法(左,右)矩阵
- 第38节:MATLAB 矩阵标量操作
- 第39节:MATLAB 矩阵的转置
- 第40节:MATLAB 串联矩阵
- 第41节:MATLAB 矩阵乘法
- 第42节:MATLAB 矩阵的行列式
- 第43节:MATLAB 逆矩阵
- 第44节:MATLAB 数组
- 第45节:MATLAB 冒号符号
- 第46节:MATLAB 数字
- 第47节:MATLAB 字符串
- 第48节:MATLAB 函数
- 第49节:MATLAB 数据导入
- 第50节:MATLAB 数据导出
- 第51节:MATLAB 绘图
- 第52节:MATLAB 图形
- 第53节:MATLAB 代数
- 第54节:MATLAB 微积分
- 第55节:MATLAB 多项式
- 第56节:MATLAB 变换
- 第57节:MATLAB GNU Octave教程
MATLAB 数字
MATLAB 支持的数字类有很多,其中包括符号和无符号的整数及单精度和双精度浮点数。
默认情况下,MATLAB 存储所有数值为双精度浮点数。
MATLAB可以选择存储任何数字或数字为整数或单精度数字阵列。
MATLAB所有的数字类型支持基本的数组运算和数学运算。
MATLAB各种数字数据类型的转换
MATLAB提供各种数字数据类型转换为以下功能:
详细例子
在MATLAB中建立一个脚本文件,输入下述代码:
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.9000 26.0250 47.1000
x =
39.9000 26.0250 47.1000
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 =
[38] [23] [45]
最小和最大整数
MATLAB中使用函数 intmax() 和 intmin() 返回最大和最小的值,它可以表示所有类型的整数。
这两个功能整数数据类型作为参数,例如,intmax(int8) 或 intmin(int64) 最大值和最小值值,可以表示的整数数据类型并返回。
详细例子
下面的例子说明如何得到最小值和最大值的整数。
在MATLAB中建立一个脚本文件,输入下述代码:
% displaying the smallest and largest signed integer data
str = 'The range for int8 is:
%d to %d ';
sprintf(str, intmin('int8'), intmax('int8'))
str = 'The range for int16 is:
%d to %d ';
sprintf(str, intmin('int16'), intmax('int16'))
str = 'The range for int32 is:
%d to %d ';
sprintf(str, intmin('int32'), intmax('int32'))
str = 'The range for int64 is:
%d to %d ';
sprintf(str, intmin('int64'), intmax('int64'))
% displaying the smallest and largest unsigned integer data
str = 'The range for uint8 is:
%d to %d ';
sprintf(str, intmin('uint8'), intmax('uint8'))
str = 'The range for uint16 is:
%d to %d ';
sprintf(str, intmin('uint16'), intmax('uint16'))
str = 'The range for uint32 is:
%d to %d ';
sprintf(str, intmin('uint32'), intmax('uint32'))
str = 'The range for uint64 is:
%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:
-9223372036854775808 to 9223372036854775807
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 4294967295
ans =
The range for uint64 is:
0 to 1.844674e+19
MATLAB最小和最大浮点数
使用函数 realmax() 和 realmin() 返回的最大值和最小值,可以表示为浮点数。
这两个函数调用时的参数'单',返回的最大值和最小值值,可以代表单精度数据类型以及何时被称为'双'的参数,返回的最大值和最小值值,可以表示双精度数据类型。
详细实例
下面的例子说明如何获得最大和最小的浮点数。
在MATLAB中建立一个脚本文件,输入下述代码:
% displaying the smallest and largest single-precision
% floating test number
str = 'The range for single is:
%g to %g and
%g to %g';
sprintf(str, -realmax('single'), -realmin('single'), ...
realmin('single'), realmax('single'))
% displaying the smallest and largest double-precision
% floating test number
str = 'The range for double is:
%g to %g and
%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