Development Environmen Other develo。 pment Envir。 nment Features Additional development environment features are Importing and Exporting Data-Techniques for bringing data created by other applications into the matlab workspace, including the Import Wizard, and packaging MATLAB workspace variables for use by other pplication Interfacing with Source Control Systems--Access your source control system from within MATLAB Simulink @ and Stateflow Using Notebook-Access MATLAB numeric computation and visualization software from within a word processing environment (microsoft Word) 2-16
2 Development Environment 2-16 Other Development Environment Features Additional development environment features are • Importing and Exporting Data—Techniques for bringing data created by other applications into the MATLAB workspace, including the Import Wizard, and packaging MATLAB workspace variables for use by other applications. • Interfacing with Source Control Systems—Access your source control system from within MATLAB, Simulink®, and Stateflow®. • Using Notebook—Access MATLAB numeric computation and visualization software from within a word processing environment (Microsoft Word)
Manipulating Matrices This section provides an introduction to matrix operations in MATLAB Matrices and Magic Squares(p 3-2) Enter matrices, perform matrix operations, and access Expressions(p 3-10) Work with variables, numbers, operators, functions Working with Matrices(p 3-14) Generating matrices, load matrices, create matrices from M-files and concatenation and delete matrix rows and More about Matrices and Arrays Use matrices for linear algebra, work with arrays (p.3-18) multivariate data, scalar expansion, and logical ubscripting, and use the find function. ontrolling Command Window Input Change output format, suppress output, enter long lines and Output (p 3-28) and edit at the command line
3 Manipulating Matrices This section provides an introduction to matrix operations in MATLAB. Matrices and Magic Squares (p. 3-2) Enter matrices, perform matrix operations, and access matrix elements. Expressions (p. 3-10) Work with variables, numbers, operators, functions, expressions. Working with Matrices (p. 3-14) Generating matrices, load matrices, create matrices from M-files and concatentation, and delete matrix rows and columns. More About Matrices and Arrays (p. 3-18) Use matrices for linear algebra, work with arrays, multivariate data, scalar expansion, and logical subscripting, and use the find function. Controlling Command Window Input and Output (p. 3-28) Change output format, suppress output, enter long lines, and edit at the command line
Manipulating matrices Matrices and Magic Squares In MATLAB, a matrix is a rectangular array of numbers. Special meaning is sometimes attached to l-by-l matrices, which are scalars, and to matrices with only one row or column, which are vectors. MatLAB has other ways of storing both numeric and nonnumeric data, but in the beginning, it is usually best to think of everything as a matrix. The operations in matlab are designed to be as natural as possible. Where other programming languages work with lumbers one at a time matlab allows you to work with entire matrices quickly and easily. a good example matrix, used throughout this book, appears in the renaissance engraving Melencolia I by the german artist and amateur athematician Albrecht dure 3-2
3 Manipulating Matrices 3-2 Matrices and Magic Squares In MATLAB, a matrix is a rectangular array of numbers. Special meaning is sometimes attached to 1-by-1 matrices, which are scalars, and to matrices with only one row or column, which are vectors. MATLAB has other ways of storing both numeric and nonnumeric data, but in the beginning, it is usually best to think of everything as a matrix. The operations in MATLAB are designed to be as natural as possible. Where other programming languages work with numbers one at a time, MATLAB allows you to work with entire matrices quickly and easily. A good example matrix, used throughout this book, appears in the Renaissance engraving Melencolia I by the German artist and amateur mathematician Albrecht Dürer
Matrices and Magic Squares This image is filled with mathematical symbolism, and if you look carefully you will see a matrix in the upper right corner This matrix is known as a magic square and was believed by many in durer's time to have genuinely magica properties. It does turn out to have some fascinating characteristics worth exploring Entering Matrices The best way for you to get started with maTlaB is to learn how to handle matrices. Start MATLAB and follow along with each example You can enter matrices into MATLAB in several different ways: Enter an explicit list of elements. Load matrices from external data files Generate matrices using built-in functions Create matrices with your own functions in M-files Start by entering Durer's matrix as a list of its elements. You only have to follow a few basic conventions. Separate the elements of a row with blanks or commas Use a semicolon .: to indicate the end of each row Surround the entire list of elements with square brackets, 3-3
Matrices and Magic Squares 3-3 This image is filled with mathematical symbolism, and if you look carefully, you will see a matrix in the upper right corner. This matrix is known as a magic square and was believed by many in Dürer’s time to have genuinely magical properties. It does turn out to have some fascinating characteristics worth exploring. Entering Matrices The best way for you to get started with MATLAB is to learn how to handle matrices. Start MATLAB and follow along with each example. You can enter matrices into MATLAB in several different ways: • Enter an explicit list of elements. • Load matrices from external data files. • Generate matrices using built-in functions. • Create matrices with your own functions in M-files. Start by entering Dürer’s matrix as a list of its elements. You only have to follow a few basic conventions: • Separate the elements of a row with blanks or commas. • Use a semicolon, ; , to indicate the end of each row. • Surround the entire list of elements with square brackets, [ ]
Manipulating matrices To enter Durer's matrix, simply type in the Command window A=[163213;510118;96712;415141 MATLAB displays the matrix you just entered 16 2 51011 8 9 6 This exactly matches the numbers in the engraving. Once you have entered the matrix, it is automatically remembered in the matlab workspace. You can refer to it simply as A. Now that you have a in the workspace take a look at what makes it so interesting. Why is it magic? sum, transpose, and diag You are probably already aware that the special properties of a magic square have to do with the various ways of summing its elements. If you take the sum along any row or column, or along either of the two main diagonals, you will always get the same number. Let us verify that using MATLAB. The first MATLAB replies with When you do not specify an output variable, matlAB uses the variable ans short for answer, to store the results of a calculation. You have computed a row vector containing the sums of the columns of A. Sure enough, each of the columns has the same sum, the magic sum, 34 How about the row sums? MAtLAB has a preference for working with the columns of a matrix, so the easiest way to get the row sums is to transpose the matrix, compute the column sums of the transpose and then transpose the result. The transpose operation is denoted by an apostrophe or single quote It flips a matrix about its main diagonal and it turns a row vector into a column vector
3 Manipulating Matrices 3-4 To enter Dürer’s matrix, simply type in the Command Window A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1] MATLAB displays the matrix you just entered. A = 16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1 This exactly matches the numbers in the engraving. Once you have entered the matrix, it is automatically remembered in the MATLAB workspace. You can refer to it simply as A. Now that you have A in the workspace, take a look at what makes it so interesting. Why is it magic? sum, transpose, and diag You are probably already aware that the special properties of a magic square have to do with the various ways of summing its elements. If you take the sum along any row or column, or along either of the two main diagonals, you will always get the same number. Let us verify that using MATLAB. The first statement to try is sum(A) MATLAB replies with ans = 34 34 34 34 When you do not specify an output variable, MATLAB uses the variable ans, short for answer, to store the results of a calculation. You have computed a row vector containing the sums of the columns of A. Sure enough, each of the columns has the same sum, the magic sum, 34. How about the row sums? MATLAB has a preference for working with the columns of a matrix, so the easiest way to get the row sums is to transpose the matrix, compute the column sums of the transpose, and then transpose the result. The transpose operation is denoted by an apostrophe or single quote, '. It flips a matrix about its main diagonal and it turns a row vector into a column vector