matlab spring18 02 -...
TRANSCRIPT
![Page 2: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/2.jpg)
beginsadventurethe
![Page 3: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/3.jpg)
��Slides��• http://user.informatik.uni-goettingen.de/~ychen/MATLAB/
• ��elearning�������� ��
3
![Page 4: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/4.jpg)
��$�
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 �#�� ��
![Page 5: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/5.jpg)
�,�;
• *>�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
![Page 6: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/6.jpg)
��
1. � �2. ����3. ������4. ���5. ���
6
![Page 7: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/7.jpg)
1. -4)• -�Array��!%'�*�&�+�element�&9��-�& �."��7��6/&��51���$�+&,��index��2) 3�+��&����
• �0 -���� ��&(8#��
7
![Page 8: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/8.jpg)
�#��
• �MATLAB��%�����"[]" �*�","� �" "���";"���#�
• ��$�#���'�"��*�� �)'�,���'�+���)'�,�
– Note:�,!�-�&����(
8
![Page 9: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/9.jpg)
�������
>> 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
![Page 10: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/10.jpg)
����������
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���
![Page 11: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/11.jpg)
������#�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
![Page 12: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/12.jpg)
��������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
![Page 13: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/13.jpg)
���� ���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
![Page 14: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/14.jpg)
���������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
![Page 15: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/15.jpg)
�����
>> 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
�����
![Page 16: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/16.jpg)
�����
>> 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
![Page 17: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/17.jpg)
������
• �������• �������• ������
17
![Page 18: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/18.jpg)
������
>> 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
�����
������
�����
![Page 19: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/19.jpg)
B
• A".*" "./". B
• ��� B=
• ��� B=
• /
19
![Page 20: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/20.jpg)
�������
>> 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
![Page 21: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/21.jpg)
������
>> 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
![Page 22: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/22.jpg)
�����
>> 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
![Page 23: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/23.jpg)
�����
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)�� ������
������������
![Page 24: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/24.jpg)
�$��"+!
• �$�"+!��<, >, <=, >=, ==, ~=• +!��–����*-�(�� ��$��,��*���$��)�#��' �*&��
–�%��"+!&��������*��$(�����$��$�#�0�1
24
![Page 25: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/25.jpg)
��������
>> 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
![Page 26: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/26.jpg)
�)!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
![Page 27: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/27.jpg)
��������
>> 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
![Page 28: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/28.jpg)
2.����
• ����• �����• ��� ��
28
![Page 29: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/29.jpg)
��ABC
29
![Page 30: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/30.jpg)
�����
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
![Page 31: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/31.jpg)
�����
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
![Page 32: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/32.jpg)
�� �
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,($��&�-
*�%�������!���������#�!�������������
![Page 33: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/33.jpg)
����
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
![Page 34: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/34.jpg)
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
![Page 35: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/35.jpg)
��������
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
![Page 36: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/36.jpg)
����
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
![Page 37: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/37.jpg)
�����
• 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��
![Page 38: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/38.jpg)
�������
>> 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
ü ����
ü ����
![Page 39: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/39.jpg)
����
>> 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
' ����.' ��
![Page 40: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/40.jpg)
3.�������• ������• ������• ������• ������• ������• ����� �• ����� �• �������
40
![Page 41: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/41.jpg)
������
• 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
![Page 42: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/42.jpg)
��������
>> 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
![Page 43: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/43.jpg)
������
• x�����A������������
– 1A=A– x(A+B)=xA+xB– (x+y)A=xA+yA– (xy)A=x(yA)=y(xA)
43
![Page 44: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/44.jpg)
��������
>> 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
![Page 45: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/45.jpg)
� �� ���
• � ������������������
–���(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
![Page 46: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/46.jpg)
�������
>> 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
![Page 47: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/47.jpg)
�����
C=An=n�A��
>> A=[1 0; 1 2]A =
1 01 2
>> A^2ans =
1 03 4
>> A^3ans =
1 07 8
47
![Page 48: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/48.jpg)
����� �
• ind=find(X): ���X�����������������
• [row, col]=find(X): ���X�����������������ij
48
![Page 49: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/49.jpg)
� �������
>> 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
��
![Page 50: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/50.jpg)
������
• sort(A):���A����������• sort(A, dim):�dim=1�����������dim=2���������
• sort(…, mode)�mode�ascend� �����������descend� �����
50
![Page 51: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/51.jpg)
��� ����
>> 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
����
����
![Page 52: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/52.jpg)
������
>> 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
�����
����
![Page 53: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/53.jpg)
� �����
• sum(A): �� A��������� A�������� �
• sum(A, dim): dim=1����� A������dim=2����� A������
• cumsum���sum���������
53
![Page 54: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/54.jpg)
��� �����
>> 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�� ��
��� ��
��� ��
![Page 55: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/55.jpg)
� ����
• prod(A): �� A�������� A�������� �
• prod(A, dim): dim=1����� A������dim=2����� A�����
• cumprod���prod���������
55
![Page 56: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/56.jpg)
��������
>> 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
![Page 57: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/57.jpg)
�����
• 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
![Page 58: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/58.jpg)
�������
>> 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��������
![Page 59: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/59.jpg)
4.���
• ���• �����• ���• ������• ��QR��• ����
59
![Page 60: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/60.jpg)
����
• ���� �������������� ������������������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
![Page 61: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/61.jpg)
������
det(A)����A����
>> A=[1 2; 3 4]A =
1 23 4
>> det(A)ans =
-2
61
det( a bc d
!
"#
$
%&) = ad − bc
![Page 62: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/62.jpg)
��
����� �����: trace(X)
>> A=[1 2; 3 4]A =
1 23 4
>> trace(A)ans =
5
62
![Page 63: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/63.jpg)
�������
• ��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
![Page 64: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/64.jpg)
�����������
• 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
![Page 65: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/65.jpg)
���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
![Page 66: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/66.jpg)
����
• 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
![Page 67: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/67.jpg)
5.�
• � �����• � ���• � ���
67
![Page 68: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/68.jpg)
� ������
• 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
![Page 69: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/69.jpg)
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
![Page 70: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/70.jpg)
�������
>> 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
���
���
![Page 71: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/71.jpg)
����� �
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
![Page 72: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/72.jpg)
�����������
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
![Page 73: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/73.jpg)
�������
• �������������������������������
• �������������• �����������
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
![Page 74: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/74.jpg)
�� �����
• �� �������– 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
![Page 75: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/75.jpg)
-)+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
![Page 76: MATLAB Spring18 02 - user.informatik.uni-goettingen.deuser.informatik.uni-goettingen.de/~ychen/MATLAB/MATLAB_Spring18_02.pdf · 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705 30.8884](https://reader030.vdocuments.us/reader030/viewer/2022041204/5d532e9388c9932e748b5fcf/html5/thumbnails/76.jpg)
�����������
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