# G onesm produce a square matrix of dimension m more

• 10

This preview shows page 4 - 8 out of 10 pages.

The first four commands above with a single argument, e.g.ones(m), produce a square matrixof dimensionm.More special matrices:There is also a set of built-in special matrices such asmagic,hilb,pascal,toeplitz, andvander.The matrix building commands can be used to generate block of partitioned matrices. Here isTHIS CONTENT IS PROTECTED AND MAY NOT BE SHARED, UPLOADED, SOLD OR DISTRIBUTED
2019 v23 [email protected] School of Mathematical and Statistical Sciences, Arizona State University4
an example:InputOutputE=[eye(2),ones(2,3);zeros(2),[1:3; 3:-1:1]]E =10111011110012300321Note that the command[1:3; 3:-1:1]generates the 2×3 matrix123321.Addition and Multiplication of MatricesMatrix arithmetic in MATLAB is straightforward. We can multiply our original matrixAtimesBsimply by typingA*B. The sum and difference ofAandBare given byA + BandA - B,respectively. The transpose of the real matrix A is given byA’.ExponentiationPowers of matrices are easily generated. The matrixA5is computed in MATLAB by typingA^5. We can also perform operations element-wise by preceding the operand by a period.For instance, ifV=[1,2; 3,4], thenInputOutputV^2ans =7101522Equivalent toV*VV.^2ans =14916component-wise exponentiationAppending a row or a columnA row can be easily appended to an existing matrix provided the row has the same length ofthe rows of the existing matrix. The same thing goes for the columns. The commandA=[A, v]appends the column vectorvto the columns ofA, whileA = [A; u]appends the row vectoruto the rows ofA.Examples: IfA=100010001,u=567 , andv=234, thenC = [A; u]producesC=100010001567, a 4×3 matrixD = [A, v]producesD=100201030014, a 3×4 matrix.THIS CONTENT IS PROTECTED AND MAY NOT BE SHARED, UPLOADED, SOLD OR DISTRIBUTED
2019 v23 [email protected] School of Mathematical and Statistical Sciences, Arizona State University5
Deleting a row or columnLetA=[1, 2, 3, 4, 5; 6, 7, 8, 9, 10; 11, 12, 13, 14, 15], thenInputOutputA(2,:)= []A =123451112131415deletes the 2nd row of matrix AA(:,3:5) = []A =12671112deletesthe3rdthrough5thcolumns of AA([1,3],:)= []A =678910deletes the 1st and 3rd row ofAColumnwise Array OperatorsMATLAB has a number of functions that, when applied to either a row or column vectorx,returns a single number. For example, the commandmax(x)will compute the maximum entryofx, and the commandsum(x)will return the value of the sum of the entries ofx.Otherfunctions of this form aremin,prod,mean. When used with a matrix argument, these functionsare applied to each column vector and the results are returned as a row vector.For example ifA=-32541380-6313, thenInputOutputmin(A)ans =-6210minimum entry in each column ofAmax(A)ans =1384maximum entry in each column ofAsum(A)ans =-88147sum of the entries in each column ofAprod(A)ans =1818400product of the entries in each column ofATHIS CONTENT IS PROTECTED AND MAY NOT BE SHARED, UPLOADED, SOLD OR DISTRIBUTED2019 v23 [email protected] School of Mathematical and Statistical Sciences, Arizona State University6
EXERCISESInstructions
• • • 