Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

An Introduction to Matlab: Part 3 - Creating and Manipulating Vectors, Study notes of Mathematics

A lecture script on creating and manipulating vectors in matlab. It covers creating row and column vectors using different methods, vector operations, and applying functions to vectors. It also explains component-wise arithmetic.

Typology: Study notes

Pre 2010

Uploaded on 08/31/2009

koofers-user-9to
koofers-user-9to 🇺🇸

10 documents

1 / 4

Toggle sidebar

Related documents


Partial preview of the text

Download An Introduction to Matlab: Part 3 - Creating and Manipulating Vectors and more Study notes Mathematics in PDF only on Docsity! An Introduction to Matlab: Part 3 This lecture assumes that you have already worked through part 1 and part 2. You should be able to use many basic Matlab commands and use Matlab as a calculator on scalar variables. You should be able to create and execute a script le. This lecture covers • Creating row/column vectors • Vector operations • Applying functions to vectors • Component wise arithmetic Creating vectors This section goes through creating row and column vectors by typing each element, by using patterns, and by using a few built in functions. 1. Typing in vectors explicitly: Here we learn how to type vectors. (a) Open Matlab. If you already have it open, type clear all; in the Command Window. You may do everything here in a script le or on the Command Window. I would suggest simply using the Command Window for now so you don't have to rerun you le everytime we make a change. (b) Let us create a row vector consisting of the elements 1, 3, 5 and 7. To do this, we use the bracket notation []. We assign this vector to the variable u. u=[1,3,5,7] Instead of using commas, try simply inserting a space between each element. Type u=[1 3 5 7] Note that they are the same thing. Also note that this is a row vector (Matlab prints out the numbers in a row). (c) Let us create a column vector consisting of the same elements 1, 3, 5 and 7. For a row vector, we put either spaces or commas in between elements. For a column vector, we put a semicolon between each element. Note that a semicolon is also used to supress output, but Matlab is smart enough to know that a semicolon within brackets [] means to change rows. Type v=[1;3;5;7] Note that Matlab displays the elements of v as a column. 2. Typing in vectors using a pattern: We create vectors which are large and correspond to a distinct pattern. (a) Suppose we want to create a row vector, u, that contains elements 1 through 100 (ie u = [1, 2, . . . , 99, 100]). How can we do this? Well, you can type in each element, but that seems awfully silly. Matlab has a nice way to do this. Type u=[1:1:100] This notation means u goes from 1 by 1 to 100. Create the vector u = [1, 3, 5, 7, 9, 11] using this notation. How do you think you would create the vector u = [15, 12, 9, 6, 3, 0]? Well, it goes from 15 by −3 to 0. So we type u=[15:-3:0] We can also use negative numbers. Create a vector that goes from −15 to 0 by 3. (b) Note that using the notation above (u=[15:-3:0]) created a row vector. Let us say we want a column vector instead. The easiest way to do this is to use the transpose operator, which is the single quote: '. Type: u=[15:-3:0] v=u' What happened? Well, u is created as a row vector, then v is the transpose of u, which is a column vector. We could have simply done this on one line also by typing v = [15:-3:0]' Try to create a column vector that goes from 2 to 16 by 2. 1 3. Creating a zero vector, one vector, or random vector: Here we discuss how to create a vector of all zeros, all ones, or of completely random entries (between 0 and 1) (a) To create a vector of all zeros, we use the zeros function. It takes two (for now) arguments. These represent the size of the vector you wish to create. Type: u = zeros(5,1) You get a column vector with 5 zeros. Try to create a row vector with 10 entries. (b) To create a vector of all ones, we use the ones function. It has the same structure as the zeros function: v = ones(1,6) (c) You can also create a vector with random entries (these random entries are randomly drawn from between 0 and 1) using the rand function: u = rand(4,1) Vector operations This section goes through basic vector arithmetic and how to create new vectors using parts of old vectors. 1. Vector addition and scalar multiplication: We learn how to add vectors and scale vectors. (a) Type clear all; Create three row vectors, u, v and w by doing the following: u = [1 3 5 7] v = [0 1 2 4] w = [2 4 6 8 10] Add vectors u and v by typing: u+v Note that your result is another vector. We could assign it a varible by typing r=u+v . Now add v and w. What happens? We get an error because they are not the same size. Recall that we can only add vectors of the same size. Create a new vector, v1, that is the transpose of v , again by typing : v1=v'; Try adding v1 and v. What happens? An error again. Why? Because row vectors and column vectors cannot be added together. (b) How do you suppose we multiply a vector by a scalar? Well, exactly how you would expect! Let us multiply w by 2 by typing: 2*w Find πu and e2v by doing the same. 2. New vectors from old vectors: Here we'll learn how to incorporate an old vector as part of a new vector, or how to extract part of an old vector. (a) We'll use the same u, v, and w from above, so recreate the vectors if need be. Let us say that I only want to look at the 2nd element (component) of vector v. How might I do this? Well, vectors are indexed from 1 until their size (4 in this case) and are accessed using parenthesis. So typing: v(2) returns the 2nd element of v. Use this idea to nd the 1st and 4th components of v. (b) Let us say that I want more than one element of v. Say I want to get a new vector that is the same as v, but without the 2nd element. So I want a new vector, call it v1, that is size 3 an contains the 1st, 3rd and 4th components of v. I could type: v1=[v(1) v(3) v(4)] but for very large vectors, this will not be feasible. Another way I could do this is by typing: 2
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved