Lecture 1 Variables, Scripts, and Operations
-
Matlab Basic
- MATLAB can be thought of as a super-powerful graphing calculator
- In addition it is a programming language
-
一些命令
-
帮助命令
- help [ ]
- doc [ ] 更详细的帮助
-
注释
%
第一行注释为脚本的帮助文档
-
新建脚本
edit
edit [ ].m
-
输出字符串
disp
注意字符串使用单引号
-
保存变量到文件中
save
save myFile a b
- saves variables a and b to the file myfile.mat
- myfile.mat file is saved in the current directory
-
移除当前环境中变量
clear
-
加载文件中变量
load
-
清理命令窗口
clc
-
-
变量
- 创建和修改的变量会一直保存在工作区
- Matlab is a weekly typed language. 不需要初始化变量
- 变量命名:首位必须是字母;以字母、数字、下划线_ 组成
- 大小写敏感
- Built-in variables. Don’t use these names!
i
andj
can be used to indicate complex numberspi
has the value 3.1415926…ans
stores the last unassigned value (like on a calculator)Inf
and-Inf
are positive and negative infinityNaN
represents ‘Not a Number’
-
若不想显示输出,则在句末加上
;
-
数组[ ]
Two types of arrays
- matrix of numbers (either double or complex)
- cell array of objects (more advanced data structure)
- 行向量:在[ ] 中使用逗号或空格分隔值
- 列向量:在[ ]中使用
;
分隔 size( )
显示矩阵大小length( )
得到矩阵长度
- Initialize a vector of
ones
,zeros
, or random numbers
»o=ones(1,10)
row vector with 10 elements, all 1
»z=zeros(23,1)
column vector with 23 elements, all 0
»r=rand(1,45)
row vector with 45 elements (uniform [0,1])
»n=nan(1,69)
row vector of NaNs (useful for representing uninitialized variables)
- To initialize a linear vector of values use
linspace
»a=linspace(0,10,5)
starts at 0, ends at 10 (inclusive), 5 values
Can also use colon operator (:
)
»b=0:2:10
starts at 0, increments by 2, and ends at or before 10
increment can be decimal or negative
»c=1:5
if increment isn’t specified, default is 1 - To initialize logarithmically spaced values use
logspace
similar tolinspace
, but generate logarithmically spaced vectors - Vector Indexing
- Matlab 索引从0开始
- The index argument can be a vector
- 使用
max
和min
得到最大/小值和索引号 - 使用