ECE300 Complicated Sinusoidal Signals MATLAB Programming Tips This section presents a few programming tips that should help improve your MATLAB programs For more ideas and tips list some of the functions M files in the toolboxes of MATLAB using the type command Copying the style of other programmers is always an efficient way to improve your own knowledge of a computer language In the hints below we discuss some of the most important points involved in writing good MATLAB code These comments assume that your are both an experienced programmer and at least an intermediate user of MATLAB Matrix Operations The default notation in MATLAB is matrix Therefore some confusion can arise when trying to do point wise operations Take the example of multiplying two matrices A and B If the two matrices have compatible dimensions then A B is well defined But suppose that both are 5 8 matrices and that we want to multiply them together element by element In fact we can t do matrix multiplication between two 5 8 matrices To obtain point wise multiplication we use the point star operator A B In general when point is used with another arithmetic operator it modifies that operator s usual matrix definition to a point wise one Thus we have and for point wise division and exponentiation For example xx 0 9 0 49 generates an exponential of the form an for n 0 1 2 49 A Review of Matrix Multiplication Recall that before a matrix A can multiply another matrix B the number of columns in A must equal the number of rows in B For example if A is m n then B must be n l The product of these two matrices would be m l Obviously matrix multiplication is not commutative as the product B A is undefined for l m To generate the first element in the product of two matrices A and B simply take the first row of A and multiply point by point with the first column of B then sum For example if b1 b1 2 a1 a1 2 a1 3 A and B b2 1 b2 a2 1 a2 a2 3 b3 1 b3 2 then the first element of C A B would be c1 1 a1 1 b1 1 a1 2 b2 1 a1 3 b3 1 1 14 2019 1 ECE300 Complicated Sinusoidal Signals c1 2 is found by taking the first row of A and multiplying point by point with the second column of B then summing c2 1 is found by taking the second row of A and multiplying point by point with the first column of B then summing Finally c2 2 is found by multiplying the second row of A point by point with the second column of B then summing The result would be C c1 1 c1 2 c2 1 c2 2 a1 1b1 1 a1 2 b2 1 a1 3 b3 1 a1 1b1 2 a1 2 b2 2 a1 3 b3 2 a2 1 b1 1 a2 2 b2 1 a2 3 b3 1 a2 1b1 2 a2 2 b2 2 a2 3b3 2 Some useful results are as follows a1 a1 a1 a1 a2 1 1 1 1 a2 a2 a2 a a a a 3 3 3 3 a1 a2 a3 and a1 a2 a3 1 1 a4 a1 a2 a3 a4 1 1 There may be times when you want to multiply matrices element by element that is ci j ai j bi j Obviously the matrices would have to be the same size to do this In MATLAB this is accomplished using the operation For example if A and B are both 2 2 c1 1 c1 2 a1 1 b1 1 a1 2 b1 2 C A B B A c2 1 c2 2 a2 1 b2 1 a2 2 b2 2 Also remember in MATLAB exp a1 1 exp a1 2 L exp a1 n exp a2 1 exp a2 2 L exp a2 n exp A M M M M exp am 1 exp am 2 L exp am n and 1 14 2019 2 ECE300 Complicated Sinusoidal Signals cos a1 1 cos a1 2 L cos a1 n cos a2 1 cos a2 2 L cos a2 n cos A M M M M cos a cos a L cos a m 1 m 2 m n Avoid FOR Loops There is temptation among experienced programmers to use MATLAB in the same fashion as a high level language like C However this leads to very inefficient programming whenever for loops are used to do operations over the elements of a vector e g summing the elements in a vector Instead you must look for the MATLAB functions or vector operations that will do the same operation with a function call in the case of summing there is a MATLAB function called sum A for loop is extremely inefficient in MATLAB because it is an interpreted language Macro operations such as matrix multiplies are about as fast as micro operations such as incrementing an index because the overhead of interpreting the code is needed in both cases The bottom line is that for loops should only be used as a last resort and then probably only for control operations not for computational reasons More than likely 90 of the for loops used in ordinary MATLAB programs could be replaced with equivalent and faster vector code Writing a MATLAB Function You can write your own functions and add them to the MATLAB environment These functions are just a type of M file and are created as an ASCII file via a text editor The first word in the M file must be the keyword function to tell MATLAB that this file is to be treated as a function with arguments On the same line as the word function is the calling template that specifies the input and output arguments of the function The filename for the M file must end in m and the filename will become the name of the new command for MATLAB For example consider the following file function y foo x L FOO get last L points of x usage y foo x L where x input vector L number of points to get y output vector N length x if L N error input vector too short end y x N L 1 N If this file is called foo m the operation could be invoked from the MATLAB command by typing aaa foo rand 11 1 7 The effect will be to generate a vector of eleven random numbers and then get the last seven Note that the variable names inside the function are all local variables and they disappear after the function completes The argument names y x and L are dummy names which are passed values when the function is invoked 1 14 2019 3 ECE300 Complicated Sinusoidal Signals Creating Your Own Functions We will show that most functions can be written according to a standard format Consider a clip function M file that takes two input arguments a signal vector …
View Full Document