Given the Solution FOLLOW THIS MATLAB has a variety of built-in functions to mak
ID: 3528876 • Letter: G
Question
Given the
Explanation / Answer
FOLLOW THIS MATLAB has a variety of built-in functions to make it easier for you to construct matrices without having to enumerate all the elements. (The following examples show both vectors and matrices.) The ones function creates a matrix whose elements are all ones. Typing ones(m,n) creates an m row by n column matrix of ones. To create a ones matrix that is the same size as an existing matric, you can use ones(size(X)). This does not affect the input argument. For example (this definition of x applies to subsequent examples in this section): >> x = [1 2 3 4; 0 9 3 8] x = 1 2 3 4 0 9 3 8 >> y = ones(size(x)) y = 1 1 1 1 1 1 1 1 The zeros function is similar to the ones function. Typing zeros(m,n) creates an m-by-n matrix of zeros, and zeros(size(x)) will create a two-by-four matrix of zeros, if x is defined the same way as above. The max and min functions return the largest and smallest values in a vector. For example (this definition of z applies to the following series of examples): >> z = [1 2 -9 3 -3 -5] z = 1 2 -9 3 -3 -5 >> max(z) ans = 3 If called with a matrix as its argument, max returns a row vector in which each element is the maximum value of each column of the input matrix. The max function can also return a second value: the index of the maximum value in the vector. To get this, assign the result of the call to max to a two element vector instead of just a single variable. For example: >> [a b] = max(z) a = 3 b = 4 where a is the maximum value of the vector and b is the index of that value. The MATLAB function min is exactly parallel to max: >> min(z) ans = -9 sum and prod are two more useful functions for matrices. If z is a vector, sum(z) is the sum of all the elements of the vector z: >> sum(z) ans = -11 For matrices, sum sums the columns. For example: >> w = magic(3); >> w w = 8 1 6 3 5 7 4 9 2 >> sum(w) ans = 15 15 15 >> sum(sum(w)) ans = 45 Similarly, prod(z) is the product of all the elements of z. >> prod(z) ans = -810 Often, it is useful to define a vector as a subunit of a previously defined vector. To do this, you can use the colon operator. For example, using the z defined above, >> z z = 1 2 -9 3 -3 -5 >> y = z(2:5) y = 2 -9 3 -3 where (2:5) represents the sequence of index values to be taken from the larger vector. The size function returns a two-element vector giving the dimensions of the matrix with which it was called. For example: >> x = [1 2 3 4; 0 9 3 8] x = 1 2 3 4 0 9 3 8 >> y = size(x) y = 2 4 You can also define the result to be two separate values (as shown in the max example): >> [m n] = size(x) m = 2 n = 4 The length operator returns the length of a vector. If z is defined as in the above examples, >> length(z) ans = 6 For matrices, length is the length or the width, whichever is greater, i.e., length(z) is equivalent to max(size(z)). Basic Arithmetic MATLAB uses a straight-forward notation for basic scalar arithmetic. The following table summarizes simple MATLAB notation: + addition - subtraction * multiplication / division ^ exponentiation All of these work for two scalars, including complex scalars. You can also add, subtract, multiply or divide all the elements of a vector or matrix by a scalar. For example, if x is a matrix or vector, then x+1 adds one to each element x, and x/2 divides each element of x by 2. x^2 does not square each element of x, but x.^2 does. Matrix and vector exponentiation are discussed later. Another useful operator is the colon (:), which you use to specify a range of numbers (as a vector). For example: >> x = 1:4 x = 1 2 3 4 You can optionally give the range indicator a step size as the middle element in a series of colons. For example: >> x = 8:-1:5 x = 8 7 6 5 >> x = 0:0.25:1.25 x = 0 0.25 0.5 0.75 1.0 1.25 The special operator ' (prime or apostrophe) denotes the transposition of a matrix. For example: >> a = [1 2 3] a = 1 2 3 >> a' ans = 1 2 3 Element-Wise Operations You often may want to perform an operation on each element of a vector while doing a computation. For example, you may want to add two vectors by adding all of the corresponding elements. The addition (+) and subtraction (-) operators are defined to work on matrices as well as scalars. For example, if x = [1 2 3] and y = [5 6 2], then >> w = x+y w = 6 8 5 Multiplying two matrices element by element is a little different. The * symbol is defined as matrix multiplication when used on two matrices. Use .* to specify element-wise multiplication. So, using the x and y from above, >> w = x .* y w = 5 12 6 You can perform exponentiation on a vector similarly. Typing x .^ 2 squares each element of x. >> w = x .^ 2 w = 1 4 9 Finally, you cannot use / to divide two matrices element-wise, since / and are reserved for left and right matrix ``division.'' Instead, you must use the ./ function. For example: >> w = y ./ x w = 5.0000 3.0000 0.6667 All of these operations work for complex numbers as well (the * operator is no longer required in complex numbers). The abs operator returns the magnitude of its argument. If applied to a vector, it returns a vector of the magnitudes of the elements. For example, if x = [2 -4 3-4i -3i]: >> y = abs(x) y = 2 4 5 3 The angle operator returns the phase angle (i.e., the "argument") of its operand in radians. The angle operator can also work element-wise across a vector. For example: >> phase = angle(x) phase= 0 3.1416 -0.9273 -1.5708 The sqrt function computes the square root of its argument. If its argument is a matrix or vector, it computes the square root of each element. For example: >> x x = 4 -9 i 2-2i >> y = sqrt(x) y = 2.0000 0 + 3.0000i 0.7071 + 0.7071i 1.5538 - 0.6436i MATLAB also has operators for taking the real part, imaginary part, or complex conjugate of a complex number. These functions are real, imag and conj, respectively. They are defined to work element-wise on any matrix or vector. MATLAB has several operators that round fractional numbers to integers. The round function rounds its argument to the nearest integer. The fix function rounds its argument to the nearest integer towards zero, e.g. rounds ``down'' for positive numbers, and ``up'' for negative numbers. The floor function rounds its argument to the nearest integer towards negative infinity, e.g. ``down.'' The ceil (short for ceiling) function rounds its argument to the nearest integer towards positive infinity, e.g. ``up.'' round rounds to nearest integer fix rounds to nearest integer towards zero floor rounds down (towards negative infinity) ceil rounds up (towards positive infinity) All of these commands are defined to work element-wise on matrices and vectors. If you apply one of them to a complex number, it will round both the real and imaginary part in the manner indicated. For example: >> ceil(3.1+2.4i) ans= 4.0000 + 3.0000i MATLAB can also calculate the remainder of an integer division operation. If x = y * n + r, where n is an integer and r is less than n but is not negative, then rem(x,y) is r. For example: >> x x= 8 5 11 >> y y= 6 5 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.