270 likes | 356 Views
ARRAYS. CMPS1371 Introduction to Computing for Engineers. Arrays. A “Collection” is a data structure into which you can put items in, and from which you get items out. Arrays are homogeneous collection of things. They are indexable Each element of an array has a value (and a position).
E N D
ARRAYS CMPS1371Introduction to Computing for Engineers
Arrays A “Collection” is a data structure into which you can put items in, and from which you get items out. Arrays are homogeneous collection of things. They are indexable Each element of an array has a value (and a position). A vector is an one dimensional array Big part of Matlabs power is operation on entire Arrays, both on an per element basis and in a “summation” like basis.
Matrices: Two Dimensional Arrays “Mat”Lab is all about matrices – originally that's all it was about. A Matrix is a 2-D array.Behave like vectors but two dimensional. “A deck of columns.” As an array, they are indexable and homogeneous
Create Array • Remember that we can define a arrays using the following syntax: A=[3] B=[1, 3] or B=[1 3] C=[-1, 0, 0; 1, 1, 0; 0, 0, 2]; With or without commas Semi-colons start a new row
Matrix Options • 2-D Matrices can also be entered by listing each row on a separate line C = [-1, 0, 0 1, 1, 0 1, -1, 0 0, 0, 2]
These semicolons are optional Matrix Options 2-D matrix
Array Options • Use an ellipsis to continue a definition onto a new line F = [1, 52, 64, 197, 42, -42, … 55, 82, 22, 109];
Index • Indexing a matrix is based on row then column: >> A = [0 3 5 2; 7 6 0 1; 8 2 3 4] >>A(2, 4) ans = 1 2nd row 3rd column
Colon Operator • Used to define new matrices • Modify existing matrices • Extract data from existing matrices
All the rows in column 1 All the rows in column 4 All the columns in row 1
Rows 2 to 3, all the columns You don’t need to extract an entire row or column
A single colon transforms the matrix into a column MATLAB is column dominant
Indexing techniques • To identify an element in a 2-D matrix use the row and column number • For example element A(2,3) >> A = [0 3 5 2; 7 6 0 1; 8 2 3 4] >>A(2, 3) ans = 0 2nd row 3rd column
Or use single value indexing M(8) is the same element as M(2,3) >>M(2, 3) ans = 4 >>M(8) ans = 4 Element #s
More Indexing • The word “end” signifies the last element in the row or column Row 1, last element Last row, last element Last element in the single index designation scheme
Special Matrices • transpose • The transpose operator changes rows to columns or vice versa. • zeros • Creates a matrix of all zeros • ones • Creates a matrix of all ones • diag • Extracts a diagonal or creates an identity matrix • magic • Creates a “magic” matrix
Use the apostrophe to create the transpose The transpose operator makes it easy to create tables
With a single input a square matrix is created with the zeros or ones function
The diagonal function When the input argument to the diag function is a square matrix, the diagonal is returned
The diagonal function When the input is a vector, it is used as the diagonal of an identity matrix
This woodcut called Melancholia was created by Albrect Durer, in 1514. It contains a magic matrix above the angel’s head