Sparse Arrays(1/35) 令a形 a=2*eye(10) a= 2 0 0 0 00 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 2 A sparse matrix is a large matrix in which the vast majority of the elements are zero. @日济大学 AW TONGJIUNIVERSITY
Sparse Arrays(1/35) a=2*eye(10) a = 2 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 A sparse matrix is a large matrix in which the vast majority of the elements are zero
Sparse Arrays(2/35) 令a b= 1 0 0 0 0 0 0 0 0 0 0 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 5 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 00 0 0 0 0 0 1 00 00 00 00 00 10 000 0 0 0 0 001 If these two matrices are multiplied together: c=a*b CPHAW @月协大学 TONGJI UNIVERSITY
Sparse Arrays(2/35) b = 1 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 If these two matrices are multiplied together: c=a*b
Sparse Arrays(3/35) C= 2 000 00 0000 0 40 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 10 0 00 0 0 00 00 0 2 0000 00 00 0 02000 0 00 0 0 002 00 0 000000020 00000 00002 The process of multiplying these two sparse matrices together requires 1900 multiplications and additions But the nonzero elements calculateonly 190 times---1/10 of the whole calculation @日济大学 AW TONGJIUNIVE具sITY
Sparse Arrays(3/35) C= 2 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 ✓ The process of multiplying these two sparse matrices together requires multiplications and additions. ✓ But the nonzero elements calculate only 190 times --- 1/10 of the whole calculation. 1900
Sparse Arrays(4/35) ▣The sparse data type The sparse data type actually saves three values for each nonzero element:the value of the element and the row and column numbers where the element is located. A=eye(2); sparse(A) ans= (1,1) 1 (2,2) 1 CDHAW @月协大学 TONGJI UNIVERSITY
Sparse Arrays(4/35) The sparse data type ✓ The sparse data type actually saves three values for each nonzero element: the value of the element and the row and column numbers where the element is located. ✓ A=eye(2); sparse(A) ans = (1,1) 1 (2,2) 1
Sparse Arrays(5/35) Common Matlab sparse matrix functions Category Function Description speye Create a sparse identity matrix Create Create a matrix containing uniformly sprand sparse distributed random values. matrices Create a matrix containing normally sprandn distributed random values. Full-to- sparse Convert a full matrix into a sparse matrix. sparse full Convert a sparse matrix into a full matrix. conversion functions find Find indices and values of nonzero elements in a matrix. 同濟大学 AW TONGJI UNIVERSITY
Sparse Arrays(5/35) Common Matlab sparse matrix functions Category Function Description Create sparse matrices speye Create a sparse identity matrix sprand Create a matrix containing uniformly distributed randomvalues. sprandn Create a matrix containing normally distributed randomvalues. Full-tosparse conversion functions sparse Convert a full matrix into a sparse matrix. full Convert a sparse matrix into a full matrix. find Find indices and values of nonzero elements in a matrix
Sparse Arrays(6/35) Example a-speye(4) full(a) a= ans (1,1) 1 1 0 0 0 (2,2) 1 0 1 00 (3,3) 1 0 01 0 (4,4) 1 00 01 r=sprand(s)has the same ¥[xyz]=find(a) sparsity structure as s X-y=Z- sprand(a) 1 1 1 ans 2 2 1 (1,1) 0.1341 3 3 1 (2,2) 0.0653 4 4 1 (3,3) 0.3751 细月济大学 (4,4) 0.3735 TONGJI UNIVERSITY
Sparse Arrays(6/35) Example ✓ a=speye(4) a = (1,1) 1 (2,2) 1 (3,3) 1 (4,4) 1 ✓ r = sprand(s) has the same sparsity structure as s ✓ sprand(a) ans = (1,1) 0.1341 (2,2) 0.0653 (3,3) 0.3751 (4,4) 0.3735 ✓ full(a) ans = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 ✓ [x y z]=find(a) x = 1 2 3 4 y = 1 2 3 4 z = 1 1 1 1
Cell Arrays(7/35) A cell array is a special MATLAB array whose elements are cells,containers that can hold other MATLAB arrays. In programming terms,each element of a cell is a pointer to another data structure,and those structure can be of different types. For example,one cell ofa cell array might contain an array 3 of real numbers,another an array of strings,and yet another an array of complex numbers. @日济大学 AW TONGJI UNIVERSITY
Cell Arrays(7/35) A cell array is a special MATLAB array whose elements are cells, containers that can hold other MATLAB arrays. In programming terms, each element of a cell is a pointer to another data structure, and those structure can be of different types. For example, one cell of a cell array might contain an array of real numbers, another an array of strings, and yet another an array of complex numbers
Cell Arrays(8/35) Cell 1,1 Cell 1,2 「1 3 -7 3 4 'This is a text 9 -8 7 string!' Cell 2,1 Cell 2,2 「3+4i -5 -i10 3-i4 0 CPHAW 同你大学 TONGJI UNIVERSITY
Cell Arrays(8/35) Cell 1,1 Cell 1,2 ‘This is a text string!’ Cell 2,1 Cell 2,2 [] 1 3 7 2 3 4 9 8 7 − − 3 4 5 10 3 4 i i i + − − −
Cell Arrays(9/35) -7 2 3 4 9 -8 > Cell 1,1Cell 1,2 G 0 "This is a text string! Cell 2,1Cell 2,2 Q ○ 「3+4i-5 -103-4 Each element of a cell array holds a pointer to another data structure,and different cells in the same cell array can point to AW different types of data structures. 翻日济大学 TONGJI UNIVERSITY
Cell Arrays(9/35) Each element of a cell array holds a pointer to another data structure, and different cells in the same cell array can point to different types of data structures
Cell Arrays(10/35) ▣creating cell arrays √Two ways By using assignment statements. By preallocatinga cell array using the cell function Using assignment statements ■Content indexing a{1,1}=[13-7;206;051]; a{1,2)=This is a text string.'; a{2,1}=[3+4*i-5;-10*i3-4*i]: a{2,2}=[] ■Cell indexing a(1,1)={[13-7;206,051]}; a(1,2)={"This is a text string.); a(2,1)={[3+4*i-5;-10*i3-4*i]}; a(2,2)={[]}; @月协大学 TONGJI UNIVERSITY
Cell Arrays(10/35) creating cell arrays ✓ Two ways ◼ By using assignment statements. ◼ By preallocating a cell array using the cell function ✓ Using assignment statements ◼ Content indexing a{1,1}=[1 3 -7; 2 0 6;0 5 1]; a{1,2}='This is a text string.'; a{2,1}=[3+4*i -5; -10*i 3-4*i]; a{2,2}=[ ]; ◼ Cell indexing a(1,1)={[1 3 -7; 2 0 6;0 5 1]}; a(1,2)={'This is a text string.'}; a(2,1)={[3+4*i -5; -10*i 3-4*i]}; a(2,2)={[ ]};