matlab spring18 02 -...
TRANSCRIPT
beginsadventurethe
��Slides��• http://user.informatik.uni-goettingen.de/~ychen/MATLAB/
• ��elearning�������� ��
3
��$�
4
�� ��
1- 3.8 "����MATLAB��� (Ch2)
2- 3.15 �#�MATLAB���
3- 3.22 ���� (Ch3)
4- 3.29 ��������
6- 4.12 MATLAB���� (Ch4)
7- 4.19 ���MATLAB ��8- 4.26 ����� (Ch5)
9- 5.3 �#�����
10- 5.10 M���������!�I/O (Ch18)
11- 5.17 �#�M���������!�I/O
12- 5.24 � ��
13- 5.31 �#�� ��
�,�;
• *>�3+15)�3+29)�4+19)�5+3)�5+17)�5+31)
• �3�<=.�97�$�12�,'
• �,��? ��,����)��12:00��!
• 4� 5�(0-�&%�"��• �&�@��: 2�6� 8elearning (��.m, .txt, .pdf, .doc/#)
5
��
1. � �2. ����3. ������4. ���5. ���
6
1. -4)• -�Array��!%'�*�&�+�element�&9��-�& �."��7��6/&��51���$�+&,��index��2) 3�+��&����
• �0 -���� ��&(8#��
7
�#��
• �MATLAB��%�����"[]" �*�","� �" "���";"���#�
• ��$�#���'�"��*�� �)'�,���'�+���)'�,�
– Note:�,!�-�&����(
8
�������
>> A=[]A =
[]>> B=[1 2 3]B =
1 2 3>> C=[1,2,3]C =
1 2 3
>> D=[1;2;3]D =
123
>> E=B'E =
123
9
����������
MATLAB������� ����X=a:step:b (� step������step=1)
>> A=3:7A =
3 4 5 6 7
>> B=2.1:0.1:2.5B =
2.1000 2.2000 2.3000 2.4000 2.5000
>> C=3.1:-1.5:-3C =
3.1000 1.6000 0.1000 -1.4000 -2.9000
>> D=1:-1:6D =
Empty matrix: 1-by-0
10
Step��1
Step���
������#�linspacey=linspace(a, b)�&����%�(y� ��"�a�����"�b������100�"�!��
>> linspace(2, 101)ans =
Columns 1 through 162 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Columns 17 through 3218 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33�����$�%'��
Columns 97 through 10098 99 100 101
11
��������linspace ( )y=linspace(a,b,n)�"���!�$y�������a�� �����b������n��������
>> linspace(1, 2, 5)ans =
1.0000 1.2500 1.5000 1.7500 2.0000>> linspace(1, 3, 1)ans =
3
12
�n<2��y=linspace(a,b,n)#�����b
���� ���logspacey=logspace(a, b)���������y�������10a��������10b������50�������
>> logspace(1, 2)ans =
Columns 1 through 910.0000 10.4811 10.9854 11.5140 12.0679 12.6486 13.2571 13.8950 14.5635
Columns 10 through 1815.2642 15.9986 16.7683 17.5751 18.4207 19.3070 20.2359 21.2095 22.2300
Columns 19 through 2723.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884 32.3746 33.9322
Columns 28 through 3635.5648 37.2759 39.0694 40.9492 42.9193 44.9843 47.1487 49.4171 51.7947
Columns 37 through 4554.2868 56.8987 59.6362 62.5055 65.5129 68.6649 71.9686 75.4312 79.0604
Columns 46 through 5082.8643 86.8511 91.0298 95.4095 100.0000
13
���������logspace (�)
y=logspace(a,b,n)������ �y�������10a��������10b������n�������
>> logspace(1, 2, 5)ans =
10.0000 17.7828 31.6228 56.2341 100.0000
14
�����
>> A=[2 4 6 8]A =
2 4 6 8>> A(1)ans =
2>> A(1:3)ans =
2 4 6
>> A(2:end)ans =
4 6 8>> A(end:-1:1)ans =
8 6 4 2>> A([1 3])ans =
2 6
15
�����
�����
>> A=[2 4 6 8]A =
2 4 6 8
>> A(3)=5A =
2 4 5 8
>> A([1 4])=[9 9]A =
9 4 5 9
16
������
• �������• �������• ������
17
������
>> A=[1,2,3]A =
1 2 3>> B=[4,5,6]B =
4 5 6
>> A+Bans =
5 7 9
>> A+3ans =
4 5 6
>> A-Bans =
-3 -3 -3
18
�����
������
�����
B
• A".*" "./". B
• ��� B=
• ��� B=
• /
19
�������
>> A=[1,3,5]A =
1 3 5
>> B=[2,4,6]B =
2 4 6
>> A.*B %���ans =
2 12 30
>> A*5 %�������ans =
5 15 25
20
������
>> A=[6,7,8]A =
6 7 8>> B=[1,2,2]B =
1 2 2>> A./B %�������ans =
6.0000 3.5000 4.0000>> A.\B %�������ans =
0.1667 0.2857 0.2500>> A./3 %������ans =
2.0000 2.3333 2.6667>> A/3ans =
2.0000 2.3333 2.6667
21
�����
>> A=[1,2,3]A =
1 2 3
>> B=[2,4,6]B =
2 4 6
>> A.^B % ���ans =
1 16 729
>> A.^3 % ��������ans =
1 8 27
>> 3.^A % �� ��ans =
3 9 27
22
�����
C=dot(A, B)
>> A=[2,3,5]A =
2 3 5
>> B=[1,2,3]B =
1 2 3
>> dot(A, B)ans =
23
23
��sum(A.*B)�� ������
������������
�$��"+!
• �$�"+!��<, >, <=, >=, ==, ~=• +!��–����*-�(�� ��$��,��*���$��)�#��' �*&��
–�%��"+!&��������*��$(�����$��$�#�0�1
24
��������
>> A=[1, 3, 5, 7, 9]A =
1 3 5 7 9>> B=[3, 4, 5, 6, 7]B =
3 4 5 6 7>> A>=5ans =
0 0 1 1 1>> A==Bans =
0 0 1 0 0>> A>Bans =
0 0 0 1 1
25
�)!634'
• �)634'%�& ��| ��~ 9• 4'��
– 98�(#� 10$�8�(�� 00$– ����27�-�"&!�)��5��2���)"�.�(��,��2+�
– �*!634'+��������2�)-�"�!�)��)��(0�1
– �4'(a&b)��a�b 98��#�4'+�1– �4'(a|b)���1a�b���98��4'+�1– 94'(~a)��/a0�4'+�1�a98�4'+�0
26
��������
>> A=[0 1 2 3 4]A =
0 1 2 3 4>> B=[4 3 2 1 0]B =
4 3 2 1 0>> A&Bans =
0 1 1 1 0>> A|Bans =
1 1 1 1 1>> ~Bans =
0 0 0 0 1
27
2.����
• ����• �����• ��� ��
28
��ABC
29
�����
30
hilb(n): ����n*n�����invhilb(n):����n*n�������
>> hilb(3)ans =
1.0000 0.5000 0.33330.5000 0.3333 0.25000.3333 0.2500 0.2000
>> invhilb(3)ans =
9 -36 30-36 192 -18030 -180 180
Hi, j =1
i+ j −1H =
1 12
13
14
12
13
14
15
13
14
15
16
14
15
16
17
!
"
##########
$
%
&&&&&&&&&&
>> hilb(3)*invhilb(3)ans =
1.0000 0.0000 -0.00000 1.0000 0
0.0000 -0.0000 1.0000
�����
toeplitz(k, r): ������������1��k�����r�����������toeplitz(c):� �c������������
>> toeplitz([1 3 5], [1 2 3])ans =
1 2 33 1 25 3 1
>> toeplitz([2 4 6])ans =
2 4 64 2 46 4 2
31
�� �
pascal(n):��n��Pascal ��������Pascal���>> pascal(4)ans =
1 1 1 11 2 3 41 3 6 101 4 10 20
32
q �+�� � 1261,)'"$ -q B·��� � 1654,($��&�-
*�%�������!���������#�!�������������
����
vander(v):����������� ��v���A(i,j)=v(i)^(n-j)���n=length(v)
>> vander([1 2 3])ans =
1 1 14 2 19 3 1
33ü �� ������
!"#$" ⋯!&#$" ⋯
!" 1!& 1
⋯ ⋯!##$" ⋯
⋯ 1!# 1
0~1��������
rand(n): ��n*n�0~1���������rand(n, m):��n*m�0~1��������
34
>> rand(3)ans =
0.8147 0.9134 0.27850.9058 0.6324 0.54690.1270 0.0975 0.9575
>> rand(2, 3)ans =
0.9649 0.9706 0.48540.1576 0.9572 0.8003
��������
randn(n): ��n*n����0����1�����
randn(n, m):��n*m����0����1�����
35
>> randn(3)ans =
-0.2050 1.4090 -1.2075-0.1241 1.4172 0.71721.4897 0.6715 1.6302
>> randn(2, 3)ans =
0.4889 0.7269 0.29391.0347 -0.3034 -0.7873
����
magic(n): ��n*n�����
36
>> magic(3)ans =
8 1 63 5 74 9 2
>> magic(4)ans =
16 2 3 135 11 10 89 7 6 124 14 15 1
�����
• C=[A B]�� �����AB• C=[A; B]�� �����AB
37
>> A=ones(2)A =
1 11 1
>> B=eye(2)B =
1 00 1
>> [A B]ans =
1 1 1 01 1 0 1
>> [A; B]ans =
1 11 11 00 1
ü �����cat��
�������
>> A=magic(3)A =
8 1 63 5 74 9 2
>> A(2, :)=[]A =
8 1 64 9 2
>> A(:, 1)=[]A =
1 69 2
38
ü ����
ü ����
����
>> A=magic(3)A =
8 1 63 5 74 9 2
>> A'ans =
8 3 41 5 96 7 2
>> B=[2 1+3i; 1-2i 1]B =
2.0000 + 0.0000i 1.0000 + 3.0000i1.0000 - 2.0000i 1.0000 + 0.0000i
>> B'ans =
2.0000 + 0.0000i 1.0000 + 2.0000i1.0000 - 3.0000i 1.0000 + 0.0000i
>> B.'ans =
2.0000 + 0.0000i 1.0000 - 2.0000i1.0000 + 3.0000i 1.0000 + 0.0000i
39
' ����.' ��
3.�������• ������• ������• ������• ������• ������• ����� �• ����� �• �������
40
������
• C=A�B� �C����Cmn=Amn�Bmn
• C=A�x� �C����Cmn=Amn�x
• ���A+B=B+A• ��A+(B+C)=(A+B)+C
41
��������
>> A=ones(2)A =
1 11 1
>> B=eye(2)B =
1 00 1
>> A+Bans =
2 11 2
>> A+B+1ans =
3 22 3
>> A-Bans =
0 11 0
42
������
• x�����A������������
– 1A=A– x(A+B)=xA+xB– (x+y)A=xA+yA– (xy)A=x(yA)=y(xA)
43
��������
>> A=ones(3)A =
1 1 11 1 11 1 1
>> B=[1 2 3; 4 5 6; 7 8 9]B =
1 2 34 5 67 8 9
>> 2*A+2*Bans =
4 6 810 12 1416 18 20
44
� �� ���
• � ������������������
–���(A*B)*C=A*(B*C)–�����A*(B+C)=A*B+A*C–�����(B+C)*A=B*A+C*A– �� �����E*A=A*E=A
45
�������
>> A=[1 2 3; 4 5 6]A =
1 2 34 5 6
>> B=ones(2)B =
1 11 1
>> A*BError using * Inner matrix dimensions must agree.
>> B*Aans =
5 7 95 7 9
46
�����
C=An=n�A��
>> A=[1 0; 1 2]A =
1 01 2
>> A^2ans =
1 03 4
>> A^3ans =
1 07 8
47
����� �
• ind=find(X): ���X�����������������
• [row, col]=find(X): ���X�����������������ij
48
� �������
>> A=[1 0; 1 2]A =
1 01 2
>> find(A)ans =
124
>> find(A>1)ans =
4
>> [m,n]=find(A)m =
122
n =112
49
��
������
• sort(A):���A����������• sort(A, dim):�dim=1�����������dim=2���������
• sort(…, mode)�mode�ascend� �����������descend� �����
50
��� ����
>> A=rand(3)A =
0.7060 0.0462 0.69480.0318 0.0971 0.31710.2769 0.8235 0.9502
>> sort(A)ans =
0.0318 0.0462 0.31710.2769 0.0971 0.69480.7060 0.8235 0.9502
>> sort(A, 1)ans =
0.0318 0.0462 0.31710.2769 0.0971 0.69480.7060 0.8235 0.9502
>> sort(A, 2)ans =
0.0462 0.6948 0.70600.0318 0.0971 0.31710.2769 0.8235 0.9502
51
����
����
������
>> A=rand(3)A =
0.7060 0.0462 0.69480.0318 0.0971 0.31710.2769 0.8235 0.9502
>> sort(A, 'descend')ans =
0.7060 0.8235 0.95020.2769 0.0971 0.69480.0318 0.0462 0.3171
>> sort(A, 2, 'descend')ans =
0.7060 0.6948 0.04620.3171 0.0971 0.03180.9502 0.8235 0.2769
52
�����
����
� �����
• sum(A): �� A��������� A�������� �
• sum(A, dim): dim=1����� A������dim=2����� A������
• cumsum���sum���������
53
��� �����
>> A=[1 2 3; 1 1 1; 2 2 2]A =
1 2 31 1 12 2 2
>> sum(A)ans =
4 5 6
>> sum(A, 2)ans =
636
>> cumsum(A)ans =
1 2 32 3 44 5 6
>> cumsum(A, 2)ans =
1 3 61 2 32 4 6
>> sum(sum(A))ans =
15
54
��A�� ��
��� ��
��� ��
� ����
• prod(A): �� A�������� A�������� �
• prod(A, dim): dim=1����� A������dim=2����� A�����
• cumprod���prod���������
55
��������
>> A=[1 2; 3 4]A =
1 23 4
>> prod(A)ans =
3 8
>> prod(A, 2)ans =
212
>> cumprod(A)ans =
1 23 8
>> cumprod(A, 2)ans =
1 23 12
56
�����
• diff(X):���/ ���–�X���diff(X)=[X(2)-X(1) X(3)-X(2) ... X(n)-
X(n-1)]• diff([1 2 4])=[1 2]
–�X� ��[X(2:n,:) - X(1:n-1,:)]– diff(X, N)�N��– diff(X, N, dim): dim=1��� ��������dim=2��� �������
57
�������
>> a=[1 2 3; 4 5 6; 7 8 9]a =
1 2 34 5 67 8 9
>> diff(a)ans =
3 3 33 3 3
>> diff(a, 2)ans =
0 0 0
58
>> diff(a, 1, 2)ans =
1 11 11 1
N=1, dim=2��������
4.���
• ���• �����• ���• ������• ��QR��• ����
59
����
• ���� �������������� ������������������MATLAB���rank()�������
60
>> A=[1 2; 2 4]A =
1 22 4
>> rank(A)ans =
1
>> B=[1 2; 3 4]B =
1 23 4
>> rank(B)ans =
2
������
det(A)����A����
>> A=[1 2; 3 4]A =
1 23 4
>> det(A)ans =
-2
61
det( a bc d
!
"#
$
%&) = ad − bc
��
����� �����: trace(X)
>> A=[1 2; 3 4]A =
1 23 4
>> trace(A)ans =
5
62
�������
• ��A�����Q� Q'*Q=I����Q�������������A�����������������Q���A� ����
• ��orth(A)
63
>> A=[1 2; 3 4]A =
1 23 4
>> Q=orth(A)Q =
-0.4046 -0.9145-0.9145 0.4046
>> Q'*Qans =
1.0000 -0.0000-0.0000 1.0000
�����������
• E=eig(A) ���A�����������E• [X,D]=eig(A) ���A�����������D���X�X������������AX=XD
64
>> A=[1 3 5; 1 2 3; 3 4 6]A =
1 3 51 2 33 4 6
>> eig(A)ans =
9.9909-1.08330.0924
>> [X, D]=eig(A)X =
-0.5420 -0.8695 0.2021-0.3540 -0.1694 -0.8555-0.7622 0.4639 0.4766
D =9.9909 0 0
0 -1.0833 00 0 0.0924
���QR�[Q, R]=qr(A)�QR����A�������Q��������R���
>> A=[1 2 3; 1 2 2; 2 2 1]A =
1 2 31 2 22 2 1
>> [Q, R]=qr(A)Q =
-0.4082 -0.5774 -0.7071-0.4082 -0.5774 0.7071-0.8165 0.5774 -0.0000
R =-2.4495 -3.2660 -2.8577
0 -1.1547 -2.30940 0 -0.7071
65
��>> Q*Q'ans =
1.0000 0.0000 0.00000.0000 1.0000 -0.00000.0000 -0.0000 1.0000
Cholesky��LU��� ������p98-p104
����
• norm(X): �X�2����X'X����������
66
>> A=[1 2 3; 4 5 6; 7 8 9]A =
1 2 34 5 67 8 9
>> norm(A)ans =
16.8481
>> eig(A'*A)ans =
0.00001.1414
283.8586
>> sqrt(283.8586)ans =
16.8481
5.�
• � �����• � ���• � ���
67
� ������
• MATLAB�����������������
68
A =
1 0 0 00 0 0 90 2 0 00 3 0 0
!
"
####
$
%
&&&&
���l (1, 1) 1l (2, 4) 9l (3, 2) 2l (4, 2) 3
sparse(A) A
sparse(m, n) m*nfull(S) S
69
>> a=sparse([1 0 0; 0 2 0; 0 0 3])a =
(1,1) 1(2,2) 2(3,3) 3
�������
>> A=[1 0 0; 0 2 0; 0 0 3]A =
1 0 00 2 00 0 3
>> B=sparse(A)ans =
(1,1) 1(2,2) 2(3,3) 3
>> full(B)ans =
1 0 00 2 00 0 3
70
���
���
����� �
nnz():������� ���nonzeros(): ������� �
>> A=sparse([1 0 3; 5 0 0; 0 0 0])A =
(1,1) 1(2,1) 5(1,3) 3
>> nnz(A)ans =
3
>> nonzeros(A)ans =
153
71
�����������
72
� ��
speye(m, n) �������
spones(X) �"!���1������with the same sparsity structure as X�sprand(X) �"!�������� �������
sprandn(X) �"!���#���� �������
>> speye(3, 2)ans =
(1,1) 1(2,2) 1
>> full(speye(3, 2))ans =
1 00 10 0
>> spones([1 2; 0 1])ans =
(1,1) 1(1,2) 1(2,2) 1
>> sprand([1 2; 0 1])ans =
(1,1) 0.6324(1,2) 0.0975(2,2) 0.2785
>> sprandn([1 2; 0 1])ans =
(1,1) 0.3426(1,2) 3.5784(2,2) 2.7694
�������
• �������������������������������
• �������������• �����������
73
>> A=[1 2; 3 4]A =
1 23 4
>> B=sparse([1 0; 0 1])B =
(1,1) 1(2,2) 1
>> A+Bans =
2 23 5
>> B*2ans =
(1,1) 2(2,2) 2
�� �����
• �� �������– nnz��������ncol�� ���– 16 * nnz + 8 * ncol + 8 bytes (on a 64-bit machine)– 12 * nnz + 4 * ncol + 4 bytes (on a 32-bit machine)
74
http://www.mathworks.com/help/matlab/matlab_prog/strategies-for-efficient-use-of-memory.html
-)+5*���(�1�
• 64�$��(8+8)*nnz+8*(ncol+1)– (8+8)*nnz���"��'�76�/*2�&����!
– 8*(ncol+1) ���(ncol+1)� 4��� ��.���.��…�.n�*�8�#�*.n+1� 4� �.n�*���3,+5*0%
75
>> A = eye(1000);>> As = sparse(A); % Sparse matrix with only nonzero elements>> whos
Name Size Bytes Class AttributesA 1000x1000 8000000 double As 1000x1000 24008 double sparse
http://matlab.izmiran.ru/help/techdoc/math/sparse3.html
�����������
0 10 020 0 00 0 00 0 30
76
2 20 1 10 4 10
4*3����nnz=3, ncol=3
��� ��� ��� ����
(���,���)
� ����
>> a=[0 10 0; 20 0 0; 0 0 0; 0 0 30];>> b=sparse(a);>> whosName Size Bytes Class Attributesa 4x3 96 doubleb 4x3 80 double sparse