- 常用控制命令
- 直接计算
- 续行输入
- MATLAB搜索路径设置
- 数组创建
- 随机函数
- 元素标示与寻访
- MATLAB help系统(友情提示:非常强大遇到问题首选它)
- MATLAB数据类型
- 元胞数组
- 逻辑和流程控制
- MATLAB的数据流控制
- 并行处理
- 数据可视化
2. 直接计算ans %最新计算默认变量cd %设置当前工作目录clf %清除图形窗clc %清屏clear %清除工作空间dir %文件目录清单doc %显示帮助信息diary %把指令记录为文件edit %打开M文件编辑器exit %关闭MATLABhelp %显示帮助信息quit %关闭MATLABreturn %返回上层调用程序type %显示指定M文件内容which magic %文件目录who %内存变量whos %内存变量 详细
3. 续行输入(12+2*(7-4))/3^2y1 = 2*sin(0.3*pi)/(1+sqrt(5))y2 = 2*cos(0.3*pi)/(1+sqrt(5))
4. MATLAB搜索路径设置S = 1-1/2+1/3-1/4+ ...1/5-1/6+1/7-1/8
5. 数组创建A = pwdcd('c:\')cd(A)path(path,'c:/')
% 逐个元素输入法a1 = 1:6a1 = 6:-1:1a2 = 0:pi/4:pia3 = 1:-0.1:0
% 二维数组的创建a = 2.7358; b = 33/79;C = [100 1,2*a+i*b,b*sqrt(a); sin(pi/4),a+5*b,3.5+i,5]
% 中规模数组的数组编辑器创建法% 试用变量编辑器,把如下的数组输入MATLAB内存,并命名为A18。a = [0.8147 0.9134 0.2785 0.9649 0.9572 0.14190.9058 0.6324 0.5469 0.1576 0.4854 0.4218
% 中规模数组的M文件创建法A = [ 92 99 1 8 15 67 74 51 58 4098 80 7 14 16 73 55 57 64 414 81 88 20 22 54 56 63 70 4785 87 19 21 3 60 62 69 71 2886 93 25 2 9 61 68 75 52 3417 24 76 83 90 42 49 26 33 6523 5 82 89 91 48 30 32 39 6679 6 13 95 97 29 31 38 45 7210 12 94 96 78 35 37 44 46 53
6. 随机函数% 标准与经典数组生成函数ao = ones(2, 4) %1矩阵az = zeros(2, 5) %零矩阵ae = eye(3) %单位矩阵am = magic(4) %魔方矩阵ad = diag(am) %对角矩阵add = diag(diag(am))
7. 元素标示与寻访Au = rand(1, 5) %0-1均匀分布的1x5随机矩阵Ai = randi([-3, 4], 2, 8) %-3到4均匀分布的2x8随机矩阵An = randn(3, 4) %标准正态分布的3x4矩阵out = randsrc(10, 10, [-3 -1 1 3; .25 .25 .25 .25])% randsrc 生成指定范围的随机数;在-3 1 1 3四个数中随机选生成10x10的矩阵,后面的0.25% 是每个数对应的概率Ap = randperm(8); %随机排序1到8
A = zeros(2, 6)A( = 1:12a8 = A(8)a311 = A([3, 11])A(3, 3) = 37a13 = A(:, 1:3)aend = A(1:3, 4:end)A([1 3], 4:end)L = A<3% A(L) = NaNA(L) = []
8. MATLAB help系统% 数组的扩充和收缩a = 1:5; b = 6:10; c = 11:15;a_b = [a b]ab = [a;b;c]a_2 = [a b c ]AB1 = repmat(ab, [1, 2])AB2 = repmat(ab, [2, 3])AB2([2, 3, 5, 6], = []AB2(:, 1:3) = []
9. MATLAB数据类型helpwin datafunhelp datafundoc datafunlookfor sumdocsearch sum
10. 元胞数组% 双精度a = magic(4)% 逻辑1 == 2% 字符串a = '加油 帅哥'% 元胞b = cell(1, 2)% 结构体a1.a1 = 100a1.a2 = '随缘'a1.a3 = 1 == 2a1.a4 = dataset(magic(4))% 函数句柄@sum% 字符串数组a = 'This is an example.'size(a)a14 = a(1:4)ra = a(end:-1:1)Ua = double(a)char(Ua)wxc = '贵有恒'Ua = double(wxc)char(Ua)w = find(a>= 'a'&a<= 'z');a(w) = a(w)-32;char(a)b = 'Example ''3.2-1'''% 多行字符串数组的创建% 多行串数组的“补空等长”直接输入法;非等长串数组借助char , str2mat , strvcat等指令声称多行串数组clearS = ['This string array 'size(S)S1 = char('This string array', 'has two rows.')size(S1)S2 = str2mat('这', '字符', '串数组', '', '由4行组成')size(S2)S3 = strvcat('这', '字符', '串数组', '', '由4行组成')size(S3)
% 元胞数组的创建和显示% 1 外标识元胞元素赋值C_str = char('这是', '胞元数组创建算例 1');R = reshape(1:12, 3, 4)Cn = 1+2i;S_sym = sym('sin(-3*t)*exp(-t)'); % 符号数组A(1, 1) = {C_str}; A(1, 2) = {R}; A(2, 1) = {Cn}; A(2, 2) = {S_sym};A
% 2 编址元胞元素内涵直接赋值B{1, 1} = C_str; B{1, 2} = R; B{2, 1} = Cn; B{2, 2} = S_sym;celldisp(B)cellplot(B)
% 3 元胞数组的扩充、收缩和重组C = cell(2);C(:, 1) = {char('Another', 'text string'); 10:-1:1}
% 4 元胞数组的“列”扩充和“行”扩充AC = [A C]A_C = [A; C]cellplot(A_C, 'legend')A_C(3, = []R_A_C = reshape(A_C, 2, 3)
% 5 元胞数组内容的获取和配置f1 = R_A_C(1, 3)class(f1)f2 = R_A_C{1, 3}class(f2)f3 = R_A_C{1, 1}(:, [1 2 5 6])% f3 = R_A_C(1, 1)(:, [1 2 5 6])[f4, f5, f6] = deal(R_A_C{[1, 3, 4]})[ff4, ff5, ff6] = R_A_C{[1, 3, 4]}
% 6 元胞与数值数组之间的转换rng(0, 'v5uniform')A = rand(2, 3)C1 = num2cell(A)SC1 = size(C1)C2 = num2cell(A, 2)disp(['C2{1, :} = ', mat2str(C2{1, :}, 4)]) % 有效数字SC2 = size(C2)C3 = num2cell(A, 1)SC3 = size(C3)
11. 逻辑和流程控制% 7 对元胞数组运算的cellfun指令rng(1)x(1) = {1:10}; x(2) = {rand(100, 2)};xm = cellfun(@mean, x, 'UniformOutput', 0)class(xm)
A = 1:9, B = 10-Ar0 = (A<4)r1 = (A == B)
t = -3*pi:pi/10:3*pi;y = sin(t)./t;tt = t+(t == 0)*eps;yy = sin(tt)./tt;subplot(1, 2, 1), plot(t, y), axis([-9, 9, -0.5, 1.2]),xlabel('t'), ylabel('y'), title('残缺图形')subplot(1, 2, 2), plot(tt, yy), axis([-9, 9, -0.5, 1.2])xlabel('tt'), ylabel('yy'), title('正确图形')A = [-2, -1, 0, 0, 1, 2, 3]L1 = ~(A>1) % 判断哪些不大于1L2 = (A>-1)& ( A>0 &A<3) % 判断哪些大于0小于2
12. MATLAB的数据流控制% 常用逻辑函数a = false(1, 10)true(1, 10)isempty(A)isnan(A)ischar('A')
13. 并行处理tic; %tic与toc配合使用,常看这两个命令中间程序的运行时间% for循环和while循环控制for s = 0:1000000x = exp(i * 2)* exp(i * 3 + sin(i*i)) * sin(i)^2;endtime_for = toctic;s = 0;while s~=1000000x = exp(i * 2)* exp(i * 3 + sin(i*i)) * sin(i)^2;s = s+1;endtime_for = toc
14. 数据可视化matlabpoolticparfor s = 0:1000000x = exp(i * 2)* exp(i * 3 + sin(i*i)) * sin(i)^2;endtime_parfor = toc
% 三维x = linspace(-2, 2, 25); % 在x轴上取25点y = linspace(-2, 2, 25); % 在y轴上取25点[xx,yy] = meshgrid(x, y); % xx和yy都是25x25的矩阵zz = xx.*exp(-xx.^2-yy.^2); % 计算函数值,zz也是21x21的矩阵mesh(xx, yy, zz) % 画出立体网状图
x = linspace(-2, 2, 25); % 在x轴上取25点y = linspace(-2, 2, 25); % 在y轴上取25点[xx, yy] = meshgrid(x, y); % xx和yy都是25x25的矩阵zz = xx.*exp(-xx.^2 - yy.^2); % 计算函数值,zz也是25x25的矩阵surf(xx, yy, zz); % 画出立体曲面图
peaks% 对peaks函数取点,再以各种不同方法进行绘图。meshz可将曲面加上围裙[x, y, z] = peaks;meshz(x, y, z);% waterfall可在x方向或y方向产生水流效果[x, y, z] = peaks;waterfall(x, y, z)% 下列命令产生在y方向的水流效果[x, y, z] = peaks;waterfall(x', y', z')% meshc同时画出网状图与等高线[x, y, z] = peaks;meshc(x, y, z)% surfc同时画出曲面图与等高线[x, y, z] = peaks;surfc(x, y, z)% contour3画出曲面在三度空间中的等高线:contour3(peaks, 20)% contour画出曲面等高线在XY平面的投影contour(peaks, 20)
% plot3可画出三度空间中的曲线t = linspace(0, 20*pi, 501);plot3(t.*sin(t), t.*cos(t), t)% 亦可同时画出两条三度空间中的曲线t = linspace(0, 10*pi, 501);plot3(t.*sin(t), t.*cos(t), t, t.*sin(t), t.*cos(t), -t)
友情提示:可将代码复制到m文件中,一行一行运行,根据输出结果,了解每个命令的作用。