正在加载图片...
12 I Basic Operations In the following table you can find other examples: MATLAB(type after the Mathematical representation prompt >followed by Enter) is equal to-1 >0(3,1) ans= N is equal to9 M,is equal to元 3.1416 >(2,1) B.is equal to 2 ans= 2 es bi only the single instead of N(2,1). If you refer to an element in a nonexistent position,MATLAB gives you an alert: >>043.3) <ENTER> ??Index exceeds matrix dimensions What har ens if you want to address more than one element at time?This is pos sing a vector (or a matrix)in the indexing place to express the selected rows or columns >Q([1,3],2) <ENTER> ans■ -4 1 How many values do you expect MATLAB to display when you type Q([1.3].[1.2))? Two or Four?Let's try >Q([1,31,[1,21 <ENTER> ans 9 -1 1 The answer is four,because MATLAB shows the values in the positions given by Now supp a larg matrix from which you ract elements row to t e jth row in the seco m.MATLAB offers a very to this.namely the colon (operator Before see us generate a new matrix: >×=[123:456:789:101112;131415] <ENTER>12 1 Basic Operations In the following table you can fi nd other examples: Mathematical representation MATLAB (type after the prompt >> followed by Enter) Q3,1 is equal to −1 >>Q(3,1) ans= -1 N2,2 is equal to 9 >> N(2,2) ans= 9 M1,3 is equal to p >> M(1,3) ans= 3.1416 P2,1 is equal to 2 >> N(2,1) ans= 2 If P and M are two vectors, it is possible to refer to their entries by referencing only their single dimension, i.e., you can type M(3) instead of M(1,3), and N(2) instead of N(2,1). If you refer to an element in a nonexistent position, MATLAB gives you an alert: >> Q(3,3) <ENTER> ??? Index exceeds matrix dimensions. What happens if you want to address more than one element at time? This is pos￾sible in MATLAB using a vector (or a matrix) in the indexing place to express the selected rows or columns: >> Q([1,3],2) <ENTER> ans = -4 1 How many values do you expect MATLAB to display when you type Q([1,3],[1,2]) ? Two or Four? Let’s try: >> Q([1,3],[1,2]) <ENTER> ans = 4 -4 -1 1 The answer is four, because MATLAB shows the values in the positions given by each combination of the specifi ed rows and columns, i.e., Q 11 , Q 12 , Q 31 , Q 32 . Now suppose you have a large matrix from which you want to extract elements going from the ith row to the jth row in the second column. MATLAB offers a very effi cient way to this, namely the colon ( : ) operator. Before seeing how it works, let us generate a new matrix: >> x=[1 2 3; 4 5 6; 7 8 9; 10 11 12; 13 14 15] <ENTER>
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有