Matlab快速入门到进阶

入门篇

1. matlab软件下载与安装

如果是数值计算那么对版本要求不高,如果是需要仿真那么就考虑新版本。
链接:https://pan.baidu.com/s/1N-L3pdU7y3btpPNU3kiP8A
提取码:abcd
软件有点大,12G耐心点吧!
开始安装:
下载后的文件有 R2018b_win64.iso 镜像,其实就是一个压缩包,解压即可!
进入 R2018b_win64文件夹并双击安装程序“setup.exe”,基本上软件安装都是这个命名
打开安装程序之后,选择第二项“使用文件安装密钥”
下一步,选“是”同意协议。下一步,选择“我已有我的许可证的文件夹安装密钥”,然后输入密钥:

09806-07443-53955-64350-21751-41297

下一步,选择安装目录(注意软件大约20G)
下一步,选择要安装的产品(默认全选即可)
下一步,选择需要的安装选项(默认即可)
下一步,点击“安装”,等待安装吧!

安装成功后出现以下界面,默认“下一步”安装成功
打开软件(如果桌面或开始菜单没有图标,则软件在“安装目录\R2018b\bin”文件夹下),选择“在不使用Internet的情况下手动激活”

下一步,选择“输入许可证文件的完整路径(包括文件名)”,然后点击“浏览”选择下载文件中的“激活文件”文件夹下的“license_standalone.lic”
激活成功后,将下载文件中“激活文件”文件夹下的“netapi32.dll”复制到“安装目录\R2018b\bin\win64”目录下

2. Matlab基础知识

变量命名规则

  1. 区分大小写
  2. 长度不超过63位
  3. 以字母开头,可以由数字,字母,下划线组成
  4. 见名知意

数据类型

数字
字符与字符串
矩阵
元胞数组
结构体

3. Demo

Clear all  % 清除所有变量
Clc      % 清除所有命令

A = 2    % 变量命名
a_2 = 3  % 变量命名

b = rand(3,5)    % 随机生成35列的矩阵
rows = size(b,1)  % 3
colos = size(b,2)  % 5 

% 直接运算
2 + 4    
107
3 * 5
8 / 2

s = 'a'     % 字符
abs(s)  	% 97
char(65)  	% A
num2str(65) % '65'
str = 'hello world'	
length(str)

doc num2str	 % 查看帮助文档

% 矩阵
A = [1 2 3; 4 5 2; 3 2 7]
B = A'
C = A(:)
D = inv(A)
A *D

E = zeros(10, 5, 3)
E(:,:,1) = rand(10,5)
E(:,:,2) = randi(5,10,5)
E(:,:,3) = randn(10,5)

% 元胞数组
A = cell(1,6)
A[2] = eye(3)
A[5] = magic(5)
B = A[5]

% 结构体
books = struct('name', {{'Machine Learning', 'Data Mining'}},'price', [30 40])
books.name
books.price
books.name(1)
books.name{1}

% 矩阵操作
A = [1 2 3 5 8 5 4 6]
B = 1:2:9
C = repmat(B,3,1)
D = ones(2,4)

% 矩阵四则运算
A = [1 2 3 4; 5 6 7 8]
B = [1 1 2 2; 2 2 1 1]
C = A + B
D = A – B
E = A * B'
F = A .* B
G = A / B   % G = A*pinv(B)
H = A ./ B

% 矩阵下标
A = magic(5)
B = A(2,3)
C = A(3,:)
D = A(:,4)
[m,n] = find(A > 20)

1.4 逻辑与流程控制
ifelse…end
for…end
while…end
switch…case…end

% 单行注释
%{ 多行注释 %}

A = rand(1,10)
limit = 0.75
B = (A>limit)	
if any(B)
	fprintf('indices of values > %4.2f : \n', limits)
	disp(find(B))
else
	disp('all values are below the limit.')
end


k = 10
hilbert = zeros(k,k)
for m = 1:k
	for n = 1:k
		hilbert(m,n) = 1/(m+n-1)
	end
end
hilbert


n = 1
nFactorial = 1
while nFactorial <1e100
	n = n+1
	nFactorial = nFactorial * n
end
n

factorial(69)
factorial(70)
prod(1:69)
prod(1:70)


mynumber = input('Enter a number:')
switch mynumber
	case -1
		disp('nagative one')
	case 0
		disp('zero')
	case 1
		disp('positive one')
	otherwise
		disp('other value')
end

4. 脚本文件与函数文件

文件test1.m % 脚本文件

mynumber = input('Enter a number:')
switch mynumber
	case -1
		disp('nagative one')
	case 0
		disp('zero')
	case 1
		disp('positive one')
	otherwise
		disp('other value')
end

文件myFunction.m % 函数文件可以直接调用

function output = myFunction(input)
switch input
	case -1
		output = 'nagative one'
	case 0
		output = 'zero'
	case 1
		output = 'positive one'
	otherwise
		output = 'other value'
end

函数文件可以直接调用

mynumber = input('Enter a number:')
output = myFunction(mynumber)

5. 基本绘图操作

二维平面绘图 : 方式一

x = 0:0.01:2*pi
y = sin(x)
figure
plot(x,y)
title('y=sin(x)')
xlabel('x')
ylabel('y')
xlim([0 2*pi])

二维平面绘图 : 方式二

x = 0:0.01:20
y1 = 200*exp(-0.05*x).*sin(x)
y2 = 0.8*exp(-0.05*x).*sin(10*x)
figure
[AX,H1,H2] = plotyy(x,y1,x,y2,'plot')
set(get(AX(1),'Ylabel'),'String','Slow Decay')
set(get(AX(2),'Ylabel'),'String','Fast Decay')
xlabel('Time (\musec)')
title('Multiple Decay Rates')
set(H1,'LineStyle','--')
set(H2,'LineStyle',':')

三维平面绘图

t = 0:pi/50:10*pi
plot3(sin(t),cos(t),t)
xlabel('sin(t)')
ylabel('cos(t)')
zlabel('t')
grid on
axis square

图形的保存与导出

(1)	Edit --> Copy Figure --> ctrl+V
(2)	Toolbar --> Save
(3)	print('-depsc', '-tiff', '-r300', 'picture')
(4)	File --> Export Setup

1.7 文件导入
(1) mat格式

save data.mat x y1 y2
clear all
load data.mat

(2) txt格式

M = importdata('myfile.txt')
S = M.data;
save 'data.txt' S –ascii
T = load('data.txt')
Isequal(S,T)

(3) xls格式

xlswrite('data.xls',S)
W = xlsread('data.xls')
isequal(S,W)

xlswrite('data.xls',S)
U = xlsread('data.xls')
isequals(S,U)

(4) csv格式

csvwrite('data.csv',S)
V = csvread('data.csv')
isequal(S,V)

进阶篇

1. 编程习惯与风格

变量命名规则
Cell Mode
程序发布(Publish)

% 表示注释
%% 表示Cell Mode, 但是需要加一个空格,不然会当做注释
发布:File --> Publish会运行程序,并生成一个网页

2. 程序调试

断点调试
查看编辑matlab自带的工具箱函数
edit mean
edit newff

matlab内存优化配置
feature memstats

3. 向量化编程

变量清除
预分配空间
按列优先,循环次数多的安排在内层

4. 图像对象和句柄

(1) 设置线条属性

x = 0:0.01:2*pi
y = sin(x)
h = plot(x,y)
grid on
get(h)
set(h,'linestyle',':','linewidth',5,'color','b')
% set(h,'linestyle','-','linewidth',2,'color','k')

(2) 修改网格间隔

set(gca, 'xtick', 0:0.5:7)
set(gca, 'ytick', -1:0.1:1)

(3) 设置图例字体大小

x = 0:0.01:2*pi
y1 = sin(x)
y2 = cos(x)
plot(x,y1,'r')
hold on
plot(x,y2,'-.b')
h = legend('sin(x)','cos(x)');
set(h,'fontsize',16,'color','k','edgecolor','r','textcolor','w')

(4) 拆分图例

x = 0:0.01:2*pi
y1 = sin(x)
y2 = cos(x)
plot(x,y1,'r')
hold on
plot(x,y2,'-.b')
ax1 = axes('position',get(gca,'position'),'visible','off')
legend(ax1,h1,'sin(x)','location','northwest')
ax2 = axes('position',get(gca,'position'),'visible','off')
legend(ax2,h2,'cos(x)','location','northwest')
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值