MAE 08 Lecture Notes - Lecture 14: Matlab
Document Summary
Combined use of for/while loops with if/switch statements. Use for loop to create a vector v: V = [ 1 4 9 16 25 36 49 64 81 100] Use for loop to compute sum of all elements in the vector. Use for loop to compute difference between two consecutive elements in the vector. Use for loop to compute product of all elements in the vector. Pre-allocating a vector/ matrix is better in terms of speed. Compare 2 ways to create a vector a = [1: 10000] Case 1: create a vector a on the fly with for loop. Case 2: preallocate the vector and then use for loop to modify values of each elements. Nested for loops: for loopvar1 = range1 actions for loopvar2 = range 2 end actions end actions (outer loop) (inner loop) Given: h(x,y) = exp[-(x2 + y2)] for x = [-1: 0. 1: 1] and y = [-2: 0. 1: 2]