《科学计算科学计算 (6).pdf》由会员分享,可在线阅读,更多相关《科学计算科学计算 (6).pdf(17页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、1.5 1.5 MMatrix atrix E Elements lements ReferenceReferenceThe methods of referring to matrix elementsUsing the colon expression to get submatricesUsing empty matrices to delete matrix elementsChange the shape of a matrix A(3,2)=2001.1.The Methods of Referring to Matrix ElementsThe Methods of Referr
2、ing to Matrix ElementsA(3,2)represents the element in the 3rd row and 2nd column of matrix A.(1)To Refer to Elements of a Matrix by Subscripts A=1,2,3;4,5,6;A(4,5)=10A=1 2 3 0 04 5 6 0 00 0 0 0 00 0 0 0 10Note:if the given row subscript or column subscript is greater than the number of rows and colu
3、mns of the original matrix,MATLAB will automatically expand the original matrix,and set the expanded matrix elements without assignment to 0.(2)Reference by IndexIn MATLAB,matrix elements are stored by the order of columns.That is,the elements of the first column are stored firstly,and then the elem
4、ents ofthe second column,until those of the last column are stored.The index of matrix elements is the order in which matrix elements arearranged in the memory.A=1,2,3;4,5,6A=1 2 34 5 6 A(3)ans=2The index actually corresponds to the subscript.Take matrix A with m rows and n columns as an example,the
5、 index of matrix element A(i,j)is(j-1)m+i.D=sub2ind(S,I,J)A vector composed of the number of rows and columnsRow subscript of the matrix to be converted Column subscript of transformation matrixIndexThe sub2ind function converts row and column subscripts of specified elements in a matrix into stored
6、 index.A=1:3;4:6A=1 2 34 5 6 D=sub2ind(size(A),1,2;2,2,1,1;3,2)D=1 26 4An example of the sub2ind function.I,J=ind2sub(S,D)A vector composed of the number of rows and columnsindexRow subscriptColumn subscriptThe ind2sub function is used to convert the index of matrix elements into corresponding subsc
7、ripts.I,J=ind2sub(3,3,1,3,5)I=1 3 2J=1 1 2An example of the ind2sub function.2.Using the Colon Expression to Get a S2.Using the Colon Expression to Get a SubmatrixubmatrixA submatrix refers to a matrix composed of some elements in a matrix.A(i,:)All the elements of row i in matrix A.A(:,j)All the el
8、ements of column j in matrix A.A(i:i+m,k:k+m)All the elements from row i to row i+m and from column k tocolumn k+m in matrix A.A(i:i+m,:)All the elements from row i to row i+m in matrix A.A=1,2,3,4,5;6,7,8,9,10;11,12,13,14,15A=1 2 3 4 56 7 8 9 1011 12 13 14 15 A(1:2,:)ans=1 2 3 4 56 7 8 9 10 A(2:3,1
9、:2:5)ans=6 8 1011 13 15 A=1,2,3,4,5;6,7,8,9,10;11,12,13,14,15;16,17,18,19,20;A(end,:)ans=16 17 18 19 20 A(1,4,3:end)ans=3 4 518 19 20The end operator indicates the subscript of the last element of a dimension.x=x=3.3.UsUse e an Ean Empty mpty MMatrix to atrix to D Delete elete E Elements of lements
10、of a Ma MatrixatrixAn empty matrix is a matrix with no elements.The x is an empty matrix.A=1,2,3,0,0;7,0,9,2,6;1,4,-1,1,8A=1 2 3 0 07 0 9 2 61 4 -1 1 8 A(:,2,4)=A=1 3 07 9 61 -1 84.4.Change the Change the S Shape of hape of a a MMatrixatrixreshape(A,m,n):rearranges matrix A into a two dimensional ma
11、trix with m rows and n columns when the total elements of the matrix remain unchanged.Note:the reshape function only changes the number of rows and columns of the original matrix,but it does not change the number of matrix elements and storing order of the original matrix.x=23,45,65,34,65,34,98,45,78,65,43,76;y=reshape(x,3,4)y=23 34 98 6545 65 45 4365 34 78 76 A=-45,65,71;27,35,91A=-45 65 7127 35 91 B=A(:)B=-452765357191Note:A(:)is equivalent to reshape(A,6,1).A(:)stack elements of each column in matrix A to finally form a column vector.
限制150内