![matlab tutorial matlab tutorial](https://ctms.engin.umich.edu/CTMS/Content/Extras/figures/buildn.png)
The special operator ' (prime or apostrophe) denotes the transposition of a matrix.
#Matlab tutorial series#
You can optionally give the range indicator a step size as the middle element in a series of colons. Matrix and vector exponentiation are discussed later.Īnother useful operator is the colon ( :), which you use to specify a range of numbers (as a vector). x^2 does not square each element of x, but x.^2 does. 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. You can also add, subtract, multiply or divide all the elements of a vector or matrix by a scalar. The following table summarizes simple MATLAB notation:Īll of these work for two scalars, including complex scalars. MATLAB uses a straight-forward notation for basic scalar arithmetic. If z is defined as in the above examples,įor matrices, length is the length or the width, whichever is greater, i.e., length(z) is equivalent to max(size(z)). The length operator returns the length of a vector. You can also define the result to be two separate values (as shown in the max example): The size function returns a two-element vector giving the dimensions of the matrix with which it was called. Where (2:5) represents the sequence of index values to be taken from the larger vector. 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.
![matlab tutorial matlab tutorial](https://images-na.ssl-images-amazon.com/images/I/41FWTIhxtfL._SY494_BO1,204,203,200_.jpg)
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:
![matlab tutorial matlab tutorial](http://i.ytimg.com/vi/ANxZ42A4qmo/maxresdefault.jpg)
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:
#Matlab tutorial mac#
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 ()