![]() To do this, you can use the colon operator. Often, it is useful to define a vector as a subunit of a previously defined vector. Similarly, prod(z) is the product of all the elements of z. ![]() If z is a vector, sum(z) is the sum of all the elements of the vector z:įor matrices, sum sums the columns. Sum and prod are two more useful functions for matrices. The MATLAB function min is exactly parallel to max: ![]() Where a is the maximum value of the vector and b is the index of that value. To get this, assign the result of the call to max to a two element vector instead of just a single variable. The max function can also return a second value: the index of the maximum value in the vector. 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. For example (this definition of z applies to the following series of examples): The max and min functions return the largest and smallest values in a vector. 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 zeros function is similar to the ones function. For example (this definition of x applies to subsequent examples in this section): To create a ones matrix that is the same size as an existing matric, you can use ones(size( X)). Typing ones( m, n) creates an m row by n column matrix of ones. The ones function creates a matrix whose elements are all ones. (The following examples show both vectors and matrices.) MATLAB has a variety of built-in functions to make it easier for you to construct matrices without having to enumerate all the elements. So, using the y matrix defined above, you could specify a subset of y using the colon: The colon causes MATLAB to step in sequence through the numbers specified. You can also specify a range of numbers in a defined vector or matrix using the colon operator. You can specify a value in matrix y the same way: If x is already defined as a vector of the form then you can define a new variable as a subset of x by using the index of the specific value in vector x. This means that the first element always has index (1,1), not (0,0). It is important to note that MATLAB indexes matrices in the following manner: Vectors and scalars are initialized the same way as matrices. (In MATLAB 4, the * operator is not required in complex numbers, as it was in previous versions.) In the example above, the expression 4 + 5i, with the + sign surrounded by blanks, would represent two separate numbers. When entering complex numbers as matrix elements within brackets, it is important to not use blanks. You can also initialize matrices with complex numbers: (Note: You can also use the semicolon after an expression or statement to suppress printing or to separate statements.) Within the square brackets that are used to form vectors and matrices, you can use a semicolon to end a row. Where each value may be rational or complex numbers. The most straightforward way to initialize a matrix is to type a command of the form: Start the program from Windows or Mac OSX. You need to be on CMU network or VPN to CMU network to use MATLAB. ![]() Greatly borrowed from MIT's MATLAB on Athena tutorial () ![]()
0 Comments
Leave a Reply. |