This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Vectors • A vector – the primary way MATLAB stores homogenous data (data of one type – for example, all numbers, all letters, etc) • Creating a vector • Direct entry • A = [1 2 3 4 5] • Rangespecification (“colon operator”) • A = 1:5 OR A = 1:1:5 (both the same) • (When the “1” in the middle of 1:1:5 is omitted, the step between each number is understood to be one) • B = 1:2:7 = [1 3 5 7] • Using the function linspace • A = linspace(1,5,5) = [1 2 3 4 5] • First number is the first element in the vector, second number is the last element in the vector, third number is the total number of elements in the vector • Accessing/Manipulating a vector • Indexing – the primary method of accessing/removing/changing elements inside of a vector • Create vector A: A = [1 2 3 4 5] • To access the third element of A and assign it to a variable called B, do the following: • B = A(3) • Now, B is equal to the number 3 (which is the third element of A) • To change the third element of A to the number 8, do the following: • A(3) = 8 • Now, A = [1 2 8 4 5] • What if we wanted to make the 8 th element of A the number 4? If A only has 5 elements, will we receive an error? No – MATLAB will compensate, filling in zeros for elements 6 through 7: • A(8) = 4 • Now, A = [1 2 8 4 5 0 0 4] • To remove an item from a vector, set the vector indexed at the element in question equal to empty brackets ( ): • A(5) = (delete the fifth element of A) • Now, A = [1 2 8 4 0 0 4] • Notice how the length of A decreased by one when we deleted an element (kind of obvious, but this has important later) • All of these indexing techniques apply to ranges as well as single numbers; for example, to make the 1 st five elements of A equal to 3: • A(1:5) = 3 • Now, A = [3 3 3 3 3 0 0 4] • To delete the first five elements of A: • A(1:5) = • Now, A = [0 0 4] • Concatenating Vectors • To “Concatenate” two vectors means to create a new vector by using elements of the old vectors. For example, • Create vector A: A = [1 2 3 4 5] • Create vector B: B = [6 7 8 9 10] • Now, create vector C where C vectors A and B concatenated together: • C = [A, B] • Now, C = [1 2 3 4 5 6 7 8 9 10] • This technique can also be combined with indexing to make a new vector with only certain elements of the first two vectors. For example, To create a new vector C from vectors A and B that consists of the first three elements of A and the last three elements of B: • C = [A(1:3), B(end2:end)] • Now, C = [1 2 3 8 9 10] • Note: the ‘end’ operator is simply is another way of indexing from the last element of a vector; we can also specify indices using mathematical operations such as + and . So, “end2” would be read by MATLAB as the last element of B minus two more elements. Thus, if B has five elements, then “end2” would be 52, or the third element (which in this case is the number 8). So another way of reading B(end2:end) would be...
View
Full Document
 Spring '08
 Stallworth
 Madrid Metro, Metropolitana di Napoli, ASCII

Click to edit the document details