Physics 326G Winter 2008 Class 2 In this class you will learn how to define and work with arrays or vectors. Matlab is designed to work with arrays. An array is a list of numbers (or other things) arranged in rows and/or columns. One-dimensional arrays are also called vectors, and can be row vectors (1 row, n columns) or column vectors (n rows, 1 column). We will deal with two-dimensional (and higher-dimensional) arrays next class. Suppose we have the following set of data: Time: 3 4 6 9 10 13 Dose: 12 14 15 17 18 20 We can enter these data into arrays by enclosing the numbers in square brackets. For example, [3 4 6 9 10 13] produces a row vector with 6 elements. To define array variables, just use the assignment operator (=): time1 = [3 4 6 9 10 13]; You can enter the array elements separated by commas if you want: time1 = [3, 4, 6, 9, 10, 13]; does the same thing. Define row vectors time1 and dose1 containing the above data. You can define column vectors in the same way but using semicolons to separate the rows. time2 = [3; 4; 6; 9; 10; 13]; produces a column vector with 6 rows. Define column vectors time2 and dose2. You can add or subtract vectors in the obvious way: time1 + dose1 time2 dose2
The vectors you are adding or subtracting have to be the same size. Since time1 is a 1 6 row vector and time2 is a 6 1 column vector, time1 + time2 will not work. Multiplication and division of arrays are more complicated stay tuned. You can create a sequence of numbers using the colon (:). Type 1:10 This generates a sequence of numbers from 1 to 10, and puts them in an array. You can give the array a name using the assignment operator (=). myseq = 1:10 To generate a sequence with a spacing between the elements different from one, you provide the first element, the spacing, and the last element. Try myseq = 1:2.5:10 The sequence will stop at the last element less than or equal to the limit specified. To see this, try myseq = 1:2:10 The spacing need not be positive. Try myseq = 1:-0.5:-10 You specify particular elements of an array by their element number, called their index. The array produced in the last example has 23 elements. The first is myseq(1), which is equal to 1. The third is myseq(3), and so on. Specifying an index that is not an integer, or is less than 1, or is greater than the length of the array gives an error. If you don t know the length of an array you can still address the last element by saying myseq(length(myseq)) To address several elements, you make an array of the indices you want. Try myseq([1,3,8]) and myseq(1:5)
You can add elements to an array to make it longer. Try myseq(24) = 99 Then type myseq(26) = 100 The element myseq(25) has not been defined, so Matlab just sets it equal to zero. You can also concatenate arrays like this: array1 = [1 2 3]; array2 = [5 6 7 8 9]; array3 = [array1, array2] or for column arrays array1 = [1; 2; 3]; array2 = [5; 6; 7]; array3 = [array1; array2] Note that the arrays you are concatenating here all have to have the same number of rows. Investigate the function CAT, which concatenates arrays. Figure out what the ' (prime) operator does to an array, as in array1 The elements of an array need not be numbers. Character strings are stored in arrays, with each element of the array being a single character. Strings have to be enclosed in single quotes. Define a string variable called name: name = Waldo What is name(3)? name(2:4)? STRCAT and STRVCAT concatenate arrays of strings. There are several ways to multiply arrays. First, you can always multiply an array by a scalar: 3*time1 Here, each element of the array is multiplied by the scalar factor. Second, you can multiply element by element with the.* (dot times) operator, as in a.*b. This gives a vector in which the nth element is the nth element of the array a multiplied by the nth element of the array b. Try
time1.* dose1 The vectors here have to have the same dimensions or you will get an error. Third, you can do matrix multiplication with *. Try time1 * time2 and time2 * time1 If you don t understand the results of these operations you need to review your old linear algebra notes! Note that in the operation a*b the number of columns in a must equal the number of rows in b. You can also divide an array by a scalar, in which case each element is divided by the scalar: dose1/3 You can divide two arrays element by element using dot-divide, a./b, in which case the nth element of the array a is divided by the nth element of the array b. time1./ dose2 We will get into matrix division next class. Element-by-element exponentiation works similarly. Figure out what happens when you do time1.^2 Now do the following exercises to get some practice working with arrays. 1. Create a row vector called vec1 which has elements going from 0 to 100 in steps of 4. Now create a column vector vec2 that is the transpose of vec1. 2. Create a new row vector vec3 that consists of elements 1 to 3 and 18 to 21 of vec1. 3. Create vec4 which has elements 4. Calculate the following: a. vec3+vec4 b. vec3./vec4 c. vec3.*vec4 d. vec4.^2 e. vec1/4 2 3 7 x, x, x... x, where x = 1.22.
5. Many functions will take arrays as arguments. Calculate a. sin(vec4) b. log(vec4)/log(x) 6. Define an array t that runs from 0 to 30 in steps of 0.1. Now make a new array called sint equal to the sine of your array t. The command PLOT(x,y) will make a simple graph of y vs. x. Try it. Plot sin t vs. t. Then plot sin t against t. t