Contents

I. 清空环境变量及命令

clear all   % 清除Workspace中的所有变量
clc         % 清除Command Window中的所有命令

II. 变量命令规则

1. 变量名区分大小写

A = 2
a = 3
A =

     2


a =

     3

2. 变量名长度不超过63位 ABCDEFGHIJKLMNOPQRSTUVWXYZ123456ABCDEFGHIJKLMNOPQRSTUVWXYZ123456 = 3

3. 变量名以字母开头,可以由字母、数字和下划线组成,但不能使用标点 3A = 4 .a = 5 /b = 5

a_2 = 3
% a.2 = 4
a_2 =

     3

4. 变量名应简洁明了,通过变量名可以直观看出变量所表示的物理意义

A = rand(3,5)
rows = size(A, 1)
cols = size(A, 2)
A =

    0.0577    0.5950    0.1930    0.3907    0.3971
    0.9798    0.9622    0.3416    0.2732    0.3747
    0.2848    0.1858    0.9329    0.1519    0.1311


rows =

     3


cols =

     5

III. MATLAB数据类型

1. 数字

2 + 4

10 - 7

3 * 5

8 / 2
ans =

     6


ans =

     3


ans =

    15


ans =

     4

2. 字符与字符串

s = 'a'
abs(s)
char(65)
num2str(65)

str = 'I Love MATLAB & Machine Learning.'

length(str)

doc num2str
s =

a


ans =

    97


ans =

A


ans =

65


str =

I Love MATLAB & Machine Learning.


ans =

    33

3. 矩阵

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 =

     1     2     3
     4     5     2
     3     2     7


B =

     1     4     3
     2     5     2
     3     2     7


C =

     1
     4
     3
     2
     5
     2
     3
     2
     7


D =

   -0.9118    0.2353    0.3235
    0.6471    0.0588   -0.2941
    0.2059   -0.1176    0.0882


ans =

    1.0000    0.0000   -0.0000
    0.0000    1.0000   -0.0000
    0.0000    0.0000    1.0000


E(:,:,1) =

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0


E(:,:,2) =

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0


E(:,:,3) =

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0


E(:,:,1) =

    0.4350    0.4963    0.9573    0.2299    0.5566
    0.0915    0.6423    0.6203    0.5761    0.5294
    0.6146    0.2213    0.6003    0.8106    0.8300
    0.0110    0.8371    0.1726    0.4038    0.8588
    0.5733    0.9711    0.0903    0.9884    0.7890
    0.7897    0.8464    0.2553    0.0900    0.3178
    0.2354    0.5060    0.8586    0.3209    0.4522
    0.4480    0.2789    0.9111    0.5114    0.7522
    0.5694    0.7466    0.6996    0.0606    0.1099
    0.0614    0.2369    0.7252    0.7257    0.1097


E(:,:,2) =

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0


E(:,:,3) =

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0


E(:,:,1) =

    0.4350    0.4963    0.9573    0.2299    0.5566
    0.0915    0.6423    0.6203    0.5761    0.5294
    0.6146    0.2213    0.6003    0.8106    0.8300
    0.0110    0.8371    0.1726    0.4038    0.8588
    0.5733    0.9711    0.0903    0.9884    0.7890
    0.7897    0.8464    0.2553    0.0900    0.3178
    0.2354    0.5060    0.8586    0.3209    0.4522
    0.4480    0.2789    0.9111    0.5114    0.7522
    0.5694    0.7466    0.6996    0.0606    0.1099
    0.0614    0.2369    0.7252    0.7257    0.1097


E(:,:,2) =

     2     1     2     3     3
     3     5     5     5     4
     5     3     4     3     5
     4     5     2     1     2
     2     1     2     1     4
     2     3     1     5     5
     5     2     4     3     1
     5     5     3     2     3
     4     1     1     4     3
     2     3     1     2     2


E(:,:,3) =

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0


E(:,:,1) =

    0.4350    0.4963    0.9573    0.2299    0.5566
    0.0915    0.6423    0.6203    0.5761    0.5294
    0.6146    0.2213    0.6003    0.8106    0.8300
    0.0110    0.8371    0.1726    0.4038    0.8588
    0.5733    0.9711    0.0903    0.9884    0.7890
    0.7897    0.8464    0.2553    0.0900    0.3178
    0.2354    0.5060    0.8586    0.3209    0.4522
    0.4480    0.2789    0.9111    0.5114    0.7522
    0.5694    0.7466    0.6996    0.0606    0.1099
    0.0614    0.2369    0.7252    0.7257    0.1097


E(:,:,2) =

     2     1     2     3     3
     3     5     5     5     4
     5     3     4     3     5
     4     5     2     1     2
     2     1     2     1     4
     2     3     1     5     5
     5     2     4     3     1
     5     5     3     2     3
     4     1     1     4     3
     2     3     1     2     2


E(:,:,3) =

   -0.8320   -0.0056    0.6254   -0.0508   -0.0842
    0.4979    1.1072    0.7530   -0.8127    0.0893
    2.3156   -0.1856    0.2135   -0.4384    1.4561
   -0.7938   -1.1214   -0.7702    0.8586    0.2195
    0.5410    0.2464   -0.0071    0.1952   -0.1149
   -0.5591    1.5610    0.0932    0.8889    0.0686
    1.9766   -1.1966    0.9353    0.0692    0.7515
    0.5447   -0.2423    0.6635    2.4868   -0.6894
   -0.1379    1.0048   -0.3502   -1.6656    0.4508
    0.6199   -1.9201    1.6199   -0.4159   -1.5650

4. 元胞数组

A = cell(1, 6)
A{2} = eye(3)
A{5} = magic(5)
B = A{5}
A = 

    []    []    []    []    []    []


A = 

    []    [3x3 double]    []    []    []    []


A = 

    []    [3x3 double]    []    []    [5x5 double]    []


B =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

5. 结构体

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

     name: {'Machine Learning'  'Data Mining'}
    price: [30 40]


ans = 

    'Machine Learning'    'Data Mining'


ans = 

    'Machine Learning'


ans =

Machine Learning

IV. MATLAB矩阵操作

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     5     8     5     4     6


B =

     1     3     5     7     9


C =

     1     3     5     7     9
     1     3     5     7     9
     1     3     5     7     9


D =

     1     1     1     1
     1     1     1     1

2. 矩阵的四则运算

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     % B * G = A
H = A ./ B
A =

     1     2     3     4
     5     6     7     8


B =

     1     1     2     2
     2     2     1     1


C =

     2     3     5     6
     7     8     8     9


D =

     0     1     1     2
     3     4     6     7


E =

    17    13
    41    37


F =

     1     2     6     8
    10    12     7     8


G =

    1.8333   -0.1667
    3.1667    1.1667


H =

    1.0000    2.0000    1.5000    2.0000
    2.5000    3.0000    7.0000    8.0000

3. 矩阵的下标

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

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9


B =

     7


C =

     4     6    13    20    22


D =

     8
    14
    20
    21
     2


m =

     2
     1
     5
     4
     3


n =

     1
     2
     3
     4
     5

V. MATLAB逻辑与流程控制

1. if ... else ... end

A = rand(1,10)
limit = 0.75;

B = (A > limit);   % B is a vector of logical values
if any(B)
  fprintf('Indices of values > %4.2f: \n', limit);
  disp(find(B))
else
  disp('All values are below the limit.')
end
A =

  Columns 1 through 7

    0.4785    0.2568    0.3691    0.6618    0.1696    0.2788    0.1982

  Columns 8 through 10

    0.1951    0.3268    0.8803

Indices of values > 0.75: 
    10

2. for ... end

k = 10;
hilbert = zeros(k,k);      % Preallocate matrix

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

  Columns 1 through 7

    1.0000    0.5000    0.3333    0.2500    0.2000    0.1667    0.1429
    0.5000    0.3333    0.2500    0.2000    0.1667    0.1429    0.1250
    0.3333    0.2500    0.2000    0.1667    0.1429    0.1250    0.1111
    0.2500    0.2000    0.1667    0.1429    0.1250    0.1111    0.1000
    0.2000    0.1667    0.1429    0.1250    0.1111    0.1000    0.0909
    0.1667    0.1429    0.1250    0.1111    0.1000    0.0909    0.0833
    0.1429    0.1250    0.1111    0.1000    0.0909    0.0833    0.0769
    0.1250    0.1111    0.1000    0.0909    0.0833    0.0769    0.0714
    0.1111    0.1000    0.0909    0.0833    0.0769    0.0714    0.0667
    0.1000    0.0909    0.0833    0.0769    0.0714    0.0667    0.0625

  Columns 8 through 10

    0.1250    0.1111    0.1000
    0.1111    0.1000    0.0909
    0.1000    0.0909    0.0833
    0.0909    0.0833    0.0769
    0.0833    0.0769    0.0714
    0.0769    0.0714    0.0667
    0.0714    0.0667    0.0625
    0.0667    0.0625    0.0588
    0.0625    0.0588    0.0556
    0.0588    0.0556    0.0526

3. while ... end

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)
n =

    70


ans =

   1.7112e+98


ans =

  1.1979e+100


ans =

   1.7112e+98


ans =

  1.1979e+100

4. switch ... case ... end

mynumber = input('Enter a number:');

switch mynumber
    case -1
        disp('negative one');
    case 0
        disp('zero');
    case 1
        disp('positive one');
    otherwise
        disp('other value');
end
Error using input
Cannot call INPUT from EVALC.

Error in Example_1 (line 151)
mynumber = input('Enter a number:');

VI. MATLAB脚本与函数文件

1. 脚本文件

myScript

2. 函数文件

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

VII. MATLAB基本绘图操作

1. 二维平面绘图

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

x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*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',':')

2. 三维立体绘图

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

3. 图形的保存与导出

% (1) Edit → Copy Figure
% (2) Toolbar → Save
% (3) print('-depsc','-tiff','-r300','picture1')
% (4) File → Export Setup

VIII. MATLAB文件导入

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.xlsx',S)
U = xlsread('data.xlsx');
isequal(S, U)

4. csv格式

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