{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Final+Exam+Review - Vectors Conditionals and Itteration...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Vectors Conditionals and Itteration Functions Strings Cell Arrays Structures and Structure Arrays Arrays File I/O Recursion Plotting Bodies of Rotation Matrices Images Numerical Methods Sound Sorting Queues (Graph Search)
Image of page 2
We remember these right? Of course, you’ve been doing them all semester now! They contain only numerical values of class double. You can create them several ways, such as: 1. Colon Operator: x = 2:2:10; x = [2 4 6 8 10]; 2. Manually using square brackets : x = [1 2 3 4 5]; 3. Linspace: x = linspace(4, 20, 5) x = [4 8 12 16 20]; Remember linspace assumes 100 values if you do not specify in the third input parameter. 4. Concatenation: x = [A B C]; horizontally concatenates vectors A, B, and C.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Indexing/Assignment : If you want to index the 2 nd element in your vector: x = vec(2); If you have a vector, vec, and you want the third element to be a 4: vec(3) = 4; Or if you wish the 5 th and 6 th elements in your vector to be a 7 and an 8: vec([5 6]) = [7 8]; If you wish to remove the last 3 elements of the vector: vec(end-3:end) = [ ]; To get all even values from vec: new_vec = vec(mod(v1,2)==0) To get the values of all the even indices: new_vec=vec(2:2:end)
Image of page 4
Remember ‘if’ and ‘switch’ statements? if’ Statements: if <condition> <code> elseif <condition> <code> else <code> end *do NOT forget the end!! Switch Cases: switch <variable> case {‘option1’, ‘option2’} <code> case ‘option’ <code> othewise <code> end
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
We would use an if statement to test multiple things at once. For instance if we have several variables that play into the coding decision we need to make: x needs to be larger than 5, y needs to be smaller than 7, and STR needs to be ‘hello world’ for us to add 6 to our vector, but if J is larger than 8 then we want to remove the last three letters from our string…. you see where this is going? Multiple variables are playing into the actions we are going to take. We use Switch if we are testing a single variable, say X, for certain conditions to carry out certain code. if we ‘switch x’ all of our cases are going to be wether ‘x’ is greater than, equal to, less than, true, false… ect.
Image of page 6
For loops ! For loops are used to manipulate a vector or a variable with a known length. We can then set up an index something such as: for index = 1:length(variable) <code> end If you are deleting things from the variable as you loop through however, you’ll need to start from the end: for index = length(variable):-1:1 so that your index does not exceed the boundaries of the length of your variable, as it will be getting shorter each time.
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
While Loops! While loops are a little more ambiguous and sometimes more difficult to formulate. They are however more useful in situations where you have no idea how many iterations (such as 1:length(variable)) you are going to need to carry out to come to a solution. The idea is you are going to iterate until a certain condition becomes true, example: ***x = 1; while x<5 x = x + rand(1,1); end Here we are adding random numbers until x becomes larger than 5, we don’t know how many times that will take.
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern