{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Final+Exam+Review

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

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

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

View Full Document
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)
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.

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

View Full Document
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)
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

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

View Full Document
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.
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.

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

View Full Document
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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern