Arrays: Vectors and Matrices: Initializing Vectors: Explicit Lists
Arrays: Vectors and Matrices: Initializing Vectors: Explicit Lists
As mentioned earlier, the name MATLAB stands for MATrix LABoratory because MATLAB has been designed to work with matrices. A matrix is a rectangular object (e.g. a table) consisting of rows and columns. We will postpone most of the details of proper matrices and how MATLAB works with them until Chapter 6. A vector is a special type of matrix, having only one row, or one column. Vectors are also called lists or arrays in other programming languages. If you havent come across vectors officially yet, dont worryjust think of them as lists of numbers. MATLAB handles vectors and matrices in the same way, but since vectors are easier to think about than matrices, we will look at them first. This will enhance your understanding and appreciation of many other aspects of MATLAB. As mentioned above, MATLAB refers to scalars, vectors and matrices generally as arrays. We will also use the term array generally, with vector and matrix referring to their one-dimensional (1-D) and two-dimensional (2-D) forms.
linspace
The function linspace can be used to initialize a vector of equally spaced values, e.g. linspace(0, pi/2, 10) creates a vector of 10 equally spaced points from 0 to /2 (inclusive).
Transposing vectors
All of the vectors examined so far are row vectors. Each has one row and several columns. To generate the column vectors that are often needed in mathematics, you need to transpose such vectors, i.e. you need to interchange their rows and columns. This is done with the single quote, or apostrophe (), which is the nearest MATLAB can get to the mathematical dash () that is often used to indicate the transpose. Enter x = 1:5 and then enter x to display the transpose of x. Note that x itself remains a row vector. Or you can create a column vector directly: y = [1 4 8 0 -1]
Subscripts
We can refer to particular elements of a vector by means of subscripts. 1. Enter r = rand(1,7) This gives a row vector of seven random numbers. 2. Now enter r(3) This will display the third element of r. The number 3 is the subscript. 3. Now enter r(2:4) This should give the second, third and fourth elements. 4. What about r(1:2:7)? 5. And r([1 7 2 6])? 6. use an empty vector to remove elements from a vector, e.g. r([1 7 2]) = [ ] will remove elements 1, 7 and 2. To summarize: A subscript is indicated inside round brackets (also called parentheses). A subscript may be a scalar or a vector. In MATLAB subscripts always start at 1. Fractional subscripts are always rounded down, e.g. x(1.9) refers to element x(1).
Matrices
A matrix may be thought of as a table consisting of rows and columns. You create a matrix just as you do a vector, except that a semicolon is used to indicate the end of a row, e.g. the statement a = [1 2 3; 4 5 6] results in a = 1 2 3 4 5 6 A matrix may be transposed, e.g. with a initialized as above, the statement a results in a = 1 4 2 5 3 6 A matrix can be constructed from column vectors of the same length. The statements x = 0:30:180; table = [x sin(x*pi/180)] result in table = 0 0 30.0000 0.5000 60.0000 0.8660 90.0000 1.0000 120.0000 0.8660 150.0000 0.5000 180.0000 0.0000
Numbers
Numbers can be represented in MATLAB in the usual decimal form (fixed point), with an optional decimal point, e.g. 1.2345 -123 .0001 A number may also be represented in scientific notation, e.g. 1.2345109 may be represented in MATLAB as 1.2345e9. This is also called floating point notation. The number has two parts: the mantissa, which may have an optional decimal point (1.2345 in this example) and the exponent (9), which must be an integer (signed or unsigned). Mantissa and exponent must be separated by the letter e (or E). The mantissa is multiplied by the power of 10 indicated by the exponent. Note that the following is not scientific notation: 1.2345*109. It is actually an expression involving two arithmetic operations (* and ) and therefore more time consuming. Use scientific notation if the numbers are very small or very large, since theres less chance of making a mistake, e.g. represent 0.000000001 as 1e-9. On computers using standard floating point arithmetic, numbers are represented to approximately 16 significant decimal digits. The relative accuracy of numbers is given by the function eps, which is defined as the distance between 1.0 and the next largest floating point number. Enter eps to see its value on your computer. The range of numbers is roughly }10308 to }10308. Precise values for your computer are returned by the MATLAB functions realmin and realmax.
Data types
MATLAB has 14 fundamental data types (or classes). The default numeric data type is doubleprecision; all MATLAB computations are done in double-precision. More information on data types can be found in the Help index. MATLAB also supports signed and unsigned integer types and single-precision floating point, by means of functions such as int8, uint8, single, etc.
Arithmetic operations between two scalarsHowever, before mathematical operations can be performed on such data types they must first be converted to double-precision using the double function.
Operation Algebraic form MATLAB Addition a+b a + b Subtraction ab a - b Multiplication ab a * b Right division a/b a / b Left division b/a a \ b Power ab a b
Arithmetic operators
The evaluation of expressions is achieved by means of arithmetic operators. The arithmetic operations on two scalar constants or variables are shown in Table 2.1. Operators operate on operands (a and b in the table). Left division seems a little curious: divide the right operand by the left operand. For scalar operands the expressions 1/3 and 3\1 have the same numerical value (a colleague of mine speaks the latter as 3 under 1). However, matrix left division has an entirely different meaning, as we shall see later Precedence of arithmetic operations
Precedence Operator 1 Parentheses 2 Power, 3 Multiplication and division, 4 Addition and subtraction, (round brackets) left to right left to right left to right
You may be surprised at the following: 1+[1:5] Were you? The value 1 is added to each element of the vector 1:5. In this context, the addition is called an array operation, because it operates on each element of the vector (array). Array operations are discussed below. See Appendix B for a complete list of MATLAB operators and their precedences.
Expressions
An expression is a formula consisting of variables, numbers, operators, and function names. An expression is evaluated when you enter it at the MATLAB prompt, e.g. evaluate 2 as follows: 2 * pi MATLABs response is: ans = 6.2832 Note that MATLAB uses the function ans (which stands for answer) to return the last expression to be evaluated but not assigned to a variable. If an expression is terminated with a semicolon (;) its value is not displayed, although it is still returned by ans.
Statements
MATLAB statements are frequently of the form variable = expression e.g. s = u * t - g / 2 * t . 2; This is an example of an assignment statement, because the value of the expression on the right is assigned to the variable (s) on the left. Assignment always works in this direction. Note that the object on the left-hand side of the assignment must be a variable name. A common mistake is to get the statement the wrong way round, like a + b = c Basically any line that you enter in the Command Window or in a program, which MATLAB accepts, is a statement, so a statement could be an assignment, a command, or simply an expression, e.g. x = 29; % assignment clear % command pi/2 % expression This naming convention is in keeping with most other programming languages and serves to emphasize the different types of statements that are found in programming. However, the MATLAB documentation tends to refer to all of these as functions. As we have seen, a semicolon at the end of an assignment or expression suppresses any output. This is useful for suppressing irritating output of intermediate results (or large matrices). A statement which is too long to fit onto one line may be continued to the next line with an ellipsis of at least three dots, e.g. x = 3 * 4 - 8 .... / 2 2; Statements on the same line may be separated by commas (output not suppressed) or semicolons (output suppressed), e.g. a = 2; b = 3, c = 4; Note that the commas and semicolons are not technically part of the statements; they are separators. Statements may involve array operations, in which case the variable on the left-hand side may become a vector or matrix.