Unformatted text preview:

ECE 2610 Signal and Systems A–1MATLAB Basicsand MoreScalars, Vectors, and Matrices• MATLAB was originally developed to be a matrix laboratory• In its present form it has been greatly expanded to includemany other features, but still maintains inherent matrix andvector based arithmetic at its core• The basic data structure of MATLAB is the matrix– A matrix with a single row is also known as a row vector– A matrix with a single column is also known as a columnvector– A matrix with just one row and one column (a single ele-ment) is simply a scalarAa11a12a13a21a22a23a31a32a33Bb11b12=,=Cc11c21= Dd11=,AppendixAAppendix A • MATLAB Basics and MoreA–2 ECE 2610 Signals and SystemsVariable Initialization• Variable names in MATLAB– Must be begin with a letter– May contain digits and the underscore character– May be any length, but must be unique in the first 19 char-acters• A common way to create a matrix instance is by simplyassigning a list of numbers to it, e.g.A = [3.1415]; % A 1x1 matrixB = [23, 35.3]; % A 1x2 matrixC = [1, 2, 3; 4, 5, 6; 7, 8, 9]; % a 3x3 matrix– Note: a comma or a blank is used to separate elements inthe same row, while a semicolon is used to separate rows– The assignment statements listed above are terminatedwith a semicolon to suppress MATLAB from echoing thevariables value• To continue one line into another we can break a line where acomma occurs, and then follow the comma with an ellipsis(three periods in a row), e.g.,M = [.1, .2, .3, .4, .5, .6, .7, .8, .9, 1.0];% or break the lineM = [.1, .2, .3, .4, .5, ....6, .7, .8, .9, 1.0];• One matrix may be used to define another matrix, e.g.,A = [4, 5, 6];B = [1, 2, 3, A]; % createsB = [1, 2, 3 ,4 ,5 ,6];Scalars, Vectors, and MatricesECE 2610 Signals and Systems A–3The Colon Operator• To make matrix generation and addressing easier we use thecolon operator• The colon operator is indeed powerful, and mastering it isessential to becoming a MATLAB expert– Here we use it to generate row vectorsk = 0:6; % createsk = [0, 1, 2, 3, 4, 5, 6];t = 0:.25:2; % createst = [0, .25, .5, .75, 1, 1.25, 1.5, 1.75, 2];s = -10:2:0; % createss = [-10, -8, -6, -4, -2, 0];– Here we use to generate row and column slices of amatrixA = [1, 2, 3; 4, 5, 6; 7, 8, 9];A_col2 = A(:,2); % Span all rows with the column % index fixed at 2.A_row1 = A(1,:); % With row index fixed at 1, % span all columns.A_11 = A(2,2); % Produces the scalar A_11 = 5• We can combine the techniques to extract a submatrix of AA_sub = A(2:3,2:3) % Extract a sub matrix % consisting of rows 2-3 and % columns 2-3A123456789=Appendix A • MATLAB Basics and MoreA–4 ECE 2610 Signals and Systems• We can swap the rows and columns of a matrix using thetranspose operator, e.g.,A = [1, 2, 3];A_transpose = A’; % producesA_transpose = [1; 2; 3];• A simple formatting scheme to have MATLAB display thevalues of several equal length vectors side-by-side is the fol-lowing (actual MATLAB command line interaction)>> A = [0, 1, 2, 3, 4];>> B = [0, 1, 4, 9, 16];>> C = [0, 1, 8, 27, 64];>> % Display in side-by-side columns:>> [A' B' C'] % commas may be included but not neededans = 0 0 0 1 1 1 2 4 8 3 9 27 4 16 64Special Values and Matrices• To make life easier, MATLAB comes with several predefinedvalues and matrix generators– pi represents in MATLAB floating point precision, e.g.,>> pians = 3.1416Asub5689=πScalars, Vectors, and MatricesECE 2610 Signals and Systems A–5– i, j represents the value – Inf is the MATLAB notation for infinity, i.e., 1/0– Nan is the MATLAB representation for not-a-number;often a result of a 0/0 operation– clock displays the current time, e.g.,>> clockans = 1.0e+003 * 1.9970 0.0080 0.0270 0.0230 0.0160 0.0508– date is the current date in a string format>> dateans = 23-Aug-2006– eps is the smallest floating-point number by which twonumbers can differ, e.g.,>> epsans = 2.2204e-016• A matrix of zeros can be generated withA_3x3 = zeros(3,3); % orA_3x3 = zeros(3);B_3x2 = ones(3,2);C_3x3 = ones(size(A_3x3));1–Appendix A • MATLAB Basics and MoreA–6 ECE 2610 Signals and Systems• In linear algebra the identity matrix is often neededI_3x3 = eye(3);I_3x2 = eye(3,2);User Prompting• In the future we will write MATLAB programs or script files(saved as *.m files)• Short of making a full graphical user interface (GUI) MAT-LAB program, which MATLAB can also do, we may simplywish to prompt the user to supply some input data• When the following is encountered in a MATLAB program,the variable on the left is assigned the value the user enters atthe MATLAB promptx = input(‘Please enter your height and weight: ’);A3x3000000000= B3x2111111=,C3x3111111111=I3x3100010001= I3x2,100100=Output Display/Print FormattingECE 2610 Signals and Systems A–7– We create a simple script file (more on this in the nextchapter) user_prompt– To run the script we type the file name at the MATLABprompt (making sure the file is in the MATLAB path)>> clear % clear all variables form the workspace>> user_prompt % run the script file user_prompt.mPlease enter your height and weight: [100 200]>> xx = 100 200Output Display/Print Formatting • Globally the display format for the command window can bechanged by:– Typing commands directly, e.g.,format short % four decimal digitsformat long % 14 decimal digitsformat short e % short with scientific notationformat long e % long with scientific notation% others available% the *.m file user_prompt.mx = input(‘Please enter your height and weight: ’);Appendix A • MATLAB Basics and MoreA–8 ECE 2610 Signals and Systems– Selecting displays formats from the command window pulldown menus, e.g.,• Formatted output from script files is also possible, that is theformat of variables printed to the screen can be made uniquefor each variable you want the program to print, and customtext be distributed around the numeric values• The command disp() is used to display text and print thecontents of a matrix>> disp('A Text String to Display.')A Text String to Display.>> M = [1, 2; 3, 4];>> disp(M) 1 23 4The x–y Plot CommandECE 2610 Signals and Systems A–9• A fully customizable way of printing both text and matricesis the fprintf() commandfprintf(format_string,matrices)• The format string contains text you wish to display


View Full Document

UCCS ECE 2610 - MATLAB Basics and More

Download MATLAB Basics and More
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view MATLAB Basics and More and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view MATLAB Basics and More 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?